PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

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