/tests/Propel/Tests/Generator/Behavior/Timestampable/TimestampableBehaviorTest.php

https://github.com/fabienpomerol/Propel2 · PHP · 223 lines · 181 code · 23 blank · 19 comment · 2 complexity · 4804c1a6674ede92f5b15ce02f4f784f MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. namespace Propel\Tests\Generator\Behavior\Timestampable;
  10. use Propel\Tests\Helpers\Bookstore\BookstoreTestBase;
  11. use Propel\Tests\Bookstore\Behavior\Table1;
  12. use Propel\Tests\Bookstore\Behavior\Table1Peer;
  13. use Propel\Tests\Bookstore\Behavior\Table2;
  14. use Propel\Tests\Bookstore\Behavior\Table2Peer;
  15. use Propel\Tests\Bookstore\Behavior\Table2Query;
  16. use Propel\Runtime\Collection\ObjectCollection;
  17. /**
  18. * Tests for TimestampableBehavior class
  19. *
  20. * @author François Zaninotto
  21. */
  22. class TimestampableBehaviorTest extends BookstoreTestBase
  23. {
  24. public function testParameters()
  25. {
  26. $table2 = Table2Peer::getTableMap();
  27. $this->assertEquals(count($table2->getColumns()), 4, 'Timestampable adds two columns by default');
  28. $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table2', 'getCreatedAt'), 'Timestampable adds a created_at column by default');
  29. $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table2', 'getUpdatedAt'), 'Timestampable adds an updated_at column by default');
  30. $table1 = Table1Peer::getTableMap();
  31. $this->assertEquals(count($table1->getColumns()), 4, 'Timestampable does not add two columns when they already exist');
  32. $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table1', 'getCreatedOn'), 'Timestampable allows customization of create_column name');
  33. $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table1', 'getUpdatedOn'), 'Timestampable allows customization of update_column name');
  34. }
  35. public function testPreSave()
  36. {
  37. $t1 = new Table2();
  38. $this->assertNull($t1->getUpdatedAt());
  39. $tsave = time();
  40. $t1->save();
  41. $this->assertEquals($t1->getUpdatedAt('U'), $tsave, 'Timestampable sets updated_column to time() on creation');
  42. sleep(1);
  43. $t1->setTitle('foo');
  44. $tupdate = time();
  45. $t1->save();
  46. $this->assertEquals($t1->getUpdatedAt('U'), $tupdate, 'Timestampable changes updated_column to time() on update');
  47. }
  48. public function testPreSaveNoChange()
  49. {
  50. $t1 = new Table2();
  51. $this->assertNull($t1->getUpdatedAt());
  52. $tsave = time();
  53. $t1->save();
  54. $this->assertEquals($t1->getUpdatedAt('U'), $tsave, 'Timestampable sets updated_column to time() on creation');
  55. sleep(1);
  56. $tupdate = time();
  57. $t1->save();
  58. $this->assertEquals($t1->getUpdatedAt('U'), $tsave, 'Timestampable only changes updated_column if the object was modified');
  59. }
  60. public function testPreSaveManuallyUpdated()
  61. {
  62. $t1 = new Table2();
  63. $t1->setUpdatedAt(time() - 10);
  64. $tsave = time();
  65. $t1->save();
  66. $this->assertNotEquals($t1->getUpdatedAt('U'), $tsave, 'Timestampable does not set updated_column to time() on creation when it is set by the user');
  67. // tip: if I set it to time()-10 a second time, the object sees that I want to change it to the same value
  68. // and skips the update, therefore the updated_at is not in the list of modified columns,
  69. // and the behavior changes it to the current date... let's say it's an edge case
  70. $t1->setUpdatedAt(time() - 15);
  71. $tupdate = time();
  72. $t1->save();
  73. $this->assertNotEquals($t1->getUpdatedAt('U'), $tupdate, 'Timestampable does not change updated_column to time() on update when it is set by the user');
  74. }
  75. public function testPreInsert()
  76. {
  77. $t1 = new Table2();
  78. $this->assertNull($t1->getCreatedAt());
  79. $tsave = time();
  80. $t1->save();
  81. $this->assertEquals($t1->getCreatedAt('U'), $tsave, 'Timestampable sets created_column to time() on creation');
  82. sleep(1);
  83. $t1->setTitle('foo');
  84. $tupdate = time();
  85. $t1->save();
  86. $this->assertEquals($t1->getCreatedAt('U'), $tsave, 'Timestampable does not update created_column on update');
  87. }
  88. public function testPreInsertManuallyUpdated()
  89. {
  90. $t1 = new Table2();
  91. $t1->setCreatedAt(time() - 10);
  92. $tsave = time();
  93. $t1->save();
  94. $this->assertNotEquals($t1->getCreatedAt('U'), $tsave, 'Timestampable does not set created_column to time() on creation when it is set by the user');
  95. }
  96. public function testObjectKeepUpdateDateUnchanged()
  97. {
  98. $t1 = new Table2();
  99. $t1->setUpdatedAt(time() - 10);
  100. $tsave = time();
  101. $t1->save();
  102. $this->assertNotEquals($t1->getUpdatedAt('U'), $tsave);
  103. // let's save it a second time; the updated_at should be changed
  104. $t1->setTitle('foo');
  105. $tsave = time();
  106. $t1->save();
  107. $this->assertEquals($t1->getUpdatedAt('U'), $tsave);
  108. // now let's do this a second time
  109. $t1 = new Table2();
  110. $t1->setUpdatedAt(time() - 10);
  111. $tsave = time();
  112. $t1->save();
  113. $this->assertNotEquals($t1->getUpdatedAt('U'), $tsave);
  114. // let's save it a second time; the updated_at should be changed
  115. $t1->keepUpdateDateUnchanged();
  116. $t1->setTitle('foo');
  117. $tsave = time();
  118. $t1->save();
  119. $this->assertNotEquals($t1->getUpdatedAt('U'), $tsave, 'keepUpdateDateUnchanged() prevents the behavior from updating the update date');
  120. }
  121. protected function populateUpdatedAt()
  122. {
  123. Table2Query::create()->deleteAll();
  124. $ts = new ObjectCollection();
  125. $ts->setModel('\Propel\Tests\Bookstore\Behavior\Table2');
  126. for ($i=0; $i < 10; $i++) {
  127. $t = new Table2();
  128. $t->setTitle('UpdatedAt' . $i);
  129. /* additional -30 in case the check is done in the same second (which we can't guarantee, so no assert(8 ...) below).*/
  130. $t->setUpdatedAt(time() - $i * 24 * 60 * 60 - 30);
  131. $ts[]= $t;
  132. }
  133. $ts->save();
  134. }
  135. protected function populateCreatedAt()
  136. {
  137. Table2Query::create()->deleteAll();
  138. $ts = new ObjectCollection();
  139. $ts->setModel('\Propel\Tests\Bookstore\Behavior\Table2');
  140. for ($i=0; $i < 10; $i++) {
  141. $t = new Table2();
  142. $t->setTitle('CreatedAt' . $i);
  143. $t->setCreatedAt(time() - $i * 24 * 60 * 60 - 30);
  144. $ts[]= $t;
  145. }
  146. $ts->save();
  147. }
  148. public function testQueryRecentlyUpdated()
  149. {
  150. $q = Table2Query::create()->recentlyUpdated();
  151. $this->assertTrue($q instanceof Table2Query, 'recentlyUpdated() returns the current Query object');
  152. $this->populateUpdatedAt();
  153. $ts = Table2Query::create()->recentlyUpdated()->count();
  154. $this->assertEquals(7, $ts, 'recentlyUpdated() returns the elements updated in the last 7 days by default');
  155. $ts = Table2Query::create()->recentlyUpdated(5)->count();
  156. $this->assertEquals(5, $ts, 'recentlyUpdated() accepts a number of days as parameter');
  157. }
  158. public function testQueryRecentlyCreated()
  159. {
  160. $q = Table2Query::create()->recentlyCreated();
  161. $this->assertTrue($q instanceof Table2Query, 'recentlyCreated() returns the current Query object');
  162. $this->populateCreatedAt();
  163. $ts = Table2Query::create()->recentlyCreated()->count();
  164. $this->assertEquals(7, $ts, 'recentlyCreated() returns the elements created in the last 7 days by default');
  165. $ts = Table2Query::create()->recentlyCreated(5)->count();
  166. $this->assertEquals(5, $ts, 'recentlyCreated() accepts a number of days as parameter');
  167. }
  168. public function testQueryLastUpdatedFirst()
  169. {
  170. $q = Table2Query::create()->lastUpdatedFirst();
  171. $this->assertTrue($q instanceof Table2Query, 'lastUpdatedFirst() returns the current Query object');
  172. $this->populateUpdatedAt();
  173. $t = Table2Query::create()->lastUpdatedFirst()->findOne();
  174. $this->assertEquals('UpdatedAt0', $t->getTitle(), 'lastUpdatedFirst() returns element with most recent update date first');
  175. }
  176. public function testQueryFirstUpdatedFirst()
  177. {
  178. $q = Table2Query::create()->firstUpdatedFirst();
  179. $this->assertTrue($q instanceof Table2Query, 'firstUpdatedFirst() returns the current Query object');
  180. $this->populateUpdatedAt();
  181. $t = Table2Query::create()->firstUpdatedFirst()->findOne();
  182. $this->assertEquals('UpdatedAt9', $t->getTitle(), 'firstUpdatedFirst() returns the element with oldest updated date first');
  183. }
  184. public function testQueryLastCreatedFirst()
  185. {
  186. $q = Table2Query::create()->lastCreatedFirst();
  187. $this->assertTrue($q instanceof Table2Query, 'lastCreatedFirst() returns the current Query object');
  188. $this->populateCreatedAt();
  189. $t = Table2Query::create()->lastCreatedFirst()->findOne();
  190. $this->assertEquals('CreatedAt0', $t->getTitle(), 'lastCreatedFirst() returns element with most recent create date first');
  191. }
  192. public function testQueryFirstCreatedFirst()
  193. {
  194. $q = Table2Query::create()->firstCreatedFirst();
  195. $this->assertTrue($q instanceof Table2Query, 'firstCreatedFirst() returns the current Query object');
  196. $this->populateCreatedAt();
  197. $t = Table2Query::create()->firstCreatedFirst()->findOne();
  198. $this->assertEquals('CreatedAt9', $t->getTitle(), 'firstCreatedFirst() returns the element with oldest create date first');
  199. }
  200. }