ElggPriorityListTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. class ElggPriorityListTest extends \PHPUnit_Framework_TestCase {
  3. public function testAdd() {
  4. $pl = new \ElggPriorityList();
  5. $elements = array(
  6. 'Test value',
  7. 'Test value 2',
  8. 'Test value 3'
  9. );
  10. shuffle($elements);
  11. foreach ($elements as $element) {
  12. $this->assertTrue($pl->add($element) !== false);
  13. }
  14. $test_elements = $pl->getElements();
  15. $this->assertTrue(is_array($test_elements));
  16. foreach ($test_elements as $i => $element) {
  17. // should be in the array
  18. $this->assertTrue(in_array($element, $elements));
  19. // should be the only element, so priority 0
  20. $this->assertEquals($i, array_search($element, $elements));
  21. }
  22. }
  23. public function testAddWithPriority() {
  24. $pl = new \ElggPriorityList();
  25. $elements = array(
  26. 10 => 'Test Element 10',
  27. 5 => 'Test Element 5',
  28. 0 => 'Test Element 0',
  29. 100 => 'Test Element 100',
  30. -1 => 'Test Element -1',
  31. -5 => 'Test Element -5',
  32. );
  33. foreach ($elements as $priority => $element) {
  34. $pl->add($element, $priority);
  35. }
  36. $test_elements = $pl->getElements();
  37. // should be sorted by priority
  38. $elements_sorted = array(
  39. -5 => 'Test Element -5',
  40. -1 => 'Test Element -1',
  41. 0 => 'Test Element 0',
  42. 5 => 'Test Element 5',
  43. 10 => 'Test Element 10',
  44. 100 => 'Test Element 100',
  45. );
  46. $this->assertSame($elements_sorted, $test_elements);
  47. foreach ($test_elements as $priority => $element) {
  48. $this->assertSame($elements[$priority], $element);
  49. }
  50. }
  51. public function testGetNextPriority() {
  52. $pl = new \ElggPriorityList();
  53. $elements = array(
  54. 2 => 'Test Element',
  55. 0 => 'Test Element 2',
  56. -2 => 'Test Element 3',
  57. );
  58. foreach ($elements as $priority => $element) {
  59. $pl->add($element, $priority);
  60. }
  61. // we're not specifying a priority so it should be the next consecutive to 0.
  62. $this->assertEquals(1, $pl->getNextPriority());
  63. // add another one at priority 1
  64. $pl->add('Test Element 1');
  65. // next consecutive to 0 is now 3.
  66. $this->assertEquals(3, $pl->getNextPriority());
  67. }
  68. public function testRemove() {
  69. $pl = new \ElggPriorityList();
  70. $elements = array();
  71. for ($i=0; $i<3; $i++) {
  72. $element = new \stdClass();
  73. $element->name = "Test Element $i";
  74. $element->someAttribute = rand(0, 9999);
  75. $elements[] = $element;
  76. $pl->add($element);
  77. }
  78. $pl->remove($elements[1]);
  79. $test_elements = $pl->getElements();
  80. // make sure it's gone.
  81. $this->assertEquals(2, count($test_elements));
  82. $this->assertSame($elements[0], $test_elements[0]);
  83. $this->assertSame($elements[2], $test_elements[2]);
  84. }
  85. public function testMove() {
  86. $pl = new \ElggPriorityList();
  87. $elements = array(
  88. -5 => 'Test Element -5',
  89. 0 => 'Test Element 0',
  90. 5 => 'Test Element 5',
  91. );
  92. foreach ($elements as $priority => $element) {
  93. $pl->add($element, $priority);
  94. }
  95. $this->assertEquals($pl->move($elements[-5], 10), 10);
  96. // check it's at the new place
  97. $this->assertSame($elements[-5], $pl->getElement(10));
  98. // check it's not at the old
  99. $this->assertFalse($pl->getElement(-5));
  100. }
  101. public function testConstructor() {
  102. $elements = array(
  103. 10 => 'Test Element 10',
  104. 5 => 'Test Element 5',
  105. 0 => 'Test Element 0',
  106. 100 => 'Test Element 100',
  107. -1 => 'Test Element -1',
  108. -5 => 'Test Element -5'
  109. );
  110. $pl = new \ElggPriorityList($elements);
  111. $test_elements = $pl->getElements();
  112. $elements_sorted = array(
  113. -5 => 'Test Element -5',
  114. -1 => 'Test Element -1',
  115. 0 => 'Test Element 0',
  116. 5 => 'Test Element 5',
  117. 10 => 'Test Element 10',
  118. 100 => 'Test Element 100',
  119. );
  120. $this->assertSame($elements_sorted, $test_elements);
  121. }
  122. public function testGetPriority() {
  123. $pl = new \ElggPriorityList();
  124. $elements = array(
  125. 'Test element 0',
  126. 'Test element 1',
  127. 'Test element 2',
  128. );
  129. foreach ($elements as $element) {
  130. $pl->add($element);
  131. }
  132. $this->assertSame(0, $pl->getPriority($elements[0]));
  133. $this->assertSame(1, $pl->getPriority($elements[1]));
  134. $this->assertSame(2, $pl->getPriority($elements[2]));
  135. }
  136. public function testGetElement() {
  137. $pl = new \ElggPriorityList();
  138. $priorities = array();
  139. $elements = array(
  140. 'Test element 0',
  141. 'Test element 1',
  142. 'Test element 2',
  143. );
  144. foreach ($elements as $element) {
  145. $priorities[] = $pl->add($element);
  146. }
  147. $this->assertSame($elements[0], $pl->getElement($priorities[0]));
  148. $this->assertSame($elements[1], $pl->getElement($priorities[1]));
  149. $this->assertSame($elements[2], $pl->getElement($priorities[2]));
  150. }
  151. public function testPriorityCollision() {
  152. $pl = new \ElggPriorityList();
  153. $elements = array(
  154. 5 => 'Test element 5',
  155. 6 => 'Test element 6',
  156. 0 => 'Test element 0',
  157. );
  158. foreach ($elements as $priority => $element) {
  159. $pl->add($element, $priority);
  160. }
  161. // add at a colliding priority
  162. $pl->add('Colliding element', 5);
  163. // should float to the top closest to 5, so 7
  164. $this->assertEquals(7, $pl->getPriority('Colliding element'));
  165. }
  166. public function testIterator() {
  167. $elements = array(
  168. -5 => 'Test element -5',
  169. 0 => 'Test element 0',
  170. 5 => 'Test element 5',
  171. );
  172. $pl = new \ElggPriorityList($elements);
  173. foreach ($pl as $priority => $element) {
  174. $this->assertSame($elements[$priority], $element);
  175. }
  176. }
  177. public function testCountable() {
  178. $pl = new \ElggPriorityList();
  179. $this->assertEquals(0, count($pl));
  180. $pl->add('Test element 0');
  181. $this->assertEquals(1, count($pl));
  182. $pl->add('Test element 1');
  183. $this->assertEquals(2, count($pl));
  184. $pl->add('Test element 2');
  185. $this->assertEquals(3, count($pl));
  186. }
  187. public function testUserSort() {
  188. $elements = array(
  189. 'A',
  190. 'B',
  191. 'C',
  192. 'D',
  193. 'E',
  194. );
  195. $elements_sorted_string = $elements;
  196. shuffle($elements);
  197. $pl = new \ElggPriorityList($elements);
  198. // will sort by priority
  199. $test_elements = $pl->getElements();
  200. $this->assertSame($elements, $test_elements);
  201. function test_sort($elements) {
  202. sort($elements, SORT_LOCALE_STRING);
  203. return $elements;
  204. }
  205. // force a new sort using our function
  206. $pl->sort('test_sort');
  207. $test_elements = $pl->getElements();
  208. $this->assertSame($elements_sorted_string, $test_elements);
  209. }
  210. }