SortableTest.php 9.0 KB

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