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

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

http://github.com/propelorm/Propel2
PHP | 359 lines | 254 code | 31 blank | 74 comment | 2 complexity | ad5f55a8078f05be5e97e0fe446bd5e6 MD5 | raw file
  1. <?php
  2. /**
  3. * MIT License. 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. namespace Propel\Tests\Generator\Behavior\Timestampable;
  8. use Propel\Generator\Util\QuickBuilder;
  9. use Propel\Runtime\Collection\ObjectCollection;
  10. use Propel\Tests\Bookstore\Behavior\Map\Table1TableMap;
  11. use Propel\Tests\Bookstore\Behavior\Map\Table2TableMap;
  12. use Propel\Tests\Bookstore\Behavior\Table2;
  13. use Propel\Tests\Bookstore\Behavior\Table2Query;
  14. use Propel\Tests\Helpers\Bookstore\BookstoreTestBase;
  15. use TableWithoutCreatedAt;
  16. use TableWithoutUpdatedAt;
  17. /**
  18. * Tests for TimestampableBehavior class
  19. *
  20. * @author François Zaninotto
  21. *
  22. * @group database
  23. */
  24. class TimestampableBehaviorTest extends BookstoreTestBase
  25. {
  26. /**
  27. * @return void
  28. */
  29. public static function setUpBeforeClass(): void
  30. {
  31. static::$isInitialized = false;
  32. parent::setUpBeforeClass();
  33. }
  34. private function assertTimeEquals($expected, $actual, $message = '')
  35. {
  36. // accept $expected or ($expected + 1s)
  37. return $this->assertThat(
  38. $actual,
  39. $this->logicalOr(
  40. $this->equalTo($expected),
  41. $this->equalTo($expected + 1)
  42. ),
  43. $message
  44. );
  45. }
  46. /**
  47. * @return void
  48. */
  49. public function testParameters()
  50. {
  51. $table2 = Table2TableMap::getTableMap();
  52. $this->assertEquals(count($table2->getColumns()), 4, 'Timestampable adds two columns by default');
  53. $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table2', 'getCreatedAt'), 'Timestampable adds a created_at column by default');
  54. $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table2', 'getUpdatedAt'), 'Timestampable adds an updated_at column by default');
  55. $table1 = Table1TableMap::getTableMap();
  56. $this->assertEquals(count($table1->getColumns()), 4, 'Timestampable does not add two columns when they already exist');
  57. $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table1', 'getCreatedOn'), 'Timestampable allows customization of create_column name');
  58. $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table1', 'getUpdatedOn'), 'Timestampable allows customization of update_column name');
  59. }
  60. /**
  61. * @return void
  62. */
  63. public function testPreSave()
  64. {
  65. $t1 = new Table2();
  66. $this->assertNull($t1->getUpdatedAt());
  67. $t1->save();
  68. $this->assertEquals(
  69. $t1->getUpdatedAt(),
  70. $t1->getCreatedAt(),
  71. 'Timestampable sets updated_column same created_column on creation'
  72. );
  73. sleep(1);
  74. $t1->setTitle('foo');
  75. $tupdate = time();
  76. $t1->save();
  77. $this->assertTimeEquals($tupdate, $t1->getUpdatedAt('U'), 'Timestampable changes updated_column to time() on update');
  78. }
  79. /**
  80. * @return void
  81. */
  82. public function testPreSaveNoChange()
  83. {
  84. $t1 = new Table2();
  85. $this->assertNull($t1->getUpdatedAt());
  86. $tsave = time();
  87. $t1->save();
  88. $this->assertTimeEquals($tsave, $t1->getUpdatedAt('U'), 'Timestampable sets updated_column to time() on creation');
  89. sleep(1);
  90. $tupdate = time();
  91. $t1->save();
  92. $this->assertTimeEquals($tsave, $t1->getUpdatedAt('U'), 'Timestampable only changes updated_column if the object was modified');
  93. }
  94. /**
  95. * @return void
  96. */
  97. public function testPreSaveManuallyUpdated()
  98. {
  99. $t1 = new Table2();
  100. $t1->setUpdatedAt(time() - 10);
  101. $tsave = time();
  102. $t1->save();
  103. $this->assertLessThan($tsave, $t1->getUpdatedAt('U'), 'Timestampable does not set updated_column to time() on creation when it is set by the user');
  104. // tip: if I set it to time()-10 a second time, the object sees that I want to change it to the same value
  105. // and skips the update, therefore the updated_at is not in the list of modified columns,
  106. // and the behavior changes it to the current date... let's say it's an edge case
  107. $t1->setUpdatedAt(time() - 15);
  108. $tupdate = time();
  109. $t1->save();
  110. $this->assertLessThan($tupdate, $t1->getUpdatedAt('U'), 'Timestampable does not change updated_column to time() on update when it is set by the user');
  111. }
  112. /**
  113. * @return void
  114. */
  115. public function testPreInsert()
  116. {
  117. $t1 = new Table2();
  118. $this->assertNull($t1->getCreatedAt());
  119. $tsave = time();
  120. $t1->save();
  121. $this->assertTimeEquals($tsave, $t1->getCreatedAt('U'), 'Timestampable sets created_column to time() on creation');
  122. sleep(1);
  123. $t1->setTitle('foo');
  124. $tupdate = time();
  125. $t1->save();
  126. $this->assertTimeEquals($tsave, $t1->getCreatedAt('U'), 'Timestampable does not update created_column on update');
  127. }
  128. /**
  129. * @return void
  130. */
  131. public function testPreInsertManuallyUpdated()
  132. {
  133. $t1 = new Table2();
  134. $t1->setCreatedAt(time() - 10);
  135. $tsave = time();
  136. $t1->save();
  137. $this->assertLessThan($tsave, $t1->getCreatedAt('U'), 'Timestampable does not set created_column to time() on creation when it is set by the user');
  138. }
  139. /**
  140. * @return void
  141. */
  142. public function testObjectKeepUpdateDateUnchanged()
  143. {
  144. $t1 = new Table2();
  145. $t1->setUpdatedAt(time() - 10);
  146. $tsave = time();
  147. $t1->save();
  148. $this->assertLessThan($tsave, $t1->getUpdatedAt('U'));
  149. // let's save it a second time; the updated_at should be changed
  150. $t1->setTitle('foo');
  151. $tsave = time();
  152. $t1->save();
  153. $this->assertTimeEquals($tsave, $t1->getUpdatedAt('U'));
  154. // now let's do this a second time
  155. $t1 = new Table2();
  156. $t1->setUpdatedAt(time() - 10);
  157. $tsave = time();
  158. $t1->save();
  159. $this->assertLessThan($tsave, $t1->getUpdatedAt('U'));
  160. // let's save it a second time; the updated_at should be changed
  161. $t1->keepUpdateDateUnchanged();
  162. $t1->setTitle('foo');
  163. $tsave = time();
  164. $t1->save();
  165. $this->assertLessThan($tsave, $t1->getUpdatedAt('U'), 'keepUpdateDateUnchanged() prevents the behavior from updating the update date');
  166. }
  167. /**
  168. * @return void
  169. */
  170. protected function populateUpdatedAt()
  171. {
  172. Table2Query::create()->deleteAll();
  173. $ts = new ObjectCollection();
  174. $ts->setModel('\Propel\Tests\Bookstore\Behavior\Table2');
  175. for ($i = 0; $i < 10; $i++) {
  176. $t = new Table2();
  177. $t->setTitle('UpdatedAt' . $i);
  178. /* additional -30 in case the check is done in the same second (which we can't guarantee, so no assert(8 ...) below).*/
  179. $t->setUpdatedAt(time() - $i * 24 * 60 * 60 - 30);
  180. $ts[] = $t;
  181. }
  182. $ts->save();
  183. }
  184. /**
  185. * @return void
  186. */
  187. protected function populateCreatedAt()
  188. {
  189. Table2Query::create()->deleteAll();
  190. $ts = new ObjectCollection();
  191. $ts->setModel('\Propel\Tests\Bookstore\Behavior\Table2');
  192. for ($i = 0; $i < 10; $i++) {
  193. $t = new Table2();
  194. $t->setTitle('CreatedAt' . $i);
  195. $t->setCreatedAt(time() - $i * 24 * 60 * 60 - 30);
  196. $ts[] = $t;
  197. }
  198. $ts->save();
  199. }
  200. /**
  201. * @return void
  202. */
  203. public function testQueryRecentlyUpdated()
  204. {
  205. $q = Table2Query::create()->recentlyUpdated();
  206. $this->assertTrue($q instanceof Table2Query, 'recentlyUpdated() returns the current Query object');
  207. $this->populateUpdatedAt();
  208. $ts = Table2Query::create()->recentlyUpdated()->count();
  209. $this->assertEquals(7, $ts, 'recentlyUpdated() returns the elements updated in the last 7 days by default');
  210. $ts = Table2Query::create()->recentlyUpdated(5)->count();
  211. $this->assertEquals(5, $ts, 'recentlyUpdated() accepts a number of days as parameter');
  212. }
  213. /**
  214. * @return void
  215. */
  216. public function testQueryRecentlyCreated()
  217. {
  218. $q = Table2Query::create()->recentlyCreated();
  219. $this->assertTrue($q instanceof Table2Query, 'recentlyCreated() returns the current Query object');
  220. $this->populateCreatedAt();
  221. $ts = Table2Query::create()->recentlyCreated()->count();
  222. $this->assertEquals(7, $ts, 'recentlyCreated() returns the elements created in the last 7 days by default');
  223. $ts = Table2Query::create()->recentlyCreated(5)->count();
  224. $this->assertEquals(5, $ts, 'recentlyCreated() accepts a number of days as parameter');
  225. }
  226. /**
  227. * @return void
  228. */
  229. public function testQueryLastUpdatedFirst()
  230. {
  231. $q = Table2Query::create()->lastUpdatedFirst();
  232. $this->assertTrue($q instanceof Table2Query, 'lastUpdatedFirst() returns the current Query object');
  233. $this->populateUpdatedAt();
  234. $t = Table2Query::create()->lastUpdatedFirst()->findOne();
  235. $this->assertEquals('UpdatedAt0', $t->getTitle(), 'lastUpdatedFirst() returns element with most recent update date first');
  236. }
  237. /**
  238. * @return void
  239. */
  240. public function testQueryFirstUpdatedFirst()
  241. {
  242. $q = Table2Query::create()->firstUpdatedFirst();
  243. $this->assertTrue($q instanceof Table2Query, 'firstUpdatedFirst() returns the current Query object');
  244. $this->populateUpdatedAt();
  245. $t = Table2Query::create()->firstUpdatedFirst()->findOne();
  246. $this->assertEquals('UpdatedAt9', $t->getTitle(), 'firstUpdatedFirst() returns the element with oldest updated date first');
  247. }
  248. /**
  249. * @return void
  250. */
  251. public function testQueryLastCreatedFirst()
  252. {
  253. $q = Table2Query::create()->lastCreatedFirst();
  254. $this->assertTrue($q instanceof Table2Query, 'lastCreatedFirst() returns the current Query object');
  255. $this->populateCreatedAt();
  256. $t = Table2Query::create()->lastCreatedFirst()->findOne();
  257. $this->assertEquals('CreatedAt0', $t->getTitle(), 'lastCreatedFirst() returns element with most recent create date first');
  258. }
  259. /**
  260. * @return void
  261. */
  262. public function testQueryFirstCreatedFirst()
  263. {
  264. $q = Table2Query::create()->firstCreatedFirst();
  265. $this->assertTrue($q instanceof Table2Query, 'firstCreatedFirst() returns the current Query object');
  266. $this->populateCreatedAt();
  267. $t = Table2Query::create()->firstCreatedFirst()->findOne();
  268. $this->assertEquals('CreatedAt9', $t->getTitle(), 'firstCreatedFirst() returns the element with oldest create date first');
  269. }
  270. /**
  271. * @return void
  272. */
  273. public function testDisableUpdatedAt()
  274. {
  275. $schema = <<<EOF
  276. <database name="timestampable_database">
  277. <table name="table_without_updated_at">
  278. <column name="id" type="INTEGER" primaryKey="true" autoIncrement="true"/>
  279. <column name="name" type="varchar"/>
  280. <behavior name="timestampable">
  281. <parameter name="disable_updated_at" value="true"/>
  282. </behavior>
  283. </table>
  284. </database>
  285. EOF;
  286. $builder = new QuickBuilder();
  287. $builder->setSchema($schema);
  288. $builder->build();
  289. $this->assertTrue(method_exists('TableWithoutUpdatedAt', 'getCreatedAt'));
  290. $this->assertTrue(method_exists('TableWithoutUpdatedAt', 'setCreatedAt'));
  291. $this->assertFalse(method_exists('TableWithoutUpdatedAt', 'getUpdatedAt'));
  292. $this->assertFalse(method_exists('TableWithoutUpdatedAt', 'setUpdatedAt'));
  293. $obj = new TableWithoutUpdatedAt();
  294. $obj->setName('Peter');
  295. $this->assertNull($obj->getCreatedAt());
  296. $this->assertEquals(1, $obj->save());
  297. $this->assertNotNull($obj->getCreatedAt());
  298. }
  299. /**
  300. * @return void
  301. */
  302. public function testDisableCreatedAt()
  303. {
  304. $schema = <<<EOF
  305. <database name="timestampable_database">
  306. <table name="table_without_created_at">
  307. <column name="id" type="INTEGER" primaryKey="true" autoIncrement="true"/>
  308. <column name="name" type="varchar"/>
  309. <behavior name="timestampable">
  310. <parameter name="disable_created_at" value="true"/>
  311. </behavior>
  312. </table>
  313. </database>
  314. EOF;
  315. $builder = new QuickBuilder();
  316. $builder->setSchema($schema);
  317. $builder->build();
  318. $this->assertFalse(method_exists('TableWithoutCreatedAt', 'getCreatedAt'));
  319. $this->assertFalse(method_exists('TableWithoutCreatedAt', 'setCreatedAt'));
  320. $this->assertTrue(method_exists('TableWithoutCreatedAt', 'getUpdatedAt'));
  321. $this->assertTrue(method_exists('TableWithoutCreatedAt', 'setUpdatedAt'));
  322. $obj = new TableWithoutCreatedAt();
  323. $obj->setName('Peter');
  324. $this->assertNull($obj->getUpdatedAt());
  325. $this->assertEquals(1, $obj->save());
  326. $this->assertNotNull($obj->getUpdatedAt());
  327. }
  328. }