SortableTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. public function testInsertedNewNode()
  42. {
  43. $node = $this->em->find(self::NODE, $this->nodeId);
  44. //$this->assertTrue($node instanceof Sortable);
  45. $this->assertEquals(0, $node->getPosition());
  46. }
  47. public function testInsertSortedList()
  48. {
  49. for ($i = 2; $i <= 10; $i++) {
  50. $node = new Node();
  51. $node->setName("Node".$i);
  52. $node->setPath("/");
  53. $this->em->persist($node);
  54. }
  55. $this->em->flush();
  56. $this->em->clear();
  57. $nodes = $this->em->createQuery("SELECT node FROM Sortable\Fixture\Node node "
  58. ."WHERE node.path = :path ORDER BY node.position")
  59. ->setParameter('path', '/')
  60. ->getResult();
  61. $this->assertEquals(10, count($nodes));
  62. $this->assertEquals('Node1', $nodes[0]->getName());
  63. }
  64. public function testShiftForward()
  65. {
  66. $node2 = new Node();
  67. $node2->setName("Node2");
  68. $node2->setPath("/");
  69. $this->em->persist($node2);
  70. $node = new Node();
  71. $node->setName("Node3");
  72. $node->setPath("/");
  73. $this->em->persist($node);
  74. $node = new Node();
  75. $node->setName("Node4");
  76. $node->setPath("/");
  77. $this->em->persist($node);
  78. $node = new Node();
  79. $node->setName("Node5");
  80. $node->setPath("/");
  81. $this->em->persist($node);
  82. $this->em->flush();
  83. $this->assertEquals(1, $node2->getPosition());
  84. $node2->setPosition(3);
  85. $this->em->persist($node2);
  86. $this->em->flush();
  87. $this->em->clear();
  88. $repo = $this->em->getRepository('Sortable\\Fixture\\Node');
  89. $nodes = $repo->getBySortableGroups(array('path' => '/'));
  90. $this->assertEquals('Node1', $nodes[0]->getName());
  91. $this->assertEquals('Node3', $nodes[1]->getName());
  92. $this->assertEquals('Node4', $nodes[2]->getName());
  93. $this->assertEquals('Node2', $nodes[3]->getName());
  94. $this->assertEquals('Node5', $nodes[4]->getName());
  95. }
  96. public function testShiftBackward()
  97. {
  98. $node = new Node();
  99. $node->setName("Node2");
  100. $node->setPath("/");
  101. $this->em->persist($node);
  102. $node = new Node();
  103. $node->setName("Node3");
  104. $node->setPath("/");
  105. $this->em->persist($node);
  106. $node2 = new Node();
  107. $node2->setName("Node4");
  108. $node2->setPath("/");
  109. $this->em->persist($node2);
  110. $node = new Node();
  111. $node->setName("Node5");
  112. $node->setPath("/");
  113. $this->em->persist($node);
  114. $this->em->flush();
  115. $this->assertEquals(3, $node2->getPosition());
  116. $node2->setPosition(1);
  117. $this->em->persist($node2);
  118. $this->em->flush();
  119. $this->em->clear();
  120. $repo = $this->em->getRepository('Sortable\\Fixture\\Node');
  121. $nodes = $repo->getBySortableGroups(array('path' => '/'));
  122. $this->assertEquals('Node1', $nodes[0]->getName());
  123. $this->assertEquals('Node4', $nodes[1]->getName());
  124. $this->assertEquals('Node2', $nodes[2]->getName());
  125. $this->assertEquals('Node3', $nodes[3]->getName());
  126. $this->assertEquals('Node5', $nodes[4]->getName());
  127. }
  128. public function testDelete()
  129. {
  130. $node2 = new Node();
  131. $node2->setName("Node2");
  132. $node2->setPath("/");
  133. $this->em->persist($node2);
  134. $node3 = new Node();
  135. $node3->setName("Node3");
  136. $node3->setPath("/");
  137. $this->em->persist($node3);
  138. $this->em->flush();
  139. $this->em->remove($node2);
  140. $this->em->flush();
  141. $this->em->clear();
  142. $repo = $this->em->getRepository('Sortable\\Fixture\\Node');
  143. $nodes = $repo->getBySortableGroups(array('path' => '/'));
  144. $this->assertEquals('Node1', $nodes[0]->getName());
  145. $this->assertEquals('Node3', $nodes[1]->getName());
  146. $this->assertEquals(0, $nodes[0]->getPosition());
  147. $this->assertEquals(1, $nodes[1]->getPosition());
  148. }
  149. public function testGroupByAssociation()
  150. {
  151. $category1 = new Category();
  152. $category1->setName("Category1");
  153. $this->em->persist($category1);
  154. $category2 = new Category();
  155. $category2->setName("Category2");
  156. $this->em->persist($category2);
  157. $this->em->flush();
  158. $item3 = new Item();
  159. $item3->setName("Item3");
  160. $item3->setCategory($category1);
  161. $this->em->persist($item3);
  162. $item4 = new Item();
  163. $item4->setName("Item4");
  164. $item4->setCategory($category1);
  165. $this->em->persist($item4);
  166. $this->em->flush();
  167. $item1 = new Item();
  168. $item1->setName("Item1");
  169. $item1->setPosition(0);
  170. $item1->setCategory($category1);
  171. $this->em->persist($item1);
  172. $item2 = new Item();
  173. $item2->setName("Item2");
  174. $item2->setPosition(0);
  175. $item2->setCategory($category1);
  176. $this->em->persist($item2);
  177. $item2 = new Item();
  178. $item2->setName("Item2_2");
  179. $item2->setPosition(0);
  180. $item2->setCategory($category2);
  181. $this->em->persist($item2);
  182. $this->em->flush();
  183. $item1 = new Item();
  184. $item1->setName("Item1_2");
  185. $item1->setPosition(0);
  186. $item1->setCategory($category2);
  187. $this->em->persist($item1);
  188. $this->em->flush();
  189. $this->em->clear();
  190. $repo = $this->em->getRepository('Sortable\\Fixture\\Category');
  191. $category1 = $repo->findOneByName('Category1');
  192. $category2 = $repo->findOneByName('Category2');
  193. $repo = $this->em->getRepository('Sortable\\Fixture\\Item');
  194. $items = $repo->getBySortableGroups(array('category' => $category1));
  195. $this->assertEquals("Item1", $items[0]->getName());
  196. $this->assertEquals("Category1", $items[0]->getCategory()->getName());
  197. $this->assertEquals("Item2", $items[1]->getName());
  198. $this->assertEquals("Category1", $items[1]->getCategory()->getName());
  199. $this->assertEquals("Item3", $items[2]->getName());
  200. $this->assertEquals("Category1", $items[2]->getCategory()->getName());
  201. $this->assertEquals("Item4", $items[3]->getName());
  202. $this->assertEquals("Category1", $items[3]->getCategory()->getName());
  203. $items = $repo->getBySortableGroups(array('category' => $category2));
  204. $this->assertEquals("Item1_2", $items[0]->getName());
  205. $this->assertEquals("Category2", $items[0]->getCategory()->getName());
  206. $this->assertEquals("Item2_2", $items[1]->getName());
  207. $this->assertEquals("Category2", $items[1]->getCategory()->getName());
  208. }
  209. /**
  210. * Test for issue #219
  211. */
  212. public function test219()
  213. {
  214. $item1 = new SimpleListItem();
  215. $item1->setName("Item 1");
  216. $this->em->persist($item1);
  217. $this->em->flush();
  218. $item1->setName("Update...");
  219. $item1->setPosition(1);
  220. $this->em->persist($item1);
  221. $this->em->flush();
  222. $this->em->remove($item1);
  223. $this->em->flush();
  224. $this->em->clear();
  225. }
  226. /**
  227. * Test for issue #226
  228. */
  229. public function test226()
  230. {
  231. $paper1 = new Paper();
  232. $paper1->setName("Paper1");
  233. $this->em->persist($paper1);
  234. $paper2 = new Paper();
  235. $paper2->setName("Paper2");
  236. $this->em->persist($paper2);
  237. $author1 = new Author();
  238. $author1->setName("Author1");
  239. $author1->setPaper($paper1);
  240. $author2 = new Author();
  241. $author2->setName("Author2");
  242. $author2->setPaper($paper1);
  243. $author3 = new Author();
  244. $author3->setName("Author3");
  245. $author3->setPaper($paper2);
  246. $this->em->persist($author1);
  247. $this->em->persist($author2);
  248. $this->em->persist($author3);
  249. $this->em->flush();
  250. $this->assertEquals(1, $author1->getPosition());
  251. $this->assertEquals(2, $author2->getPosition());
  252. $this->assertEquals(1, $author3->getPosition());
  253. }
  254. protected function getUsedEntityFixtures()
  255. {
  256. return array(
  257. self::NODE,
  258. self::ITEM,
  259. self::CATEGORY,
  260. self::SIMPLE_LIST_ITEM,
  261. self::AUTHOR,
  262. self::PAPER,
  263. );
  264. }
  265. private function populate()
  266. {
  267. $node = new Node();
  268. $node->setName("Node1");
  269. $node->setPath("/");
  270. $this->em->persist($node);
  271. $this->em->flush();
  272. $this->em->clear();
  273. $this->nodeId = $node->getId();
  274. }
  275. }