SortableTest.php 12 KB

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