/var/www/(Del)gepgroup.hk/php-activerecord/test/UtilsTest.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
include 'helpers/config.php';

use 
ActiveRecord as AR;

class 
UtilsTest extends SnakeCase_PHPUnit_Framework_TestCase
{
    public function 
set_up()
    {
        
$this->object_array = array(null,null);
        
$this->object_array[0] = new stdClass();
        
$this->object_array[0]->"0a";
        
$this->object_array[0]->"0b";
        
$this->object_array[1] = new stdClass();
        
$this->object_array[1]->"1a";
        
$this->object_array[1]->"1b";

        
$this->array_hash = array(
            array(
"a" => "0a""b" => "0b"),
            array(
"a" => "1a""b" => "1b"));
    }

    public function 
test_collect_with_array_of_objects_using_closure()
    {
        
$this->assert_equals(array("0a","1a"),AR\collect($this->object_array,function($obj) { return $obj->a; }));
    }

    public function 
test_collect_with_array_of_objects_using_string()
    {
        
$this->assert_equals(array("0a","1a"),AR\collect($this->object_array,"a"));
    }

    public function 
test_collect_with_array_hash_using_closure()
    {
        
$this->assert_equals(array("0a","1a"),AR\collect($this->array_hash,function($item) { return $item["a"]; }));
    }

    public function 
test_collect_with_array_hash_using_string()
    {
        
$this->assert_equals(array("0a","1a"),AR\collect($this->array_hash,"a"));
    }

    public function 
test_array_flatten()
    {
        
$this->assert_equals(array(), AR\array_flatten(array()));
        
$this->assert_equals(array(1), AR\array_flatten(array(1)));
        
$this->assert_equals(array(1), AR\array_flatten(array(array(1))));
        
$this->assert_equals(array(12), AR\array_flatten(array(array(12))));
        
$this->assert_equals(array(12), AR\array_flatten(array(array(1), 2)));
        
$this->assert_equals(array(12), AR\array_flatten(array(1, array(2))));
        
$this->assert_equals(array(123), AR\array_flatten(array(1, array(2), 3)));
        
$this->assert_equals(array(1234), AR\array_flatten(array(1, array(23), 4)));
        
$this->assert_equals(array(123456), AR\array_flatten(array(1, array(23), 4, array(56))));
    }

    public function 
test_all()
    {
        
$this->assert_true(AR\all(null,array(null,null)));
        
$this->assert_true(AR\all(1,array(1,1)));
        
$this->assert_false(AR\all(1,array(1,'1')));
        
$this->assert_false(AR\all(null,array('',null)));
    }

    public function 
test_classify()
    {
        
$bad_class_names = array('ubuntu_rox''stop_the_Snake_Case''CamelCased''camelCased');
        
$good_class_names = array('UbuntuRox''StopTheSnakeCase''CamelCased''CamelCased');

        
$class_names = array();
        foreach (
$bad_class_names as $s)
            
$class_names[] = AR\classify($s);

        
$this->assert_equals($class_names$good_class_names);
    }

    public function 
test_classify_singularize()
    {
        
$bad_class_names = array('events''stop_the_Snake_Cases''angry_boxes''Mad_Sheep_herders''happy_People');
        
$good_class_names = array('Event''StopTheSnakeCase''AngryBox''MadSheepHerder''HappyPerson');

        
$class_names = array();
        foreach (
$bad_class_names as $s)
            
$class_names[] = AR\classify($strue);

        
$this->assert_equals($class_names$good_class_names);
    }

    public function 
test_singularize()
    {
        
$this->assert_equals('order_status',AR\Utils::singularize('order_status'));
        
$this->assert_equals('order_status',AR\Utils::singularize('order_statuses'));
        
$this->assert_equals('os_type'AR\Utils::singularize('os_type'));
        
$this->assert_equals('os_type'AR\Utils::singularize('os_types'));
        
$this->assert_equals('photo'AR\Utils::singularize('photos'));
        
$this->assert_equals('pass'AR\Utils::singularize('pass'));
        
$this->assert_equals('pass'AR\Utils::singularize('passes'));
    }

    public function 
test_wrap_strings_in_arrays()
    {
        
$x = array('1',array('2'));
        
$this->assert_equals(array(array('1'),array('2')),ActiveRecord\wrap_strings_in_arrays($x));

        
$x '1';
        
$this->assert_equals(array(array('1')),ActiveRecord\wrap_strings_in_arrays($x));
    }
};
?>