test_collection.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. require_once 'collection.php';
  3. use webbasics\Collection;
  4. class IdObject {
  5. static $count = 0;
  6. function __construct($foo=null) {
  7. $this->id = ++self::$count;
  8. $this->foo = $foo;
  9. }
  10. function getId() {
  11. return $this->id;
  12. }
  13. static function clearCounter() {
  14. self::$count = 0;
  15. }
  16. }
  17. function set($items=array()) {
  18. return new Collection($items);
  19. }
  20. function std_object(array $properties) {
  21. $object = new stdClass();
  22. foreach( $properties as $property => $value )
  23. $object->{$property} = $value;
  24. return $object;
  25. }
  26. class CollectionTest extends PHPUnit_Framework_TestCase {
  27. function setUp() {
  28. $this->set = set(array(1, 2));
  29. }
  30. function testAdd() {
  31. $this->set->add(3);
  32. $this->assertEquals($this->set, set(array(1, 2, 3)));
  33. }
  34. /**
  35. * @expectedException InvalidArgumentException
  36. */
  37. function testInsertError() {
  38. set(array('foo' => 1))->insert(2, 'foo');
  39. }
  40. function testInsertSuccess() {
  41. $this->set->insert(4, 1);
  42. $this->assertEquals($this->set, set(array(1, 4, 2)));
  43. $this->set->insert(5, 0);
  44. $this->assertEquals($this->set, set(array(5, 1, 4, 2)));
  45. }
  46. function testAll() {
  47. $this->assertEquals(set()->all(), array());
  48. $this->assertEquals(set(array())->all(), array());
  49. $this->assertEquals(set(array(1))->all(), array(1));
  50. $this->assertEquals(set(array(1, 2))->all(), array(1, 2));
  51. }
  52. /**
  53. * @expectedException OutOfBoundsException
  54. */
  55. function testLastEmpty() {
  56. set()->last();
  57. }
  58. function testLast() {
  59. $this->assertEquals($this->set->last(), 2);
  60. }
  61. /**
  62. * @expectedException OutOfBoundsException
  63. */
  64. function testFirstEmpty() {
  65. set()->first();
  66. }
  67. function testFirst() {
  68. $this->assertEquals($this->set->first(), 1);
  69. }
  70. function testCount() {
  71. $this->assertEquals(set()->count(), 0);
  72. $this->assertEquals(set(array())->count(), 0);
  73. $this->assertEquals(set(array(1))->count(), 1);
  74. $this->assertEquals(set(array(1, 2))->count(), 2);
  75. }
  76. function testIndexExists() {
  77. $this->assertTrue($this->set->indexExists(1));
  78. $this->assertTrue(set(array('foo' => 'bar'))->indexExists('foo'));
  79. }
  80. function testGet() {
  81. $this->assertEquals($this->set->get(0), 1);
  82. $this->assertEquals($this->set->get(1), 2);
  83. $this->assertEquals(set(array('foo' => 'bar'))->get('foo'), 'bar');
  84. }
  85. function testDeleteIndex() {
  86. $this->set->deleteIndex(0);
  87. $this->assertEquals($this->set, set(array(1 => 2)));
  88. }
  89. function testDelete() {
  90. $this->set->delete(1);
  91. $this->assertEquals($this->set, set(array(1 => 2)));
  92. }
  93. function assertSetEquals(array $expected_items, $set) {
  94. $this->assertAttributeEquals($expected_items, 'items', $set);
  95. }
  96. function testUniques() {
  97. $this->assertSetEquals(array(1, 2), set(array(1, 2, 2))->uniques());
  98. $this->assertSetEquals(array(2, 1), set(array(2, 1, 2))->uniques());
  99. $this->assertSetEquals(array(2, 1), set(array(2, 2, 1))->uniques());
  100. }
  101. function setItems($collection, $items, $clone) {
  102. $rm = new ReflectionMethod($collection, 'setItems');
  103. $rm->setAccessible(true);
  104. return $rm->invoke($collection, $items, $clone);
  105. }
  106. function testSetItemsClone() {
  107. $result = $this->setItems($this->set, array(3, 4), true);
  108. $this->assertSetEquals(array(1, 2), $this->set);
  109. $this->assertSetEquals(array(3, 4), $result);
  110. $this->assertNotSame($this->set, $result);
  111. }
  112. function testSetItemsNoClone() {
  113. $result = $this->setItems($this->set, array(3, 4), false);
  114. $this->assertSame($this->set, $result);
  115. }
  116. /**
  117. * @depends testSetItemsClone
  118. */
  119. function testFilter() {
  120. $smaller_than_five = function($number) { return $number < 5; };
  121. $this->assertSetEquals(array(2, 4, 1, 4), set(array(2, 7, 4, 7, 1, 8, 4, 5))->filter($smaller_than_five));
  122. }
  123. /**
  124. * @depends testFilter
  125. */
  126. function testFindSuccess() {
  127. $items = array(
  128. array('foo' => 'bar', 'bar' => 'baz'),
  129. array('foo' => 'baz', 'bar' => 'foo'),
  130. std_object(array('foo' => 'bar', 'baz' => 'bar')),
  131. );
  132. $this->assertSetEquals(array($items[1]), set($items)->find(array('foo' => 'baz')));
  133. $this->assertSetEquals(array($items[0], $items[2]), set($items)->find(array('foo' => 'bar')));
  134. }
  135. /**
  136. * @depends testFindSuccess
  137. * @expectedException \UnexpectedValueException
  138. * @expectedExceptionMessage Collection::find encountered a non-object and non-array item "foobar".
  139. */
  140. function testFindFailure() {
  141. $items = array(
  142. array('foo' => 'bar', 'bar' => 'baz'),
  143. 'foobar',
  144. );
  145. set($items)->find(array('foo' => 'bar'));
  146. }
  147. function testGetAttributeSimple() {
  148. IdObject::clearCounter();
  149. $set = set(array(new IdObject(), new IdObject(), new IdObject()));
  150. $this->assertEquals(array(1, 2, 3), $set->getAttribute('id'));
  151. }
  152. /**
  153. * @depends testGetAttributeSimple
  154. */
  155. function testGetAttributeIndices() {
  156. IdObject::clearCounter();
  157. $set = set(array('foo' => new IdObject(), 'bar' => new IdObject(), 'baz' => new IdObject()));
  158. $this->assertEquals(array('foo' => 1, 'bar' => 2, 'baz' => 3), $set->getAttribute('id'));
  159. }
  160. /**
  161. * @depends testAll
  162. * @depends testSetItemsClone
  163. */
  164. function testIndexBy() {
  165. IdObject::clearCounter();
  166. $set = set(array(new IdObject('foo'), new IdObject('bar'), new IdObject('baz')));
  167. list($foo, $bar, $baz) = $set->all();
  168. $this->assertSetEquals(array('foo' => $foo, 'bar' => $bar, 'baz' => $baz), $set->indexBy('foo'));
  169. }
  170. /**
  171. * @depends testSetItemsClone
  172. */
  173. function testMap() {
  174. $plus_five = function($number) { return $number + 5; };
  175. $this->assertSetEquals(array(6, 7, 8), set(array(1, 2, 3))->map($plus_five));
  176. }
  177. /**
  178. * @depends testSetItemsClone
  179. */
  180. function testMapMethod() {
  181. IdObject::clearCounter();
  182. $set = set(array(new IdObject(), new IdObject(), new IdObject()));
  183. $this->assertSetEquals(array(1, 2, 3), $set->mapMethod('getId'));
  184. }
  185. }
  186. ?>