UtilsTest.php 3.7 KB

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