CollectionTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace Doctrine\Tests\Common\Collections;
  3. use Doctrine\Tests;
  4. use Doctrine\Common\Collections\Criteria;
  5. class CollectionTest extends \Doctrine\Tests\DoctrineTestCase
  6. {
  7. /**
  8. * @var \Doctrine\Common\Collections\Collection
  9. */
  10. private $_coll;
  11. protected function setUp()
  12. {
  13. $this->_coll = new \Doctrine\Common\Collections\ArrayCollection;
  14. }
  15. public function testIssetAndUnset()
  16. {
  17. $this->assertFalse(isset($this->_coll[0]));
  18. $this->_coll->add('testing');
  19. $this->assertTrue(isset($this->_coll[0]));
  20. unset($this->_coll[0]);
  21. $this->assertFalse(isset($this->_coll[0]));
  22. }
  23. public function testToString()
  24. {
  25. $this->_coll->add('testing');
  26. $this->assertTrue(is_string((string) $this->_coll));
  27. }
  28. public function testRemovingNonExistentEntryReturnsNull()
  29. {
  30. $this->assertEquals(null, $this->_coll->remove('testing_does_not_exist'));
  31. }
  32. public function testExists()
  33. {
  34. $this->_coll->add("one");
  35. $this->_coll->add("two");
  36. $exists = $this->_coll->exists(function($k, $e) { return $e == "one"; });
  37. $this->assertTrue($exists);
  38. $exists = $this->_coll->exists(function($k, $e) { return $e == "other"; });
  39. $this->assertFalse($exists);
  40. }
  41. public function testMap()
  42. {
  43. $this->_coll->add(1);
  44. $this->_coll->add(2);
  45. $res = $this->_coll->map(function($e) { return $e * 2; });
  46. $this->assertEquals(array(2, 4), $res->toArray());
  47. }
  48. public function testFilter()
  49. {
  50. $this->_coll->add(1);
  51. $this->_coll->add("foo");
  52. $this->_coll->add(3);
  53. $res = $this->_coll->filter(function($e) { return is_numeric($e); });
  54. $this->assertEquals(array(0 => 1, 2 => 3), $res->toArray());
  55. }
  56. public function testFirstAndLast()
  57. {
  58. $this->_coll->add('one');
  59. $this->_coll->add('two');
  60. $this->assertEquals($this->_coll->first(), 'one');
  61. $this->assertEquals($this->_coll->last(), 'two');
  62. }
  63. public function testArrayAccess()
  64. {
  65. $this->_coll[] = 'one';
  66. $this->_coll[] = 'two';
  67. $this->assertEquals($this->_coll[0], 'one');
  68. $this->assertEquals($this->_coll[1], 'two');
  69. unset($this->_coll[0]);
  70. $this->assertEquals($this->_coll->count(), 1);
  71. }
  72. public function testContainsKey()
  73. {
  74. $this->_coll[5] = 'five';
  75. $this->assertTrue($this->_coll->containsKey(5));
  76. }
  77. public function testContains()
  78. {
  79. $this->_coll[0] = 'test';
  80. $this->assertTrue($this->_coll->contains('test'));
  81. }
  82. public function testSearch()
  83. {
  84. $this->_coll[0] = 'test';
  85. $this->assertEquals(0, $this->_coll->indexOf('test'));
  86. }
  87. public function testGet()
  88. {
  89. $this->_coll[0] = 'test';
  90. $this->assertEquals('test', $this->_coll->get(0));
  91. }
  92. public function testGetKeys()
  93. {
  94. $this->_coll[] = 'one';
  95. $this->_coll[] = 'two';
  96. $this->assertEquals(array(0, 1), $this->_coll->getKeys());
  97. }
  98. public function testGetValues()
  99. {
  100. $this->_coll[] = 'one';
  101. $this->_coll[] = 'two';
  102. $this->assertEquals(array('one', 'two'), $this->_coll->getValues());
  103. }
  104. public function testCount()
  105. {
  106. $this->_coll[] = 'one';
  107. $this->_coll[] = 'two';
  108. $this->assertEquals($this->_coll->count(), 2);
  109. $this->assertEquals(count($this->_coll), 2);
  110. }
  111. public function testForAll()
  112. {
  113. $this->_coll[] = 'one';
  114. $this->_coll[] = 'two';
  115. $this->assertEquals($this->_coll->forAll(function($k, $e) { return is_string($e); }), true);
  116. $this->assertEquals($this->_coll->forAll(function($k, $e) { return is_array($e); }), false);
  117. }
  118. public function testPartition()
  119. {
  120. $this->_coll[] = true;
  121. $this->_coll[] = false;
  122. $partition = $this->_coll->partition(function($k, $e) { return $e == true; });
  123. $this->assertEquals($partition[0][0], true);
  124. $this->assertEquals($partition[1][0], false);
  125. }
  126. public function testClear()
  127. {
  128. $this->_coll[] = 'one';
  129. $this->_coll[] = 'two';
  130. $this->_coll->clear();
  131. $this->assertEquals($this->_coll->isEmpty(), true);
  132. }
  133. public function testRemove()
  134. {
  135. $this->_coll[] = 'one';
  136. $this->_coll[] = 'two';
  137. $el = $this->_coll->remove(0);
  138. $this->assertEquals('one', $el);
  139. $this->assertEquals($this->_coll->contains('one'), false);
  140. $this->assertNull($this->_coll->remove(0));
  141. }
  142. public function testRemoveElement()
  143. {
  144. $this->_coll[] = 'one';
  145. $this->_coll[] = 'two';
  146. $this->assertTrue($this->_coll->removeElement('two'));
  147. $this->assertFalse($this->_coll->contains('two'));
  148. $this->assertFalse($this->_coll->removeElement('two'));
  149. }
  150. public function testSlice()
  151. {
  152. $this->_coll[] = 'one';
  153. $this->_coll[] = 'two';
  154. $this->_coll[] = 'three';
  155. $slice = $this->_coll->slice(0, 1);
  156. $this->assertInternalType('array', $slice);
  157. $this->assertEquals(array('one'), $slice);
  158. $slice = $this->_coll->slice(1);
  159. $this->assertEquals(array(1 => 'two', 2 => 'three'), $slice);
  160. $slice = $this->_coll->slice(1, 1);
  161. $this->assertEquals(array(1 => 'two'), $slice);
  162. }
  163. public function fillMatchingFixture()
  164. {
  165. $std1 = new \stdClass();
  166. $std1->foo = "bar";
  167. $this->_coll[] = $std1;
  168. $std2 = new \stdClass();
  169. $std2->foo = "baz";
  170. $this->_coll[] = $std2;
  171. }
  172. /**
  173. * @group DDC-1637
  174. */
  175. public function testMatching()
  176. {
  177. $this->fillMatchingFixture();
  178. $col = $this->_coll->matching(new Criteria(Criteria::expr()->eq("foo", "bar")));
  179. $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $col);
  180. $this->assertNotSame($col, $this->_coll);
  181. $this->assertEquals(1, count($col));
  182. }
  183. /**
  184. * @group DDC-1637
  185. */
  186. public function testMatchingOrdering()
  187. {
  188. $this->fillMatchingFixture();
  189. $col = $this->_coll->matching(new Criteria(null, array('foo' => 'DESC')));
  190. $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $col);
  191. $this->assertNotSame($col, $this->_coll);
  192. $this->assertEquals(2, count($col));
  193. $this->assertEquals('baz', $col[0]->foo);
  194. $this->assertEquals('bar', $col[1]->foo);
  195. }
  196. /**
  197. * @group DDC-1637
  198. */
  199. public function testMatchingSlice()
  200. {
  201. $this->fillMatchingFixture();
  202. $col = $this->_coll->matching(new Criteria(null, null, 1, 1));
  203. $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $col);
  204. $this->assertNotSame($col, $this->_coll);
  205. $this->assertEquals(1, count($col));
  206. $this->assertEquals('baz', $col[0]->foo);
  207. }
  208. public function testCanRemoveNullValuesByKey()
  209. {
  210. $this->_coll->add(null);
  211. $this->_coll->remove(0);
  212. $this->assertTrue($this->_coll->isEmpty());
  213. }
  214. public function testCanVerifyExistingKeysWithNullValues()
  215. {
  216. $this->_coll->set('key', null);
  217. $this->assertTrue($this->_coll->containsKey('key'));
  218. }
  219. }