PageRenderTime 114ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/test/testsuite/generator/behavior/TimestampableBehaviorTest.php

http://github.com/propelorm/Propel
PHP | 259 lines | 187 code | 42 blank | 30 comment | 2 complexity | caa61b1623ce7f02bda95781d6c3c3bb 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. require_once dirname(__FILE__) . '/../../../tools/helpers/bookstore/BookstoreTestBase.php';
  10. require_once dirname(__FILE__) . '/../../../../generator/lib/config/GeneratorConfig.php';
  11. require_once dirname(__FILE__) . '/../../../../generator/lib/model/Behavior.php';
  12. require_once dirname(__FILE__) . '/../../../../generator/lib/behavior/TimestampableBehavior.php';
  13. require_once dirname(__FILE__) . '/../../../../generator/lib/util/PropelQuickBuilder.php';
  14. /**
  15. * Tests for TimestampableBehavior class
  16. *
  17. * @author François Zaninotto
  18. * @version $Revision$
  19. * @package generator.behavior
  20. */
  21. class TimestampableBehaviorTest extends BookstoreTestBase
  22. {
  23. public function testParameters()
  24. {
  25. $table2 = Table2Peer::getTableMap();
  26. $this->assertEquals(count($table2->getColumns()), 4, 'Timestampable adds two columns by default');
  27. $this->assertTrue(method_exists('Table2', 'getCreatedAt'), 'Timestamplable adds a created_at column by default');
  28. $this->assertTrue(method_exists('Table2', 'getUpdatedAt'), 'Timestamplable adds an updated_at column by default');
  29. $table1 = Table1Peer::getTableMap();
  30. $this->assertEquals(count($table1->getColumns()), 4, 'Timestampable does not add two columns when they already exist');
  31. $this->assertTrue(method_exists('Table1', 'getCreatedOn'), 'Timestamplable allows customization of create_column name');
  32. $this->assertTrue(method_exists('Table1', 'getUpdatedOn'), 'Timestamplable allows customization of update_column name');
  33. }
  34. public function testPreSave()
  35. {
  36. $t1 = new Table2();
  37. $this->assertNull($t1->getUpdatedAt());
  38. $tsave = time();
  39. $t1->save();
  40. // Do not use assertEquals or similar, the second may have just changed!
  41. // If both are integers, the update_at has been set.
  42. $this->assertGreaterThanOrEqual($tsave, $t1->getUpdatedAt('U'), 'Timestampable sets updated_column to time() on creation');
  43. // Ensure the timestamp will change.
  44. sleep(1);
  45. $t1->setTitle('foo');
  46. $t1->save();
  47. // Do not use assertEquals or similar, time goes by, here too!
  48. $this->assertGreaterThan($tsave, $t1->getUpdatedAt('U'), 'Timestampable changes updated_column to time() on update');
  49. }
  50. public function testPreSaveNoChange()
  51. {
  52. $t1 = new Table2();
  53. $t1->save();
  54. $date = $t1->getUpdatedAt('U');
  55. // Ensure the timestamp will change.
  56. sleep(1);
  57. $t1->save();
  58. $this->assertEquals($date, $t1->getUpdatedAt('U'), '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. // Do not use assertEquals or similar, the second may have just changed!
  82. // If both are integers, the created_at has been set.
  83. $this->assertGreaterThanOrEqual($tsave, $t1->getCreatedAt('U'), 'Timestampable sets created_column to time() on creation');
  84. $tcreated = $t1->getCreatedAt('U');
  85. // Ensure the timestamp will change.
  86. sleep(1);
  87. $t1->setTitle('foo');
  88. $t1->save();
  89. $this->assertEquals($tcreated, $t1->getCreatedAt('U'), 'Timestampable does not update created_column on update');
  90. }
  91. public function testPreInsertManuallyUpdated()
  92. {
  93. $tcreated = time() - 10;
  94. $t1 = new Table2();
  95. $t1->setCreatedAt($tcreated);
  96. $t1->save();
  97. $this->assertEquals($tcreated, $t1->getCreatedAt('U'), 'Timestampable does not set created_column to time() on creation when it is set by the user');
  98. }
  99. /**
  100. * @depends testPreSave
  101. * @depends testPreSaveManuallyUpdated
  102. */
  103. public function testObjectKeepUpdateDateUnchanged()
  104. {
  105. $tsave = time() - 10;
  106. $t1 = new Table2();
  107. $t1->setUpdatedAt($tsave);
  108. $t1->save();
  109. $t1->keepUpdateDateUnchanged();
  110. $t1->setTitle('foo');
  111. $t1->save();
  112. $this->assertEquals($tsave, $t1->getUpdatedAt('U'), 'keepUpdateDateUnchanged() prevents the behavior from updating the update date');
  113. }
  114. protected function populateUpdatedAt()
  115. {
  116. Table2Query::create()->deleteAll();
  117. $ts = new PropelObjectCollection();
  118. $ts->setModel('Table2');
  119. for ($i=0; $i < 10; $i++) {
  120. $t = new Table2();
  121. $t->setTitle('UpdatedAt' . $i);
  122. /* additional -30 in case the check is done in the same second (which we can't guarantee, so no assert(8 ...) below).*/
  123. $t->setUpdatedAt(time() - $i * 24 * 60 * 60 - 30);
  124. $ts[]= $t;
  125. }
  126. $ts->save();
  127. }
  128. protected function populateCreatedAt()
  129. {
  130. Table2Query::create()->deleteAll();
  131. $ts = new PropelObjectCollection();
  132. $ts->setModel('Table2');
  133. for ($i=0; $i < 10; $i++) {
  134. $t = new Table2();
  135. $t->setTitle('CreatedAt' . $i);
  136. $t->setCreatedAt(time() - $i * 24 * 60 * 60 - 30);
  137. $ts[]= $t;
  138. }
  139. $ts->save();
  140. }
  141. public function testQueryRecentlyUpdated()
  142. {
  143. $q = Table2Query::create()->recentlyUpdated();
  144. $this->assertTrue($q instanceof Table2Query, 'recentlyUpdated() returns the current Query object');
  145. $this->populateUpdatedAt();
  146. $ts = Table2Query::create()->recentlyUpdated()->count();
  147. $this->assertEquals(7, $ts, 'recentlyUpdated() returns the elements updated in the last 7 days by default');
  148. $ts = Table2Query::create()->recentlyUpdated(5)->count();
  149. $this->assertEquals(5, $ts, 'recentlyUpdated() accepts a number of days as parameter');
  150. }
  151. public function testQueryRecentlyCreated()
  152. {
  153. $q = Table2Query::create()->recentlyCreated();
  154. $this->assertTrue($q instanceof Table2Query, 'recentlyCreated() returns the current Query object');
  155. $this->populateCreatedAt();
  156. $ts = Table2Query::create()->recentlyCreated()->count();
  157. $this->assertEquals(7, $ts, 'recentlyCreated() returns the elements created in the last 7 days by default');
  158. $ts = Table2Query::create()->recentlyCreated(5)->count();
  159. $this->assertEquals(5, $ts, 'recentlyCreated() accepts a number of days as parameter');
  160. }
  161. public function testQueryLastUpdatedFirst()
  162. {
  163. $q = Table2Query::create()->lastUpdatedFirst();
  164. $this->assertTrue($q instanceof Table2Query, 'lastUpdatedFirst() returns the current Query object');
  165. $this->populateUpdatedAt();
  166. $t = Table2Query::create()->lastUpdatedFirst()->findOne();
  167. $this->assertEquals('UpdatedAt0', $t->getTitle(), 'lastUpdatedFirst() returns element with most recent update date first');
  168. }
  169. public function testQueryFirstUpdatedFirst()
  170. {
  171. $q = Table2Query::create()->firstUpdatedFirst();
  172. $this->assertTrue($q instanceof Table2Query, 'firstUpdatedFirst() returns the current Query object');
  173. $this->populateUpdatedAt();
  174. $t = Table2Query::create()->firstUpdatedFirst()->findOne();
  175. $this->assertEquals('UpdatedAt9', $t->getTitle(), 'firstUpdatedFirst() returns the element with oldest updated date first');
  176. }
  177. public function testQueryLastCreatedFirst()
  178. {
  179. $q = Table2Query::create()->lastCreatedFirst();
  180. $this->assertTrue($q instanceof Table2Query, 'lastCreatedFirst() returns the current Query object');
  181. $this->populateCreatedAt();
  182. $t = Table2Query::create()->lastCreatedFirst()->findOne();
  183. $this->assertEquals('CreatedAt0', $t->getTitle(), 'lastCreatedFirst() returns element with most recent create date first');
  184. }
  185. public function testQueryFirstCreatedFirst()
  186. {
  187. $q = Table2Query::create()->firstCreatedFirst();
  188. $this->assertTrue($q instanceof Table2Query, 'firstCreatedFirst() returns the current Query object');
  189. $this->populateCreatedAt();
  190. $t = Table2Query::create()->firstCreatedFirst()->findOne();
  191. $this->assertEquals('CreatedAt9', $t->getTitle(), 'firstCreatedFirst() returns the element with oldest create date first');
  192. }
  193. public function testDisableUpdatedAt()
  194. {
  195. $schema = <<<EOF
  196. <database name="timestampable_database">
  197. <table name="table_without_updated_at">
  198. <column name="id" type="INTEGER" primaryKey="true" />
  199. <behavior name="timestampable">
  200. <parameter name="disable_updated_at" value="true" />
  201. </behavior>
  202. </table>
  203. </database>
  204. EOF;
  205. $builder = new PropelQuickBuilder();
  206. $builder->setSchema($schema);
  207. $builder->build();
  208. $this->assertTrue(method_exists('TableWithoutUpdatedAt', 'getCreatedAt'));
  209. $this->assertTrue(method_exists('TableWithoutUpdatedAt', 'setCreatedAt'));
  210. $this->assertFalse(method_exists('TableWithoutUpdatedAt', 'getUpdatedAt'));
  211. $this->assertFalse(method_exists('TableWithoutUpdatedAt', 'setUpdatedAt'));
  212. $obj = new TableWithoutUpdatedAt();
  213. $this->assertNull($obj->getCreatedAt());
  214. $this->assertEquals(1, $obj->save());
  215. $this->assertNotNull($obj->getCreatedAt());
  216. }
  217. }