CollectionTest.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace Doctrine\Tests\Common\Collections;
  3. use Doctrine\Tests;
  4. class CollectionTest extends \Doctrine\Tests\DoctrineTestCase
  5. {
  6. /**
  7. * @var \Doctrine\Common\Collections\Collection
  8. */
  9. private $_coll;
  10. protected function setUp()
  11. {
  12. $this->_coll = new \Doctrine\Common\Collections\ArrayCollection;
  13. }
  14. public function testIssetAndUnset()
  15. {
  16. $this->assertFalse(isset($this->_coll[0]));
  17. $this->_coll->add('testing');
  18. $this->assertTrue(isset($this->_coll[0]));
  19. unset($this->_coll[0]);
  20. $this->assertFalse(isset($this->_coll[0]));
  21. }
  22. public function testToString()
  23. {
  24. $this->_coll->add('testing');
  25. $this->assertTrue(is_string((string) $this->_coll));
  26. }
  27. public function testRemovingNonExistentEntryReturnsNull()
  28. {
  29. $this->assertEquals(null, $this->_coll->remove('testing_does_not_exist'));
  30. }
  31. public function testExists()
  32. {
  33. $this->_coll->add("one");
  34. $this->_coll->add("two");
  35. $exists = $this->_coll->exists(function($k, $e) { return $e == "one"; });
  36. $this->assertTrue($exists);
  37. $exists = $this->_coll->exists(function($k, $e) { return $e == "other"; });
  38. $this->assertFalse($exists);
  39. }
  40. public function testMap()
  41. {
  42. $this->_coll->add(1);
  43. $this->_coll->add(2);
  44. $res = $this->_coll->map(function($e) { return $e * 2; });
  45. $this->assertEquals(array(2, 4), $res->toArray());
  46. }
  47. public function testFilter()
  48. {
  49. $this->_coll->add(1);
  50. $this->_coll->add("foo");
  51. $this->_coll->add(3);
  52. $res = $this->_coll->filter(function($e) { return is_numeric($e); });
  53. $this->assertEquals(array(0 => 1, 2 => 3), $res->toArray());
  54. }
  55. public function testFirstAndLast()
  56. {
  57. $this->_coll->add('one');
  58. $this->_coll->add('two');
  59. $this->assertEquals($this->_coll->first(), 'one');
  60. $this->assertEquals($this->_coll->last(), 'two');
  61. }
  62. public function testArrayAccess()
  63. {
  64. $this->_coll[] = 'one';
  65. $this->_coll[] = 'two';
  66. $this->assertEquals($this->_coll[0], 'one');
  67. $this->assertEquals($this->_coll[1], 'two');
  68. unset($this->_coll[0]);
  69. $this->assertEquals($this->_coll->count(), 1);
  70. }
  71. public function testContainsKey()
  72. {
  73. $this->_coll[5] = 'five';
  74. $this->assertTrue($this->_coll->containsKey(5));
  75. }
  76. public function testContains()
  77. {
  78. $this->_coll[0] = 'test';
  79. $this->assertTrue($this->_coll->contains('test'));
  80. }
  81. public function testSearch()
  82. {
  83. $this->_coll[0] = 'test';
  84. $this->assertEquals(0, $this->_coll->indexOf('test'));
  85. }
  86. public function testGet()
  87. {
  88. $this->_coll[0] = 'test';
  89. $this->assertEquals('test', $this->_coll->get(0));
  90. }
  91. public function testGetKeys()
  92. {
  93. $this->_coll[] = 'one';
  94. $this->_coll[] = 'two';
  95. $this->assertEquals(array(0, 1), $this->_coll->getKeys());
  96. }
  97. public function testGetValues()
  98. {
  99. $this->_coll[] = 'one';
  100. $this->_coll[] = 'two';
  101. $this->assertEquals(array('one', 'two'), $this->_coll->getValues());
  102. }
  103. public function testCount()
  104. {
  105. $this->_coll[] = 'one';
  106. $this->_coll[] = 'two';
  107. $this->assertEquals($this->_coll->count(), 2);
  108. $this->assertEquals(count($this->_coll), 2);
  109. }
  110. public function testForAll()
  111. {
  112. $this->_coll[] = 'one';
  113. $this->_coll[] = 'two';
  114. $this->assertEquals($this->_coll->forAll(function($k, $e) { return is_string($e); }), true);
  115. $this->assertEquals($this->_coll->forAll(function($k, $e) { return is_array($e); }), false);
  116. }
  117. public function testPartition()
  118. {
  119. $this->_coll[] = true;
  120. $this->_coll[] = false;
  121. $partition = $this->_coll->partition(function($k, $e) { return $e == true; });
  122. $this->assertEquals($partition[0][0], true);
  123. $this->assertEquals($partition[1][0], false);
  124. }
  125. public function testClear()
  126. {
  127. $this->_coll[] = 'one';
  128. $this->_coll[] = 'two';
  129. $this->_coll->clear();
  130. $this->assertEquals($this->_coll->isEmpty(), true);
  131. }
  132. public function testRemove()
  133. {
  134. $this->_coll[] = 'one';
  135. $this->_coll[] = 'two';
  136. $el = $this->_coll->remove(0);
  137. $this->assertEquals('one', $el);
  138. $this->assertEquals($this->_coll->contains('one'), false);
  139. $this->assertNull($this->_coll->remove(0));
  140. }
  141. public function testRemoveElement()
  142. {
  143. $this->_coll[] = 'one';
  144. $this->_coll[] = 'two';
  145. $this->assertTrue($this->_coll->removeElement('two'));
  146. $this->assertFalse($this->_coll->contains('two'));
  147. $this->assertFalse($this->_coll->removeElement('two'));
  148. }
  149. public function testSlice()
  150. {
  151. $this->_coll[] = 'one';
  152. $this->_coll[] = 'two';
  153. $this->_coll[] = 'three';
  154. $slice = $this->_coll->slice(0, 1);
  155. $this->assertInternalType('array', $slice);
  156. $this->assertEquals(array('one'), $slice);
  157. $slice = $this->_coll->slice(1);
  158. $this->assertEquals(array(1 => 'two', 2 => 'three'), $slice);
  159. $slice = $this->_coll->slice(1, 1);
  160. $this->assertEquals(array(1 => 'two'), $slice);
  161. }
  162. }