/airtime_mvc/library/propel/test/testsuite/generator/behavior/sortable/SortableBehaviorObjectBuilderModifierTest.php

https://github.com/DoghouseMedia/Airtime · PHP · 279 lines · 221 code · 28 blank · 30 comment · 0 complexity · a15c99cec278cb25ce899ff2eaf877b2 MD5 · raw file

  1. <?php
  2. /*
  3. * $Id: SortableBehaviorTest.php 1356 2009-12-11 16:36:55Z francois $
  4. * This file is part of the Propel package.
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @license MIT License
  9. */
  10. require_once 'tools/helpers/bookstore/behavior/BookstoreSortableTestBase.php';
  11. /**
  12. * Tests for SortableBehavior class
  13. *
  14. * @author Massimiliano Arione
  15. * @version $Revision: 1612 $
  16. * @package generator.behavior.sortable
  17. */
  18. class SortableBehaviorObjectBuilderModifierTest extends BookstoreSortableTestBase
  19. {
  20. protected function setUp()
  21. {
  22. parent::setUp();
  23. $this->populateTable11();
  24. }
  25. public function testPreInsert()
  26. {
  27. Table11Peer::doDeleteAll();
  28. $t1 = new Table11();
  29. $t1->save();
  30. $this->assertEquals($t1->getRank(), 1, 'Sortable inserts new line in first position if no row present');
  31. $t2 = new Table11();
  32. $t2->setTitle('row2');
  33. $t2->save();
  34. $this->assertEquals($t2->getRank(), 2, 'Sortable inserts new line in last position');
  35. }
  36. public function testPreDelete()
  37. {
  38. $max = Table11Peer::getMaxRank();
  39. $t3 = Table11Peer::retrieveByRank(3);
  40. $t3->delete();
  41. $this->assertEquals($max - 1, Table11Peer::getMaxRank(), 'Sortable rearrange subsequent rows on delete');
  42. $c = new Criteria();
  43. $c->add(Table11Peer::TITLE, 'row4');
  44. $t4 = Table11Peer::doSelectOne($c);
  45. $this->assertEquals(3, $t4->getRank(), 'Sortable rearrange subsequent rows on delete');
  46. }
  47. public function testIsFirst()
  48. {
  49. $first = Table11Peer::retrieveByRank(1);
  50. $middle = Table11Peer::retrieveByRank(2);
  51. $last = Table11Peer::retrieveByRank(4);
  52. $this->assertTrue($first->isFirst(), 'isFirst() returns true for the first in the rank');
  53. $this->assertFalse($middle->isFirst(), 'isFirst() returns false for a middle rank');
  54. $this->assertFalse($last->isFirst(), 'isFirst() returns false for the last in the rank');
  55. }
  56. public function testIsLast()
  57. {
  58. $first = Table11Peer::retrieveByRank(1);
  59. $middle = Table11Peer::retrieveByRank(2);
  60. $last = Table11Peer::retrieveByRank(4);
  61. $this->assertFalse($first->isLast(), 'isLast() returns false for the first in the rank');
  62. $this->assertFalse($middle->isLast(), 'isLast() returns false for a middle rank');
  63. $this->assertTrue($last->isLast(), 'isLast() returns true for the last in the rank');
  64. }
  65. public function testGetNext()
  66. {
  67. $t = Table11Peer::retrieveByRank(3);
  68. $this->assertEquals(4, $t->getNext()->getRank(), 'getNext() returns the next object in rank');
  69. $t = Table11Peer::retrieveByRank(4);
  70. $this->assertNull($t->getNext(), 'getNext() returns null for the last object');
  71. }
  72. public function testGetPrevious()
  73. {
  74. $t = Table11Peer::retrieveByRank(3);
  75. $this->assertEquals(2, $t->getPrevious()->getRank(), 'getPrevious() returns the previous object in rank');
  76. $t = Table11Peer::retrieveByRank(1);
  77. $this->assertNull($t->getPrevious(), 'getPrevious() returns null for the first object');
  78. }
  79. public function testInsertAtRank()
  80. {
  81. $t = new Table11();
  82. $t->setTitle('new');
  83. $t->insertAtRank(2);
  84. $this->assertEquals(2, $t->getRank(), 'insertAtRank() sets the position');
  85. $this->assertTrue($t->isNew(), 'insertAtRank() doesn\'t save the object');
  86. $t->save();
  87. $expected = array(1 => 'row1', 2 => 'new', 3 => 'row2', 4 => 'row3', 5 => 'row4');
  88. $this->assertEquals($expected, $this->getFixturesArray(), 'insertAtRank() shifts the entire suite');
  89. }
  90. public function testInsertAtMaxRankPlusOne()
  91. {
  92. $t = new Table11();
  93. $t->setTitle('new');
  94. $t->insertAtRank(5);
  95. $this->assertEquals(5, $t->getRank(), 'insertAtRank() sets the position');
  96. $t->save();
  97. $expected = array(1 => 'row1', 2 => 'row2', 3 => 'row3', 4 => 'row4', 5 => 'new');
  98. $this->assertEquals($expected, $this->getFixturesArray(), 'insertAtRank() can insert an object at the end of the list');
  99. }
  100. /**
  101. * @expectedException PropelException
  102. */
  103. public function testInsertAtNegativeRank()
  104. {
  105. $t = new Table11();
  106. $t->insertAtRank(0);
  107. }
  108. /**
  109. * @expectedException PropelException
  110. */
  111. public function testInsertAtOverMaxRank()
  112. {
  113. $t = new Table11();
  114. $t->insertAtRank(6);
  115. }
  116. public function testInsertAtBottom()
  117. {
  118. $t = new Table11();
  119. $t->setTitle('new');
  120. $t->insertAtBottom();
  121. $this->assertEquals(5, $t->getRank(), 'insertAtBottom() sets the position to the last');
  122. $this->assertTrue($t->isNew(), 'insertAtBottom() doesn\'t save the object');
  123. $t->save();
  124. $expected = array(1 => 'row1', 2 => 'row2', 3 => 'row3', 4 => 'row4', 5 => 'new');
  125. $this->assertEquals($expected, $this->getFixturesArray(), 'insertAtBottom() does not shift the entire suite');
  126. }
  127. public function testInsertAtTop()
  128. {
  129. $t = new Table11();
  130. $t->setTitle('new');
  131. $t->insertAtTop();
  132. $this->assertEquals(1, $t->getRank(), 'insertAtTop() sets the position to 1');
  133. $this->assertTrue($t->isNew(), 'insertAtTop() doesn\'t save the object');
  134. $t->save();
  135. $expected = array(1 => 'new', 2 => 'row1', 3 => 'row2', 4 => 'row3', 5 => 'row4');
  136. $this->assertEquals($expected, $this->getFixturesArray(), 'insertAtTop() shifts the entire suite');
  137. }
  138. public function testMoveToRank()
  139. {
  140. $t2 = Table11Peer::retrieveByRank(2);
  141. $t2->moveToRank(3);
  142. $expected = array(1 => 'row1', 2 => 'row3', 3 => 'row2', 4 => 'row4');
  143. $this->assertEquals($expected, $this->getFixturesArray(), 'moveToRank() can move up');
  144. $t2->moveToRank(1);
  145. $expected = array(1 => 'row2', 2 => 'row1', 3 => 'row3', 4 => 'row4');
  146. $this->assertEquals($expected, $this->getFixturesArray(), 'moveToRank() can move to the first rank');
  147. $t2->moveToRank(4);
  148. $expected = array(1 => 'row1', 2 => 'row3', 3 => 'row4', 4 => 'row2');
  149. $this->assertEquals($expected, $this->getFixturesArray(), 'moveToRank() can move to the last rank');
  150. $t2->moveToRank(2);
  151. $expected = array(1 => 'row1', 2 => 'row2', 3 => 'row3', 4 => 'row4');
  152. $this->assertEquals($expected, $this->getFixturesArray(), 'moveToRank() can move down');
  153. }
  154. /**
  155. * @expectedException PropelException
  156. */
  157. public function testMoveToNewObject()
  158. {
  159. $t = new Table11();
  160. $t->moveToRank(2);
  161. }
  162. /**
  163. * @expectedException PropelException
  164. */
  165. public function testMoveToNegativeRank()
  166. {
  167. $t = Table11Peer::retrieveByRank(2);
  168. $t->moveToRank(0);
  169. }
  170. /**
  171. * @expectedException PropelException
  172. */
  173. public function testMoveToOverMaxRank()
  174. {
  175. $t = Table11Peer::retrieveByRank(2);
  176. $t->moveToRank(5);
  177. }
  178. public function testSwapWith()
  179. {
  180. $t2 = Table11Peer::retrieveByRank(2);
  181. $t4 = Table11Peer::retrieveByRank(4);
  182. $t2->swapWith($t4);
  183. $expected = array(1 => 'row1', 2 => 'row4', 3 => 'row3', 4 => 'row2');
  184. $this->assertEquals($expected, $this->getFixturesArray(), 'swapWith() swaps ranks of the two objects and leaves the other ranks unchanged');
  185. }
  186. public function testMoveUp()
  187. {
  188. $t3 = Table11Peer::retrieveByRank(3);
  189. $res = $t3->moveUp();
  190. $this->assertEquals($t3, $res, 'moveUp() returns the current object');
  191. $expected = array(1 => 'row1', 2 => 'row3', 3 => 'row2', 4 => 'row4');
  192. $this->assertEquals($expected, $this->getFixturesArray(), 'moveUp() swaps ranks with the object of higher rank');
  193. $t3->moveUp();
  194. $expected = array(1 => 'row3', 2 => 'row1', 3 => 'row2', 4 => 'row4');
  195. $this->assertEquals($expected, $this->getFixturesArray(), 'moveUp() swaps ranks with the object of higher rank');
  196. $res = $t3->moveUp();
  197. $expected = array(1 => 'row3', 2 => 'row1', 3 => 'row2', 4 => 'row4');
  198. $this->assertEquals($expected, $this->getFixturesArray(), 'moveUp() changes nothing when called on the object at the top');
  199. }
  200. public function testMoveDown()
  201. {
  202. $t2 = Table11Peer::retrieveByRank(2);
  203. $res = $t2->moveDown();
  204. $this->assertEquals($t2, $res, 'moveDown() returns the current object');
  205. $expected = array(1 => 'row1', 2 => 'row3', 3 => 'row2', 4 => 'row4');
  206. $this->assertEquals($expected, $this->getFixturesArray(), 'moveDown() swaps ranks with the object of lower rank');
  207. $t2->moveDown();
  208. $expected = array(1 => 'row1', 2 => 'row3', 3 => 'row4', 4 => 'row2');
  209. $this->assertEquals($expected, $this->getFixturesArray(), 'moveDown() swaps ranks with the object of lower rank');
  210. $res = $t2->moveDown();
  211. $expected = array(1 => 'row1', 2 => 'row3', 3 => 'row4', 4 => 'row2');
  212. $this->assertEquals($expected, $this->getFixturesArray(), 'moveDown() changes nothing when called on the object at the bottom');
  213. }
  214. public function testMoveToTop()
  215. {
  216. $t3 = Table11Peer::retrieveByRank(3);
  217. $res = $t3->moveToTop();
  218. $this->assertEquals($t3, $res, 'moveToTop() returns the current oobject');
  219. $expected = array(1 => 'row3', 2 => 'row1', 3 => 'row2', 4 => 'row4');
  220. $this->assertEquals($expected, $this->getFixturesArray(), 'moveToTop() moves to the top');
  221. $res = $t3->moveToTop();
  222. $expected = array(1 => 'row3', 2 => 'row1', 3 => 'row2', 4 => 'row4');
  223. $this->assertEquals($expected, $this->getFixturesArray(), 'moveToTop() changes nothing when called on the top node');
  224. }
  225. public function testMoveToBottom()
  226. {
  227. $t2 = Table11Peer::retrieveByRank(2);
  228. $res = $t2->moveToBottom();
  229. $this->assertEquals($t2, $res, 'moveToBottom() returns the current object');
  230. $expected = array(1 => 'row1', 2 => 'row3', 3 => 'row4', 4 => 'row2');
  231. $this->assertEquals($expected, $this->getFixturesArray(), 'moveToBottom() moves to the bottom');
  232. $res = $t2->moveToBottom();
  233. $this->assertFalse($res, 'moveToBottom() returns false when called on the bottom node');
  234. $expected = array(1 => 'row1', 2 => 'row3', 3 => 'row4', 4 => 'row2');
  235. $this->assertEquals($expected, $this->getFixturesArray(), 'moveToBottom() changes nothing when called on the bottom node');
  236. }
  237. public function testRemoveFromList()
  238. {
  239. $t2 = Table11Peer::retrieveByRank(2);
  240. $res = $t2->removeFromList();
  241. $this->assertTrue($res instanceof Table11, 'removeFromList() returns the current object');
  242. $this->assertNull($res->getRank(), 'removeFromList() resets the object\'s rank');
  243. Table11Peer::clearInstancePool();
  244. $expected = array(1 => 'row1', 2 => 'row2', 3 => 'row3', 4 => 'row4');
  245. $this->assertEquals($expected, $this->getFixturesArray(), 'removeFromList() does not change the list until the object is saved');
  246. $t2->save();
  247. Table11Peer::clearInstancePool();
  248. $expected = array(null => 'row2', 1 => 'row1', 2 => 'row3', 3 => 'row4');
  249. $this->assertEquals($expected, $this->getFixturesArray(), 'removeFromList() changes the list once the object is saved');
  250. }
  251. }