a tiny mvc framework for php using php-activerecord
1<?php
2include 'helpers/config.php';
3
4use ActiveRecord as AR;
5
6class UtilsTest extends SnakeCase_PHPUnit_Framework_TestCase
7{
8 public function set_up()
9 {
10 $this->object_array = array(null,null);
11 $this->object_array[0] = new stdClass();
12 $this->object_array[0]->a = "0a";
13 $this->object_array[0]->b = "0b";
14 $this->object_array[1] = new stdClass();
15 $this->object_array[1]->a = "1a";
16 $this->object_array[1]->b = "1b";
17
18 $this->array_hash = array(
19 array("a" => "0a", "b" => "0b"),
20 array("a" => "1a", "b" => "1b"));
21 }
22
23 public function test_collect_with_array_of_objects_using_closure()
24 {
25 $this->assert_equals(array("0a","1a"),AR\collect($this->object_array,function($obj) { return $obj->a; }));
26 }
27
28 public function test_collect_with_array_of_objects_using_string()
29 {
30 $this->assert_equals(array("0a","1a"),AR\collect($this->object_array,"a"));
31 }
32
33 public function test_collect_with_array_hash_using_closure()
34 {
35 $this->assert_equals(array("0a","1a"),AR\collect($this->array_hash,function($item) { return $item["a"]; }));
36 }
37
38 public function test_collect_with_array_hash_using_string()
39 {
40 $this->assert_equals(array("0a","1a"),AR\collect($this->array_hash,"a"));
41 }
42
43 public function test_array_flatten()
44 {
45 $this->assert_equals(array(), AR\array_flatten(array()));
46 $this->assert_equals(array(1), AR\array_flatten(array(1)));
47 $this->assert_equals(array(1), AR\array_flatten(array(array(1))));
48 $this->assert_equals(array(1, 2), AR\array_flatten(array(array(1, 2))));
49 $this->assert_equals(array(1, 2), AR\array_flatten(array(array(1), 2)));
50 $this->assert_equals(array(1, 2), AR\array_flatten(array(1, array(2))));
51 $this->assert_equals(array(1, 2, 3), AR\array_flatten(array(1, array(2), 3)));
52 $this->assert_equals(array(1, 2, 3, 4), AR\array_flatten(array(1, array(2, 3), 4)));
53 $this->assert_equals(array(1, 2, 3, 4, 5, 6), AR\array_flatten(array(1, array(2, 3), 4, array(5, 6))));
54 }
55
56 public function test_all()
57 {
58 $this->assert_true(AR\all(null,array(null,null)));
59 $this->assert_true(AR\all(1,array(1,1)));
60 $this->assert_false(AR\all(1,array(1,'1')));
61 $this->assert_false(AR\all(null,array('',null)));
62 }
63
64 public function test_classify()
65 {
66 $bad_class_names = array('ubuntu_rox', 'stop_the_Snake_Case', 'CamelCased', 'camelCased');
67 $good_class_names = array('UbuntuRox', 'StopTheSnakeCase', 'CamelCased', 'CamelCased');
68
69 $class_names = array();
70 foreach ($bad_class_names as $s)
71 $class_names[] = AR\classify($s);
72
73 $this->assert_equals($class_names, $good_class_names);
74 }
75
76 public function test_classify_singularize()
77 {
78 $bad_class_names = array('events', 'stop_the_Snake_Cases', 'angry_boxes', 'Mad_Sheep_herders', 'happy_People');
79 $good_class_names = array('Event', 'StopTheSnakeCase', 'AngryBox', 'MadSheepHerder', 'HappyPerson');
80
81 $class_names = array();
82 foreach ($bad_class_names as $s)
83 $class_names[] = AR\classify($s, true);
84
85 $this->assert_equals($class_names, $good_class_names);
86 }
87
88 public function test_singularize()
89 {
90 $this->assert_equals('order_status',AR\Utils::singularize('order_status'));
91 $this->assert_equals('order_status',AR\Utils::singularize('order_statuses'));
92 $this->assert_equals('os_type', AR\Utils::singularize('os_type'));
93 $this->assert_equals('os_type', AR\Utils::singularize('os_types'));
94 $this->assert_equals('photo', AR\Utils::singularize('photos'));
95 $this->assert_equals('pass', AR\Utils::singularize('pass'));
96 $this->assert_equals('pass', AR\Utils::singularize('passes'));
97 }
98
99 public function test_wrap_strings_in_arrays()
100 {
101 $x = array('1',array('2'));
102 $this->assert_equals(array(array('1'),array('2')),ActiveRecord\wrap_strings_in_arrays($x));
103
104 $x = '1';
105 $this->assert_equals(array(array('1')),ActiveRecord\wrap_strings_in_arrays($x));
106 }
107};
108?>