SortableTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. namespace Gedmo\Sortable;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Sortable\Fixture\Node;
  6. use Sortable\Fixture\Item;
  7. use Sortable\Fixture\Category;
  8. /**
  9. * These are tests for sluggable behavior
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @package Gedmo.Sluggable
  13. * @link http://www.gediminasm.org
  14. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. class SortableTest extends BaseTestCaseORM
  17. {
  18. const NODE = 'Sortable\\Fixture\\Node';
  19. const ITEM = 'Sortable\\Fixture\\Item';
  20. const CATEGORY = 'Sortable\\Fixture\\Category';
  21. private $nodeId;
  22. protected function setUp()
  23. {
  24. parent::setUp();
  25. $annotationReader = new \Doctrine\Common\Annotations\AnnotationReader();
  26. $annotationReader->setAutoloadAnnotations(true);
  27. $sortable = new SortableListener;
  28. $sortable->setAnnotationReader($annotationReader);
  29. $evm = new EventManager;
  30. $evm->addEventSubscriber($sortable);
  31. $this->getMockSqliteEntityManager($evm);
  32. //$this->startQueryLog();
  33. $this->populate();
  34. }
  35. protected function tearDown()
  36. {
  37. //$this->stopQueryLog();
  38. }
  39. public function testInsertedNewNode()
  40. {
  41. $node = $this->em->find(self::NODE, $this->nodeId);
  42. //$this->assertTrue($node instanceof Sortable);
  43. $this->assertEquals(0, $node->getPosition());
  44. }
  45. public function testInsertSortedList()
  46. {
  47. for ($i = 2; $i <= 10; $i++) {
  48. $node = new Node();
  49. $node->setName("Node".$i);
  50. $node->setPath("/");
  51. $this->em->persist($node);
  52. }
  53. $this->em->flush();
  54. $this->em->clear();
  55. $nodes = $this->em->createQuery("SELECT node FROM Sortable\Fixture\Node node "
  56. ."WHERE node.path = :path ORDER BY node.position")
  57. ->setParameter('path', '/')
  58. ->getResult();
  59. $this->assertEquals(10, count($nodes));
  60. $this->assertEquals('Node1', $nodes[0]->getName());
  61. }
  62. public function testShiftForward()
  63. {
  64. $node2 = new Node();
  65. $node2->setName("Node2");
  66. $node2->setPath("/");
  67. $this->em->persist($node2);
  68. $node = new Node();
  69. $node->setName("Node3");
  70. $node->setPath("/");
  71. $this->em->persist($node);
  72. $node = new Node();
  73. $node->setName("Node4");
  74. $node->setPath("/");
  75. $this->em->persist($node);
  76. $node = new Node();
  77. $node->setName("Node5");
  78. $node->setPath("/");
  79. $this->em->persist($node);
  80. $this->em->flush();
  81. $this->assertEquals(1, $node2->getPosition());
  82. $node2->setPosition(3);
  83. $this->em->persist($node2);
  84. $this->em->flush();
  85. $this->em->clear();
  86. $repo = $this->em->getRepository('Sortable\\Fixture\\Node');
  87. $nodes = $repo->getBySortableGroups(array('path' => '/'));
  88. $this->assertEquals('Node1', $nodes[0]->getName());
  89. $this->assertEquals('Node3', $nodes[1]->getName());
  90. $this->assertEquals('Node4', $nodes[2]->getName());
  91. $this->assertEquals('Node2', $nodes[3]->getName());
  92. $this->assertEquals('Node5', $nodes[4]->getName());
  93. }
  94. public function testShiftBackward()
  95. {
  96. $node = new Node();
  97. $node->setName("Node2");
  98. $node->setPath("/");
  99. $this->em->persist($node);
  100. $node = new Node();
  101. $node->setName("Node3");
  102. $node->setPath("/");
  103. $this->em->persist($node);
  104. $node2 = new Node();
  105. $node2->setName("Node4");
  106. $node2->setPath("/");
  107. $this->em->persist($node2);
  108. $node = new Node();
  109. $node->setName("Node5");
  110. $node->setPath("/");
  111. $this->em->persist($node);
  112. $this->em->flush();
  113. $this->assertEquals(3, $node2->getPosition());
  114. $node2->setPosition(1);
  115. $this->em->persist($node2);
  116. $this->em->flush();
  117. $this->em->clear();
  118. $repo = $this->em->getRepository('Sortable\\Fixture\\Node');
  119. $nodes = $repo->getBySortableGroups(array('path' => '/'));
  120. $this->assertEquals('Node1', $nodes[0]->getName());
  121. $this->assertEquals('Node4', $nodes[1]->getName());
  122. $this->assertEquals('Node2', $nodes[2]->getName());
  123. $this->assertEquals('Node3', $nodes[3]->getName());
  124. $this->assertEquals('Node5', $nodes[4]->getName());
  125. }
  126. public function testDelete()
  127. {
  128. $node2 = new Node();
  129. $node2->setName("Node2");
  130. $node2->setPath("/");
  131. $this->em->persist($node2);
  132. $node3 = new Node();
  133. $node3->setName("Node3");
  134. $node3->setPath("/");
  135. $this->em->persist($node3);
  136. $this->em->flush();
  137. $this->em->remove($node2);
  138. $this->em->flush();
  139. $this->em->clear();
  140. $repo = $this->em->getRepository('Sortable\\Fixture\\Node');
  141. $nodes = $repo->getBySortableGroups(array('path' => '/'));
  142. $this->assertEquals('Node1', $nodes[0]->getName());
  143. $this->assertEquals('Node3', $nodes[1]->getName());
  144. $this->assertEquals(0, $nodes[0]->getPosition());
  145. $this->assertEquals(1, $nodes[1]->getPosition());
  146. }
  147. public function testGroupByAssociation()
  148. {
  149. $category1 = new Category();
  150. $category1->setName("Category1");
  151. $this->em->persist($category1);
  152. $category2 = new Category();
  153. $category2->setName("Category2");
  154. $this->em->persist($category2);
  155. $this->em->flush();
  156. $item3 = new Item();
  157. $item3->setName("Item3");
  158. $item3->setCategory($category1);
  159. $this->em->persist($item3);
  160. $item4 = new Item();
  161. $item4->setName("Item4");
  162. $item4->setCategory($category1);
  163. $this->em->persist($item4);
  164. $this->em->flush();
  165. $item1 = new Item();
  166. $item1->setName("Item1");
  167. $item1->setPosition(0);
  168. $item1->setCategory($category1);
  169. $this->em->persist($item1);
  170. $item2 = new Item();
  171. $item2->setName("Item2");
  172. $item2->setPosition(0);
  173. $item2->setCategory($category1);
  174. $this->em->persist($item2);
  175. $item2 = new Item();
  176. $item2->setName("Item2_2");
  177. $item2->setPosition(0);
  178. $item2->setCategory($category2);
  179. $this->em->persist($item2);
  180. $this->em->flush();
  181. $item1 = new Item();
  182. $item1->setName("Item1_2");
  183. $item1->setPosition(0);
  184. $item1->setCategory($category2);
  185. $this->em->persist($item1);
  186. $this->em->flush();
  187. $this->em->clear();
  188. $repo = $this->em->getRepository('Sortable\\Fixture\\Category');
  189. $category1 = $repo->findOneByName('Category1');
  190. $category2 = $repo->findOneByName('Category2');
  191. $repo = $this->em->getRepository('Sortable\\Fixture\\Item');
  192. $items = $repo->getBySortableGroups(array('category' => $category1));
  193. $this->assertEquals("Item1", $items[0]->getName());
  194. $this->assertEquals("Category1", $items[0]->getCategory()->getName());
  195. $this->assertEquals("Item2", $items[1]->getName());
  196. $this->assertEquals("Category1", $items[1]->getCategory()->getName());
  197. $this->assertEquals("Item3", $items[2]->getName());
  198. $this->assertEquals("Category1", $items[2]->getCategory()->getName());
  199. $this->assertEquals("Item4", $items[3]->getName());
  200. $this->assertEquals("Category1", $items[3]->getCategory()->getName());
  201. $items = $repo->getBySortableGroups(array('category' => $category2));
  202. $this->assertEquals("Item1_2", $items[0]->getName());
  203. $this->assertEquals("Category2", $items[0]->getCategory()->getName());
  204. $this->assertEquals("Item2_2", $items[1]->getName());
  205. $this->assertEquals("Category2", $items[1]->getCategory()->getName());
  206. }
  207. protected function getUsedEntityFixtures()
  208. {
  209. return array(
  210. self::NODE,
  211. self::ITEM,
  212. self::CATEGORY
  213. );
  214. }
  215. private function populate()
  216. {
  217. $node = new Node();
  218. $node->setName("Node1");
  219. $node->setPath("/");
  220. $this->em->persist($node);
  221. $this->em->flush();
  222. $this->em->clear();
  223. $this->nodeId = $node->getId();
  224. }
  225. }