PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/nextbigsound/propel-orm
PHP | 215 lines | 174 code | 20 blank | 21 comment | 2 complexity | 1333a9cca5a2ff9c2cee7f13cf8b7129 MD5 | raw file
  1. <?php
  2. /*
  3. * $Id$
  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/BookstoreTestBase.php';
  11. /**
  12. * Tests for TimestampableBehavior class
  13. *
  14. * @author François Zaninotto
  15. * @version $Revision$
  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. $t->setUpdatedAt(time() - $i * 24 * 60 * 60);
  126. $ts[]= $t;
  127. }
  128. $ts->save();
  129. }
  130. protected function populateCreatedAt()
  131. {
  132. Table2Query::create()->deleteAll();
  133. $ts = new PropelObjectCollection();
  134. $ts->setModel('Table2');
  135. for ($i=0; $i < 10; $i++) {
  136. $t = new Table2();
  137. $t->setTitle('CreatedAt' . $i);
  138. $t->setCreatedAt(time() - $i * 24 * 60 * 60);
  139. $ts[]= $t;
  140. }
  141. $ts->save();
  142. }
  143. public function testQueryRecentlyUpdated()
  144. {
  145. $q = Table2Query::create()->recentlyUpdated();
  146. $this->assertTrue($q instanceof Table2Query, 'recentlyUpdated() returns the current Query object');
  147. $this->populateUpdatedAt();
  148. $ts = Table2Query::create()->recentlyUpdated()->count();
  149. $this->assertEquals(8, $ts, 'recentlyUpdated() returns the elements updated in the last 7 days by default');
  150. $ts = Table2Query::create()->recentlyUpdated(5)->count();
  151. $this->assertEquals(6, $ts, 'recentlyUpdated() accepts a number of days as parameter');
  152. }
  153. public function testQueryRecentlyCreated()
  154. {
  155. $q = Table2Query::create()->recentlyCreated();
  156. $this->assertTrue($q instanceof Table2Query, 'recentlyCreated() returns the current Query object');
  157. $this->populateCreatedAt();
  158. $ts = Table2Query::create()->recentlyCreated()->count();
  159. $this->assertEquals(8, $ts, 'recentlyCreated() returns the elements created in the last 7 days by default');
  160. $ts = Table2Query::create()->recentlyCreated(5)->count();
  161. $this->assertEquals(6, $ts, 'recentlyCreated() accepts a number of days as parameter');
  162. }
  163. public function testQueryLastUpdatedFirst()
  164. {
  165. $q = Table2Query::create()->lastUpdatedFirst();
  166. $this->assertTrue($q instanceof Table2Query, 'lastUpdatedFirst() returns the current Query object');
  167. $this->populateUpdatedAt();
  168. $t = Table2Query::create()->lastUpdatedFirst()->findOne();
  169. $this->assertEquals('UpdatedAt0', $t->getTitle(), 'lastUpdatedFirst() returns element with most recent update date first');
  170. }
  171. public function testQueryFirstUpdatedFirst()
  172. {
  173. $q = Table2Query::create()->firstUpdatedFirst();
  174. $this->assertTrue($q instanceof Table2Query, 'firstUpdatedFirst() returns the current Query object');
  175. $this->populateUpdatedAt();
  176. $t = Table2Query::create()->firstUpdatedFirst()->findOne();
  177. $this->assertEquals('UpdatedAt9', $t->getTitle(), 'firstUpdatedFirst() returns the element with oldest updated date first');
  178. }
  179. public function testQueryLastCreatedFirst()
  180. {
  181. $q = Table2Query::create()->lastCreatedFirst();
  182. $this->assertTrue($q instanceof Table2Query, 'lastCreatedFirst() returns the current Query object');
  183. $this->populateCreatedAt();
  184. $t = Table2Query::create()->lastCreatedFirst()->findOne();
  185. $this->assertEquals('CreatedAt0', $t->getTitle(), 'lastCreatedFirst() returns element with most recent create date first');
  186. }
  187. public function testQueryFirstCreatedFirst()
  188. {
  189. $q = Table2Query::create()->firstCreatedFirst();
  190. $this->assertTrue($q instanceof Table2Query, 'firstCreatedFirst() returns the current Query object');
  191. $this->populateCreatedAt();
  192. $t = Table2Query::create()->firstCreatedFirst()->findOne();
  193. $this->assertEquals('CreatedAt9', $t->getTitle(), 'firstCreatedFirst() returns the element with oldest create date first');
  194. }
  195. }