PageRenderTime 42ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/propel_16/vendor/propel/test/testsuite/generator/behavior/TimestampableBehaviorTest.php

http://github.com/eventhorizonpl/forked-php-orm-benchmark
PHP | 247 lines | 202 code | 24 blank | 21 comment | 2 complexity | 728c227a0ccb6274e0249d904e57d1ef MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0
  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. $this->assertEquals($t1->getUpdatedAt('U'), $tsave, 'Timestampable sets updated_column to time() on creation');
  41. sleep(1);
  42. $t1->setTitle('foo');
  43. $tupdate = time();
  44. $t1->save();
  45. $this->assertEquals($t1->getUpdatedAt('U'), $tupdate, 'Timestampable changes updated_column to time() on update');
  46. }
  47. public function testPreSaveNoChange()
  48. {
  49. $t1 = new Table2();
  50. $this->assertNull($t1->getUpdatedAt());
  51. $tsave = time();
  52. $t1->save();
  53. $this->assertEquals($t1->getUpdatedAt('U'), $tsave, 'Timestampable sets updated_column to time() on creation');
  54. sleep(1);
  55. $tupdate = time();
  56. $t1->save();
  57. $this->assertEquals($t1->getUpdatedAt('U'), $tsave, 'Timestampable only changes updated_column if the object was modified');
  58. }
  59. public function testPreSaveManuallyUpdated()
  60. {
  61. $t1 = new Table2();
  62. $t1->setUpdatedAt(time() - 10);
  63. $tsave = time();
  64. $t1->save();
  65. $this->assertNotEquals($t1->getUpdatedAt('U'), $tsave, 'Timestampable does not set updated_column to time() on creation when it is set by the user');
  66. // tip: if I set it to time()-10 a second time, the object sees that I want to change it to the same value
  67. // and skips the update, therefore the updated_at is not in the list of modified columns,
  68. // and the behavior changes it to the current date... let's say it's an edge case
  69. $t1->setUpdatedAt(time() - 15);
  70. $tupdate = time();
  71. $t1->save();
  72. $this->assertNotEquals($t1->getUpdatedAt('U'), $tupdate, 'Timestampable does not change updated_column to time() on update when it is set by the user');
  73. }
  74. public function testPreInsert()
  75. {
  76. $t1 = new Table2();
  77. $this->assertNull($t1->getCreatedAt());
  78. $tsave = time();
  79. $t1->save();
  80. $this->assertEquals($t1->getCreatedAt('U'), $tsave, 'Timestampable sets created_column to time() on creation');
  81. sleep(1);
  82. $t1->setTitle('foo');
  83. $tupdate = time();
  84. $t1->save();
  85. $this->assertEquals($t1->getCreatedAt('U'), $tsave, 'Timestampable does not update created_column on update');
  86. }
  87. public function testPreInsertManuallyUpdated()
  88. {
  89. $t1 = new Table2();
  90. $t1->setCreatedAt(time() - 10);
  91. $tsave = time();
  92. $t1->save();
  93. $this->assertNotEquals($t1->getCreatedAt('U'), $tsave, 'Timestampable does not set created_column to time() on creation when it is set by the user');
  94. }
  95. public function testObjectKeepUpdateDateUnchanged()
  96. {
  97. $t1 = new Table2();
  98. $t1->setUpdatedAt(time() - 10);
  99. $tsave = time();
  100. $t1->save();
  101. $this->assertNotEquals($t1->getUpdatedAt('U'), $tsave);
  102. // let's save it a second time; the updated_at should be changed
  103. $t1->setTitle('foo');
  104. $tsave = time();
  105. $t1->save();
  106. $this->assertEquals($t1->getUpdatedAt('U'), $tsave);
  107. // now let's do this a second time
  108. $t1 = new Table2();
  109. $t1->setUpdatedAt(time() - 10);
  110. $tsave = time();
  111. $t1->save();
  112. $this->assertNotEquals($t1->getUpdatedAt('U'), $tsave);
  113. // let's save it a second time; the updated_at should be changed
  114. $t1->keepUpdateDateUnchanged();
  115. $t1->setTitle('foo');
  116. $tsave = time();
  117. $t1->save();
  118. $this->assertNotEquals($t1->getUpdatedAt('U'), $tsave, 'keepUpdateDateUnchanged() prevents the behavior from updating the update date');
  119. }
  120. protected function populateUpdatedAt()
  121. {
  122. Table2Query::create()->deleteAll();
  123. $ts = new PropelObjectCollection();
  124. $ts->setModel('Table2');
  125. for ($i=0; $i < 10; $i++) {
  126. $t = new Table2();
  127. $t->setTitle('UpdatedAt' . $i);
  128. /* additional -30 in case the check is done in the same second (which we can't guarantee, so no assert(8 ...) below).*/
  129. $t->setUpdatedAt(time() - $i * 24 * 60 * 60 - 30);
  130. $ts[]= $t;
  131. }
  132. $ts->save();
  133. }
  134. protected function populateCreatedAt()
  135. {
  136. Table2Query::create()->deleteAll();
  137. $ts = new PropelObjectCollection();
  138. $ts->setModel('Table2');
  139. for ($i=0; $i < 10; $i++) {
  140. $t = new Table2();
  141. $t->setTitle('CreatedAt' . $i);
  142. $t->setCreatedAt(time() - $i * 24 * 60 * 60 - 30);
  143. $ts[]= $t;
  144. }
  145. $ts->save();
  146. }
  147. public function testQueryRecentlyUpdated()
  148. {
  149. $q = Table2Query::create()->recentlyUpdated();
  150. $this->assertTrue($q instanceof Table2Query, 'recentlyUpdated() returns the current Query object');
  151. $this->populateUpdatedAt();
  152. $ts = Table2Query::create()->recentlyUpdated()->count();
  153. $this->assertEquals(7, $ts, 'recentlyUpdated() returns the elements updated in the last 7 days by default');
  154. $ts = Table2Query::create()->recentlyUpdated(5)->count();
  155. $this->assertEquals(5, $ts, 'recentlyUpdated() accepts a number of days as parameter');
  156. }
  157. public function testQueryRecentlyCreated()
  158. {
  159. $q = Table2Query::create()->recentlyCreated();
  160. $this->assertTrue($q instanceof Table2Query, 'recentlyCreated() returns the current Query object');
  161. $this->populateCreatedAt();
  162. $ts = Table2Query::create()->recentlyCreated()->count();
  163. $this->assertEquals(7, $ts, 'recentlyCreated() returns the elements created in the last 7 days by default');
  164. $ts = Table2Query::create()->recentlyCreated(5)->count();
  165. $this->assertEquals(5, $ts, 'recentlyCreated() accepts a number of days as parameter');
  166. }
  167. public function testQueryLastUpdatedFirst()
  168. {
  169. $q = Table2Query::create()->lastUpdatedFirst();
  170. $this->assertTrue($q instanceof Table2Query, 'lastUpdatedFirst() returns the current Query object');
  171. $this->populateUpdatedAt();
  172. $t = Table2Query::create()->lastUpdatedFirst()->findOne();
  173. $this->assertEquals('UpdatedAt0', $t->getTitle(), 'lastUpdatedFirst() returns element with most recent update date first');
  174. }
  175. public function testQueryFirstUpdatedFirst()
  176. {
  177. $q = Table2Query::create()->firstUpdatedFirst();
  178. $this->assertTrue($q instanceof Table2Query, 'firstUpdatedFirst() returns the current Query object');
  179. $this->populateUpdatedAt();
  180. $t = Table2Query::create()->firstUpdatedFirst()->findOne();
  181. $this->assertEquals('UpdatedAt9', $t->getTitle(), 'firstUpdatedFirst() returns the element with oldest updated date first');
  182. }
  183. public function testQueryLastCreatedFirst()
  184. {
  185. $q = Table2Query::create()->lastCreatedFirst();
  186. $this->assertTrue($q instanceof Table2Query, 'lastCreatedFirst() returns the current Query object');
  187. $this->populateCreatedAt();
  188. $t = Table2Query::create()->lastCreatedFirst()->findOne();
  189. $this->assertEquals('CreatedAt0', $t->getTitle(), 'lastCreatedFirst() returns element with most recent create date first');
  190. }
  191. public function testQueryFirstCreatedFirst()
  192. {
  193. $q = Table2Query::create()->firstCreatedFirst();
  194. $this->assertTrue($q instanceof Table2Query, 'firstCreatedFirst() returns the current Query object');
  195. $this->populateCreatedAt();
  196. $t = Table2Query::create()->firstCreatedFirst()->findOne();
  197. $this->assertEquals('CreatedAt9', $t->getTitle(), 'firstCreatedFirst() returns the element with oldest create date first');
  198. }
  199. public function testDisableUpdatedAt()
  200. {
  201. $schema = <<<EOF
  202. <database name="timestampable_database">
  203. <table name="table_without_updated_at">
  204. <column name="id" type="INTEGER" primaryKey="true" />
  205. <behavior name="timestampable">
  206. <parameter name="disable_updated_at" value="true" />
  207. </behavior>
  208. </table>
  209. </database>
  210. EOF;
  211. $builder = new PropelQuickBuilder();
  212. $builder->setSchema($schema);
  213. $builder->build();
  214. $this->assertTrue(method_exists('TableWithoutUpdatedAt', 'getCreatedAt'));
  215. $this->assertTrue(method_exists('TableWithoutUpdatedAt', 'setCreatedAt'));
  216. $this->assertFalse(method_exists('TableWithoutUpdatedAt', 'getUpdatedAt'));
  217. $this->assertFalse(method_exists('TableWithoutUpdatedAt', 'setUpdatedAt'));
  218. $obj = new TableWithoutUpdatedAt();
  219. $this->assertNull($obj->getCreatedAt());
  220. $this->assertEquals(1, $obj->save());
  221. $this->assertNotNull($obj->getCreatedAt());
  222. }
  223. }