SortableTest.php 9.5 KB

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