From 913c573927a0f848bd4351ba1724f66110adcf77 Mon Sep 17 00:00:00 2001 From: Steve Mareigner Date: Sat, 16 Sep 2023 12:44:17 +0200 Subject: [PATCH 1/5] feat(timestampable): Add ability to choose between DATETIME and TIMESTAMP SQL type --- .../Timestampable/TimestampableBehavior.php | 19 +- .../TimestampableBehaviorTest.php | 614 ++++++++++-------- 2 files changed, 346 insertions(+), 287 deletions(-) diff --git a/src/Propel/Generator/Behavior/Timestampable/TimestampableBehavior.php b/src/Propel/Generator/Behavior/Timestampable/TimestampableBehavior.php index 6c84c225fb..e6c4d53b3f 100644 --- a/src/Propel/Generator/Behavior/Timestampable/TimestampableBehavior.php +++ b/src/Propel/Generator/Behavior/Timestampable/TimestampableBehavior.php @@ -27,8 +27,17 @@ class TimestampableBehavior extends Behavior 'update_column' => 'updated_at', 'disable_created_at' => 'false', 'disable_updated_at' => 'false', + 'is_timestamp' => 'true', ]; + /** + * @return bool + */ + protected function isTimestamp(): bool + { + return $this->booleanValue($this->getParameter('is_timestamp')); + } + /** * @return bool */ @@ -57,13 +66,13 @@ public function modifyTable(): void if ($this->withCreatedAt() && !$table->hasColumn($this->getParameter('create_column'))) { $table->addColumn([ 'name' => $this->getParameter('create_column'), - 'type' => 'TIMESTAMP', + 'type' => $this->isTimestamp() ? 'TIMESTAMP' : 'DATETIME', ]); } if ($this->withUpdatedAt() && !$table->hasColumn($this->getParameter('update_column'))) { $table->addColumn([ 'name' => $this->getParameter('update_column'), - 'type' => 'TIMESTAMP', + 'type' => $this->isTimestamp() ? 'TIMESTAMP' : 'DATETIME', ]); } } @@ -106,7 +115,7 @@ public function preUpdate(AbstractOMBuilder $builder): string : '\\Propel\\Runtime\\Util\\PropelDateTime::createHighPrecision()'; return 'if ($this->isModified() && !$this->isColumnModified(' . $this->getColumnConstant('update_column', $builder) . ")) { - \$this->" . $this->getColumnSetter('update_column') . "(${valueSource}); + \$this->" . $this->getColumnSetter('update_column') . "($valueSource); }"; } @@ -131,7 +140,7 @@ public function preInsert(AbstractOMBuilder $builder): string : '$highPrecision'; $script .= " if (!\$this->isColumnModified(" . $this->getColumnConstant('create_column', $builder) . ")) { - \$this->" . $this->getColumnSetter('create_column') . "(${valueSource}); + \$this->" . $this->getColumnSetter('create_column') . "($valueSource); }"; } @@ -141,7 +150,7 @@ public function preInsert(AbstractOMBuilder $builder): string : '$highPrecision'; $script .= " if (!\$this->isColumnModified(" . $this->getColumnConstant('update_column', $builder) . ")) { - \$this->" . $this->getColumnSetter('update_column') . "(${valueSource}); + \$this->" . $this->getColumnSetter('update_column') . "($valueSource); }"; } diff --git a/tests/Propel/Tests/Generator/Behavior/Timestampable/TimestampableBehaviorTest.php b/tests/Propel/Tests/Generator/Behavior/Timestampable/TimestampableBehaviorTest.php index 035469a5cb..abb3533679 100644 --- a/tests/Propel/Tests/Generator/Behavior/Timestampable/TimestampableBehaviorTest.php +++ b/tests/Propel/Tests/Generator/Behavior/Timestampable/TimestampableBehaviorTest.php @@ -36,324 +36,374 @@ public static function setUpBeforeClass(): void parent::setUpBeforeClass(); } - private function assertTimeEquals($expected, $actual, $message = '') - { - // accept $expected or ($expected + 1s) - return $this->assertThat( - $actual, - $this->logicalOr( - $this->equalTo($expected), - $this->equalTo($expected + 1) - ), - $message - ); - } - - /** - * @return void - */ - public function testParameters() - { - $table2 = Table2TableMap::getTableMap(); - $this->assertEquals(count($table2->getColumns()), 4, 'Timestampable adds two columns by default'); - $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table2', 'getCreatedAt'), 'Timestampable adds a created_at column by default'); - $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table2', 'getUpdatedAt'), 'Timestampable adds an updated_at column by default'); - $table1 = Table1TableMap::getTableMap(); - $this->assertEquals(count($table1->getColumns()), 4, 'Timestampable does not add two columns when they already exist'); - $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table1', 'getCreatedOn'), 'Timestampable allows customization of create_column name'); - $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table1', 'getUpdatedOn'), 'Timestampable allows customization of update_column name'); - } - - /** - * @return void - */ - public function testPreSave() - { - $t1 = new Table2(); - $this->assertNull($t1->getUpdatedAt()); - $t1->save(); - $this->assertEquals( - $t1->getUpdatedAt(), - $t1->getCreatedAt(), - 'Timestampable sets updated_column same created_column on creation' - ); - sleep(1); - $t1->setTitle('foo'); - $tupdate = time(); - $t1->save(); - $this->assertTimeEquals($tupdate, $t1->getUpdatedAt('U'), 'Timestampable changes updated_column to time() on update'); - } - - /** - * @return void - */ - public function testPreSaveNoChange() - { - $t1 = new Table2(); - $this->assertNull($t1->getUpdatedAt()); - $tsave = time(); - $t1->save(); - $this->assertTimeEquals($tsave, $t1->getUpdatedAt('U'), 'Timestampable sets updated_column to time() on creation'); - sleep(1); - $tupdate = time(); - $t1->save(); - $this->assertTimeEquals($tsave, $t1->getUpdatedAt('U'), 'Timestampable only changes updated_column if the object was modified'); - } - - /** - * @return void - */ - public function testPreSaveManuallyUpdated() - { - $t1 = new Table2(); - $t1->setUpdatedAt(time() - 10); - $tsave = time(); - $t1->save(); - $this->assertLessThan($tsave, $t1->getUpdatedAt('U'), 'Timestampable does not set updated_column to time() on creation when it is set by the user'); - // tip: if I set it to time()-10 a second time, the object sees that I want to change it to the same value - // and skips the update, therefore the updated_at is not in the list of modified columns, - // and the behavior changes it to the current date... let's say it's an edge case - $t1->setUpdatedAt(time() - 15); - $tupdate = time(); - $t1->save(); - $this->assertLessThan($tupdate, $t1->getUpdatedAt('U'), 'Timestampable does not change updated_column to time() on update when it is set by the user'); - } - - /** - * @return void - */ - public function testPreInsert() - { - $t1 = new Table2(); - $this->assertNull($t1->getCreatedAt()); - $tsave = time(); - $t1->save(); - $this->assertTimeEquals($tsave, $t1->getCreatedAt('U'), 'Timestampable sets created_column to time() on creation'); - sleep(1); - $t1->setTitle('foo'); - $tupdate = time(); - $t1->save(); - $this->assertTimeEquals($tsave, $t1->getCreatedAt('U'), 'Timestampable does not update created_column on update'); - } - - /** - * @return void - */ - public function testPreInsertManuallyUpdated() - { - $t1 = new Table2(); - $t1->setCreatedAt(time() - 10); - $tsave = time(); - $t1->save(); - $this->assertLessThan($tsave, $t1->getCreatedAt('U'), 'Timestampable does not set created_column to time() on creation when it is set by the user'); - } +// private function assertTimeEquals($expected, $actual, $message = '') +// { +// // accept $expected or ($expected + 1s) +// return $this->assertThat( +// $actual, +// $this->logicalOr( +// $this->equalTo($expected), +// $this->equalTo($expected + 1) +// ), +// $message +// ); +// } +// +// /** +// * @return void +// */ +// public function testParameters() +// { +// $table2 = Table2TableMap::getTableMap(); +// $this->assertEquals(count($table2->getColumns()), 4, 'Timestampable adds two columns by default'); +// $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table2', 'getCreatedAt'), 'Timestampable adds a created_at column by default'); +// $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table2', 'getUpdatedAt'), 'Timestampable adds an updated_at column by default'); +// $table1 = Table1TableMap::getTableMap(); +// $this->assertEquals(count($table1->getColumns()), 4, 'Timestampable does not add two columns when they already exist'); +// $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table1', 'getCreatedOn'), 'Timestampable allows customization of create_column name'); +// $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table1', 'getUpdatedOn'), 'Timestampable allows customization of update_column name'); +// } +// +// /** +// * @return void +// */ +// public function testPreSave() +// { +// $t1 = new Table2(); +// $this->assertNull($t1->getUpdatedAt()); +// $t1->save(); +// $this->assertEquals( +// $t1->getUpdatedAt(), +// $t1->getCreatedAt(), +// 'Timestampable sets updated_column same created_column on creation' +// ); +// sleep(1); +// $t1->setTitle('foo'); +// $tupdate = time(); +// $t1->save(); +// $this->assertTimeEquals($tupdate, $t1->getUpdatedAt('U'), 'Timestampable changes updated_column to time() on update'); +// } +// +// /** +// * @return void +// */ +// public function testPreSaveNoChange() +// { +// $t1 = new Table2(); +// $this->assertNull($t1->getUpdatedAt()); +// $tsave = time(); +// $t1->save(); +// $this->assertTimeEquals($tsave, $t1->getUpdatedAt('U'), 'Timestampable sets updated_column to time() on creation'); +// sleep(1); +// $tupdate = time(); +// $t1->save(); +// $this->assertTimeEquals($tsave, $t1->getUpdatedAt('U'), 'Timestampable only changes updated_column if the object was modified'); +// } +// +// /** +// * @return void +// */ +// public function testPreSaveManuallyUpdated() +// { +// $t1 = new Table2(); +// $t1->setUpdatedAt(time() - 10); +// $tsave = time(); +// $t1->save(); +// $this->assertLessThan($tsave, $t1->getUpdatedAt('U'), 'Timestampable does not set updated_column to time() on creation when it is set by the user'); +// // tip: if I set it to time()-10 a second time, the object sees that I want to change it to the same value +// // and skips the update, therefore the updated_at is not in the list of modified columns, +// // and the behavior changes it to the current date... let's say it's an edge case +// $t1->setUpdatedAt(time() - 15); +// $tupdate = time(); +// $t1->save(); +// $this->assertLessThan($tupdate, $t1->getUpdatedAt('U'), 'Timestampable does not change updated_column to time() on update when it is set by the user'); +// } +// +// /** +// * @return void +// */ +// public function testPreInsert() +// { +// $t1 = new Table2(); +// $this->assertNull($t1->getCreatedAt()); +// $tsave = time(); +// $t1->save(); +// $this->assertTimeEquals($tsave, $t1->getCreatedAt('U'), 'Timestampable sets created_column to time() on creation'); +// sleep(1); +// $t1->setTitle('foo'); +// $tupdate = time(); +// $t1->save(); +// $this->assertTimeEquals($tsave, $t1->getCreatedAt('U'), 'Timestampable does not update created_column on update'); +// } +// +// /** +// * @return void +// */ +// public function testPreInsertManuallyUpdated() +// { +// $t1 = new Table2(); +// $t1->setCreatedAt(time() - 10); +// $tsave = time(); +// $t1->save(); +// $this->assertLessThan($tsave, $t1->getCreatedAt('U'), 'Timestampable does not set created_column to time() on creation when it is set by the user'); +// } +// +// /** +// * @return void +// */ +// public function testObjectKeepUpdateDateUnchanged() +// { +// $t1 = new Table2(); +// $t1->setUpdatedAt(time() - 10); +// $tsave = time(); +// $t1->save(); +// $this->assertLessThan($tsave, $t1->getUpdatedAt('U')); +// // let's save it a second time; the updated_at should be changed +// $t1->setTitle('foo'); +// $tsave = time(); +// $t1->save(); +// $this->assertTimeEquals($tsave, $t1->getUpdatedAt('U')); +// +// // now let's do this a second time +// $t1 = new Table2(); +// $t1->setUpdatedAt(time() - 10); +// $tsave = time(); +// $t1->save(); +// $this->assertLessThan($tsave, $t1->getUpdatedAt('U')); +// // let's save it a second time; the updated_at should be changed +// $t1->keepUpdateDateUnchanged(); +// $t1->setTitle('foo'); +// $tsave = time(); +// $t1->save(); +// $this->assertLessThan($tsave, $t1->getUpdatedAt('U'), 'keepUpdateDateUnchanged() prevents the behavior from updating the update date'); +// } +// +// /** +// * @return void +// */ +// protected function populateUpdatedAt() +// { +// Table2Query::create()->deleteAll(); +// $ts = new ObjectCollection(); +// $ts->setModel('\Propel\Tests\Bookstore\Behavior\Table2'); +// for ($i = 0; $i < 10; $i++) { +// $t = new Table2(); +// $t->setTitle('UpdatedAt' . $i); +// /* additional -30 in case the check is done in the same second (which we can't guarantee, so no assert(8 ...) below).*/ +// $t->setUpdatedAt(time() - $i * 24 * 60 * 60 - 30); +// $ts[] = $t; +// } +// $ts->save(); +// } +// +// /** +// * @return void +// */ +// protected function populateCreatedAt() +// { +// Table2Query::create()->deleteAll(); +// $ts = new ObjectCollection(); +// $ts->setModel('\Propel\Tests\Bookstore\Behavior\Table2'); +// for ($i = 0; $i < 10; $i++) { +// $t = new Table2(); +// $t->setTitle('CreatedAt' . $i); +// $t->setCreatedAt(time() - $i * 24 * 60 * 60 - 30); +// $ts[] = $t; +// } +// $ts->save(); +// } +// +// /** +// * @return void +// */ +// public function testQueryRecentlyUpdated() +// { +// $q = Table2Query::create()->recentlyUpdated(); +// $this->assertTrue($q instanceof Table2Query, 'recentlyUpdated() returns the current Query object'); +// $this->populateUpdatedAt(); +// $ts = Table2Query::create()->recentlyUpdated()->count(); +// $this->assertEquals(7, $ts, 'recentlyUpdated() returns the elements updated in the last 7 days by default'); +// $ts = Table2Query::create()->recentlyUpdated(5)->count(); +// $this->assertEquals(5, $ts, 'recentlyUpdated() accepts a number of days as parameter'); +// } +// +// /** +// * @return void +// */ +// public function testQueryRecentlyCreated() +// { +// $q = Table2Query::create()->recentlyCreated(); +// $this->assertTrue($q instanceof Table2Query, 'recentlyCreated() returns the current Query object'); +// $this->populateCreatedAt(); +// $ts = Table2Query::create()->recentlyCreated()->count(); +// $this->assertEquals(7, $ts, 'recentlyCreated() returns the elements created in the last 7 days by default'); +// $ts = Table2Query::create()->recentlyCreated(5)->count(); +// $this->assertEquals(5, $ts, 'recentlyCreated() accepts a number of days as parameter'); +// } +// +// /** +// * @return void +// */ +// public function testQueryLastUpdatedFirst() +// { +// $q = Table2Query::create()->lastUpdatedFirst(); +// $this->assertTrue($q instanceof Table2Query, 'lastUpdatedFirst() returns the current Query object'); +// $this->populateUpdatedAt(); +// $t = Table2Query::create()->lastUpdatedFirst()->findOne(); +// $this->assertEquals('UpdatedAt0', $t->getTitle(), 'lastUpdatedFirst() returns element with most recent update date first'); +// } +// +// /** +// * @return void +// */ +// public function testQueryFirstUpdatedFirst() +// { +// $q = Table2Query::create()->firstUpdatedFirst(); +// $this->assertTrue($q instanceof Table2Query, 'firstUpdatedFirst() returns the current Query object'); +// $this->populateUpdatedAt(); +// $t = Table2Query::create()->firstUpdatedFirst()->findOne(); +// $this->assertEquals('UpdatedAt9', $t->getTitle(), 'firstUpdatedFirst() returns the element with oldest updated date first'); +// } +// +// /** +// * @return void +// */ +// public function testQueryLastCreatedFirst() +// { +// $q = Table2Query::create()->lastCreatedFirst(); +// $this->assertTrue($q instanceof Table2Query, 'lastCreatedFirst() returns the current Query object'); +// $this->populateCreatedAt(); +// $t = Table2Query::create()->lastCreatedFirst()->findOne(); +// $this->assertEquals('CreatedAt0', $t->getTitle(), 'lastCreatedFirst() returns element with most recent create date first'); +// } +// +// /** +// * @return void +// */ +// public function testQueryFirstCreatedFirst() +// { +// $q = Table2Query::create()->firstCreatedFirst(); +// $this->assertTrue($q instanceof Table2Query, 'firstCreatedFirst() returns the current Query object'); +// $this->populateCreatedAt(); +// $t = Table2Query::create()->firstCreatedFirst()->findOne(); +// $this->assertEquals('CreatedAt9', $t->getTitle(), 'firstCreatedFirst() returns the element with oldest create date first'); +// } +// +// /** +// * @return void +// */ +// public function testDisableUpdatedAt() +// { +// $schema = << +// +// +// +// +// +// +// +//
+// +//EOF; +// +// $builder = new QuickBuilder(); +// $builder->setSchema($schema); +// $builder->build(); +// +// $this->assertTrue(method_exists('TableWithoutUpdatedAt', 'getCreatedAt')); +// $this->assertTrue(method_exists('TableWithoutUpdatedAt', 'setCreatedAt')); +// $this->assertFalse(method_exists('TableWithoutUpdatedAt', 'getUpdatedAt')); +// $this->assertFalse(method_exists('TableWithoutUpdatedAt', 'setUpdatedAt')); +// +// $obj = new TableWithoutUpdatedAt(); +// $obj->setName('Peter'); +// $this->assertNull($obj->getCreatedAt()); +// $this->assertEquals(1, $obj->save()); +// $this->assertNotNull($obj->getCreatedAt()); +// } +// +// /** +// * @return void +// */ +// public function testDisableCreatedAt() +// { +// $schema = << +// +// +// +// +// +// +// +//
+// +//EOF; +// +// $builder = new QuickBuilder(); +// $builder->setSchema($schema); +// $builder->build(); +// +// $this->assertFalse(method_exists('TableWithoutCreatedAt', 'getCreatedAt')); +// $this->assertFalse(method_exists('TableWithoutCreatedAt', 'setCreatedAt')); +// $this->assertTrue(method_exists('TableWithoutCreatedAt', 'getUpdatedAt')); +// $this->assertTrue(method_exists('TableWithoutCreatedAt', 'setUpdatedAt')); +// +// $obj = new TableWithoutCreatedAt(); +// $obj->setName('Peter'); +// $this->assertNull($obj->getUpdatedAt()); +// $this->assertEquals(1, $obj->save()); +// $this->assertNotNull($obj->getUpdatedAt()); +// } /** * @return void */ - public function testObjectKeepUpdateDateUnchanged() - { - $t1 = new Table2(); - $t1->setUpdatedAt(time() - 10); - $tsave = time(); - $t1->save(); - $this->assertLessThan($tsave, $t1->getUpdatedAt('U')); - // let's save it a second time; the updated_at should be changed - $t1->setTitle('foo'); - $tsave = time(); - $t1->save(); - $this->assertTimeEquals($tsave, $t1->getUpdatedAt('U')); - - // now let's do this a second time - $t1 = new Table2(); - $t1->setUpdatedAt(time() - 10); - $tsave = time(); - $t1->save(); - $this->assertLessThan($tsave, $t1->getUpdatedAt('U')); - // let's save it a second time; the updated_at should be changed - $t1->keepUpdateDateUnchanged(); - $t1->setTitle('foo'); - $tsave = time(); - $t1->save(); - $this->assertLessThan($tsave, $t1->getUpdatedAt('U'), 'keepUpdateDateUnchanged() prevents the behavior from updating the update date'); - } - - /** - * @return void - */ - protected function populateUpdatedAt() - { - Table2Query::create()->deleteAll(); - $ts = new ObjectCollection(); - $ts->setModel('\Propel\Tests\Bookstore\Behavior\Table2'); - for ($i = 0; $i < 10; $i++) { - $t = new Table2(); - $t->setTitle('UpdatedAt' . $i); - /* additional -30 in case the check is done in the same second (which we can't guarantee, so no assert(8 ...) below).*/ - $t->setUpdatedAt(time() - $i * 24 * 60 * 60 - 30); - $ts[] = $t; - } - $ts->save(); - } - - /** - * @return void - */ - protected function populateCreatedAt() - { - Table2Query::create()->deleteAll(); - $ts = new ObjectCollection(); - $ts->setModel('\Propel\Tests\Bookstore\Behavior\Table2'); - for ($i = 0; $i < 10; $i++) { - $t = new Table2(); - $t->setTitle('CreatedAt' . $i); - $t->setCreatedAt(time() - $i * 24 * 60 * 60 - 30); - $ts[] = $t; - } - $ts->save(); - } - - /** - * @return void - */ - public function testQueryRecentlyUpdated() - { - $q = Table2Query::create()->recentlyUpdated(); - $this->assertTrue($q instanceof Table2Query, 'recentlyUpdated() returns the current Query object'); - $this->populateUpdatedAt(); - $ts = Table2Query::create()->recentlyUpdated()->count(); - $this->assertEquals(7, $ts, 'recentlyUpdated() returns the elements updated in the last 7 days by default'); - $ts = Table2Query::create()->recentlyUpdated(5)->count(); - $this->assertEquals(5, $ts, 'recentlyUpdated() accepts a number of days as parameter'); - } - - /** - * @return void - */ - public function testQueryRecentlyCreated() - { - $q = Table2Query::create()->recentlyCreated(); - $this->assertTrue($q instanceof Table2Query, 'recentlyCreated() returns the current Query object'); - $this->populateCreatedAt(); - $ts = Table2Query::create()->recentlyCreated()->count(); - $this->assertEquals(7, $ts, 'recentlyCreated() returns the elements created in the last 7 days by default'); - $ts = Table2Query::create()->recentlyCreated(5)->count(); - $this->assertEquals(5, $ts, 'recentlyCreated() accepts a number of days as parameter'); - } - - /** - * @return void - */ - public function testQueryLastUpdatedFirst() - { - $q = Table2Query::create()->lastUpdatedFirst(); - $this->assertTrue($q instanceof Table2Query, 'lastUpdatedFirst() returns the current Query object'); - $this->populateUpdatedAt(); - $t = Table2Query::create()->lastUpdatedFirst()->findOne(); - $this->assertEquals('UpdatedAt0', $t->getTitle(), 'lastUpdatedFirst() returns element with most recent update date first'); - } - - /** - * @return void - */ - public function testQueryFirstUpdatedFirst() - { - $q = Table2Query::create()->firstUpdatedFirst(); - $this->assertTrue($q instanceof Table2Query, 'firstUpdatedFirst() returns the current Query object'); - $this->populateUpdatedAt(); - $t = Table2Query::create()->firstUpdatedFirst()->findOne(); - $this->assertEquals('UpdatedAt9', $t->getTitle(), 'firstUpdatedFirst() returns the element with oldest updated date first'); - } - - /** - * @return void - */ - public function testQueryLastCreatedFirst() - { - $q = Table2Query::create()->lastCreatedFirst(); - $this->assertTrue($q instanceof Table2Query, 'lastCreatedFirst() returns the current Query object'); - $this->populateCreatedAt(); - $t = Table2Query::create()->lastCreatedFirst()->findOne(); - $this->assertEquals('CreatedAt0', $t->getTitle(), 'lastCreatedFirst() returns element with most recent create date first'); - } - - /** - * @return void - */ - public function testQueryFirstCreatedFirst() - { - $q = Table2Query::create()->firstCreatedFirst(); - $this->assertTrue($q instanceof Table2Query, 'firstCreatedFirst() returns the current Query object'); - $this->populateCreatedAt(); - $t = Table2Query::create()->firstCreatedFirst()->findOne(); - $this->assertEquals('CreatedAt9', $t->getTitle(), 'firstCreatedFirst() returns the element with oldest create date first'); - } - - /** - * @return void - */ - public function testDisableUpdatedAt() + public function testWithDatetimeSqlType() { $schema = << - +
- +
EOF; - $builder = new QuickBuilder(); $builder->setSchema($schema); $builder->build(); - - $this->assertTrue(method_exists('TableWithoutUpdatedAt', 'getCreatedAt')); - $this->assertTrue(method_exists('TableWithoutUpdatedAt', 'setCreatedAt')); - $this->assertFalse(method_exists('TableWithoutUpdatedAt', 'getUpdatedAt')); - $this->assertFalse(method_exists('TableWithoutUpdatedAt', 'setUpdatedAt')); - - $obj = new TableWithoutUpdatedAt(); - $obj->setName('Peter'); - $this->assertNull($obj->getCreatedAt()); - $this->assertEquals(1, $obj->save()); - $this->assertNotNull($obj->getCreatedAt()); + $sql = $builder->getSQL(); + $this->assertStringContainsString('created_at DATETIME,', $sql, 'created_at column should be DATETIME'); + $this->assertStringContainsString('updated_at DATETIME,', $sql, 'updated_at column should be DATETIME'); } /** * @return void */ - public function testDisableCreatedAt() + public function testWithTimestampSqlType() { $schema = << - +
- +
EOF; - $builder = new QuickBuilder(); $builder->setSchema($schema); $builder->build(); - - $this->assertFalse(method_exists('TableWithoutCreatedAt', 'getCreatedAt')); - $this->assertFalse(method_exists('TableWithoutCreatedAt', 'setCreatedAt')); - $this->assertTrue(method_exists('TableWithoutCreatedAt', 'getUpdatedAt')); - $this->assertTrue(method_exists('TableWithoutCreatedAt', 'setUpdatedAt')); - - $obj = new TableWithoutCreatedAt(); - $obj->setName('Peter'); - $this->assertNull($obj->getUpdatedAt()); - $this->assertEquals(1, $obj->save()); - $this->assertNotNull($obj->getUpdatedAt()); + $sql = $builder->getSQL(); + $this->assertStringContainsString('created_at TIMESTAMP,', $sql, 'created_at column should be TIMESTAMP'); + $this->assertStringContainsString('updated_at TIMESTAMP,', $sql, 'updated_at column should be TIMESTAMP'); } } From fc657abe5a658b70d0978f9f42f5c6e4723733c5 Mon Sep 17 00:00:00 2001 From: Steve Mareigner Date: Mon, 18 Sep 2023 09:45:12 +0200 Subject: [PATCH 2/5] feat(timestampable): Add ability to choose between DATETIME and TIMESTAMP SQL type --- .../TimestampableBehaviorTest.php | 640 +++++++++--------- 1 file changed, 320 insertions(+), 320 deletions(-) diff --git a/tests/Propel/Tests/Generator/Behavior/Timestampable/TimestampableBehaviorTest.php b/tests/Propel/Tests/Generator/Behavior/Timestampable/TimestampableBehaviorTest.php index abb3533679..ca976e4f87 100644 --- a/tests/Propel/Tests/Generator/Behavior/Timestampable/TimestampableBehaviorTest.php +++ b/tests/Propel/Tests/Generator/Behavior/Timestampable/TimestampableBehaviorTest.php @@ -36,326 +36,326 @@ public static function setUpBeforeClass(): void parent::setUpBeforeClass(); } -// private function assertTimeEquals($expected, $actual, $message = '') -// { -// // accept $expected or ($expected + 1s) -// return $this->assertThat( -// $actual, -// $this->logicalOr( -// $this->equalTo($expected), -// $this->equalTo($expected + 1) -// ), -// $message -// ); -// } -// -// /** -// * @return void -// */ -// public function testParameters() -// { -// $table2 = Table2TableMap::getTableMap(); -// $this->assertEquals(count($table2->getColumns()), 4, 'Timestampable adds two columns by default'); -// $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table2', 'getCreatedAt'), 'Timestampable adds a created_at column by default'); -// $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table2', 'getUpdatedAt'), 'Timestampable adds an updated_at column by default'); -// $table1 = Table1TableMap::getTableMap(); -// $this->assertEquals(count($table1->getColumns()), 4, 'Timestampable does not add two columns when they already exist'); -// $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table1', 'getCreatedOn'), 'Timestampable allows customization of create_column name'); -// $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table1', 'getUpdatedOn'), 'Timestampable allows customization of update_column name'); -// } -// -// /** -// * @return void -// */ -// public function testPreSave() -// { -// $t1 = new Table2(); -// $this->assertNull($t1->getUpdatedAt()); -// $t1->save(); -// $this->assertEquals( -// $t1->getUpdatedAt(), -// $t1->getCreatedAt(), -// 'Timestampable sets updated_column same created_column on creation' -// ); -// sleep(1); -// $t1->setTitle('foo'); -// $tupdate = time(); -// $t1->save(); -// $this->assertTimeEquals($tupdate, $t1->getUpdatedAt('U'), 'Timestampable changes updated_column to time() on update'); -// } -// -// /** -// * @return void -// */ -// public function testPreSaveNoChange() -// { -// $t1 = new Table2(); -// $this->assertNull($t1->getUpdatedAt()); -// $tsave = time(); -// $t1->save(); -// $this->assertTimeEquals($tsave, $t1->getUpdatedAt('U'), 'Timestampable sets updated_column to time() on creation'); -// sleep(1); -// $tupdate = time(); -// $t1->save(); -// $this->assertTimeEquals($tsave, $t1->getUpdatedAt('U'), 'Timestampable only changes updated_column if the object was modified'); -// } -// -// /** -// * @return void -// */ -// public function testPreSaveManuallyUpdated() -// { -// $t1 = new Table2(); -// $t1->setUpdatedAt(time() - 10); -// $tsave = time(); -// $t1->save(); -// $this->assertLessThan($tsave, $t1->getUpdatedAt('U'), 'Timestampable does not set updated_column to time() on creation when it is set by the user'); -// // tip: if I set it to time()-10 a second time, the object sees that I want to change it to the same value -// // and skips the update, therefore the updated_at is not in the list of modified columns, -// // and the behavior changes it to the current date... let's say it's an edge case -// $t1->setUpdatedAt(time() - 15); -// $tupdate = time(); -// $t1->save(); -// $this->assertLessThan($tupdate, $t1->getUpdatedAt('U'), 'Timestampable does not change updated_column to time() on update when it is set by the user'); -// } -// -// /** -// * @return void -// */ -// public function testPreInsert() -// { -// $t1 = new Table2(); -// $this->assertNull($t1->getCreatedAt()); -// $tsave = time(); -// $t1->save(); -// $this->assertTimeEquals($tsave, $t1->getCreatedAt('U'), 'Timestampable sets created_column to time() on creation'); -// sleep(1); -// $t1->setTitle('foo'); -// $tupdate = time(); -// $t1->save(); -// $this->assertTimeEquals($tsave, $t1->getCreatedAt('U'), 'Timestampable does not update created_column on update'); -// } -// -// /** -// * @return void -// */ -// public function testPreInsertManuallyUpdated() -// { -// $t1 = new Table2(); -// $t1->setCreatedAt(time() - 10); -// $tsave = time(); -// $t1->save(); -// $this->assertLessThan($tsave, $t1->getCreatedAt('U'), 'Timestampable does not set created_column to time() on creation when it is set by the user'); -// } -// -// /** -// * @return void -// */ -// public function testObjectKeepUpdateDateUnchanged() -// { -// $t1 = new Table2(); -// $t1->setUpdatedAt(time() - 10); -// $tsave = time(); -// $t1->save(); -// $this->assertLessThan($tsave, $t1->getUpdatedAt('U')); -// // let's save it a second time; the updated_at should be changed -// $t1->setTitle('foo'); -// $tsave = time(); -// $t1->save(); -// $this->assertTimeEquals($tsave, $t1->getUpdatedAt('U')); -// -// // now let's do this a second time -// $t1 = new Table2(); -// $t1->setUpdatedAt(time() - 10); -// $tsave = time(); -// $t1->save(); -// $this->assertLessThan($tsave, $t1->getUpdatedAt('U')); -// // let's save it a second time; the updated_at should be changed -// $t1->keepUpdateDateUnchanged(); -// $t1->setTitle('foo'); -// $tsave = time(); -// $t1->save(); -// $this->assertLessThan($tsave, $t1->getUpdatedAt('U'), 'keepUpdateDateUnchanged() prevents the behavior from updating the update date'); -// } -// -// /** -// * @return void -// */ -// protected function populateUpdatedAt() -// { -// Table2Query::create()->deleteAll(); -// $ts = new ObjectCollection(); -// $ts->setModel('\Propel\Tests\Bookstore\Behavior\Table2'); -// for ($i = 0; $i < 10; $i++) { -// $t = new Table2(); -// $t->setTitle('UpdatedAt' . $i); -// /* additional -30 in case the check is done in the same second (which we can't guarantee, so no assert(8 ...) below).*/ -// $t->setUpdatedAt(time() - $i * 24 * 60 * 60 - 30); -// $ts[] = $t; -// } -// $ts->save(); -// } -// -// /** -// * @return void -// */ -// protected function populateCreatedAt() -// { -// Table2Query::create()->deleteAll(); -// $ts = new ObjectCollection(); -// $ts->setModel('\Propel\Tests\Bookstore\Behavior\Table2'); -// for ($i = 0; $i < 10; $i++) { -// $t = new Table2(); -// $t->setTitle('CreatedAt' . $i); -// $t->setCreatedAt(time() - $i * 24 * 60 * 60 - 30); -// $ts[] = $t; -// } -// $ts->save(); -// } -// -// /** -// * @return void -// */ -// public function testQueryRecentlyUpdated() -// { -// $q = Table2Query::create()->recentlyUpdated(); -// $this->assertTrue($q instanceof Table2Query, 'recentlyUpdated() returns the current Query object'); -// $this->populateUpdatedAt(); -// $ts = Table2Query::create()->recentlyUpdated()->count(); -// $this->assertEquals(7, $ts, 'recentlyUpdated() returns the elements updated in the last 7 days by default'); -// $ts = Table2Query::create()->recentlyUpdated(5)->count(); -// $this->assertEquals(5, $ts, 'recentlyUpdated() accepts a number of days as parameter'); -// } -// -// /** -// * @return void -// */ -// public function testQueryRecentlyCreated() -// { -// $q = Table2Query::create()->recentlyCreated(); -// $this->assertTrue($q instanceof Table2Query, 'recentlyCreated() returns the current Query object'); -// $this->populateCreatedAt(); -// $ts = Table2Query::create()->recentlyCreated()->count(); -// $this->assertEquals(7, $ts, 'recentlyCreated() returns the elements created in the last 7 days by default'); -// $ts = Table2Query::create()->recentlyCreated(5)->count(); -// $this->assertEquals(5, $ts, 'recentlyCreated() accepts a number of days as parameter'); -// } -// -// /** -// * @return void -// */ -// public function testQueryLastUpdatedFirst() -// { -// $q = Table2Query::create()->lastUpdatedFirst(); -// $this->assertTrue($q instanceof Table2Query, 'lastUpdatedFirst() returns the current Query object'); -// $this->populateUpdatedAt(); -// $t = Table2Query::create()->lastUpdatedFirst()->findOne(); -// $this->assertEquals('UpdatedAt0', $t->getTitle(), 'lastUpdatedFirst() returns element with most recent update date first'); -// } -// -// /** -// * @return void -// */ -// public function testQueryFirstUpdatedFirst() -// { -// $q = Table2Query::create()->firstUpdatedFirst(); -// $this->assertTrue($q instanceof Table2Query, 'firstUpdatedFirst() returns the current Query object'); -// $this->populateUpdatedAt(); -// $t = Table2Query::create()->firstUpdatedFirst()->findOne(); -// $this->assertEquals('UpdatedAt9', $t->getTitle(), 'firstUpdatedFirst() returns the element with oldest updated date first'); -// } -// -// /** -// * @return void -// */ -// public function testQueryLastCreatedFirst() -// { -// $q = Table2Query::create()->lastCreatedFirst(); -// $this->assertTrue($q instanceof Table2Query, 'lastCreatedFirst() returns the current Query object'); -// $this->populateCreatedAt(); -// $t = Table2Query::create()->lastCreatedFirst()->findOne(); -// $this->assertEquals('CreatedAt0', $t->getTitle(), 'lastCreatedFirst() returns element with most recent create date first'); -// } -// -// /** -// * @return void -// */ -// public function testQueryFirstCreatedFirst() -// { -// $q = Table2Query::create()->firstCreatedFirst(); -// $this->assertTrue($q instanceof Table2Query, 'firstCreatedFirst() returns the current Query object'); -// $this->populateCreatedAt(); -// $t = Table2Query::create()->firstCreatedFirst()->findOne(); -// $this->assertEquals('CreatedAt9', $t->getTitle(), 'firstCreatedFirst() returns the element with oldest create date first'); -// } -// -// /** -// * @return void -// */ -// public function testDisableUpdatedAt() -// { -// $schema = << -// -// -// -// -// -// -// -//
-// -//EOF; -// -// $builder = new QuickBuilder(); -// $builder->setSchema($schema); -// $builder->build(); -// -// $this->assertTrue(method_exists('TableWithoutUpdatedAt', 'getCreatedAt')); -// $this->assertTrue(method_exists('TableWithoutUpdatedAt', 'setCreatedAt')); -// $this->assertFalse(method_exists('TableWithoutUpdatedAt', 'getUpdatedAt')); -// $this->assertFalse(method_exists('TableWithoutUpdatedAt', 'setUpdatedAt')); -// -// $obj = new TableWithoutUpdatedAt(); -// $obj->setName('Peter'); -// $this->assertNull($obj->getCreatedAt()); -// $this->assertEquals(1, $obj->save()); -// $this->assertNotNull($obj->getCreatedAt()); -// } -// -// /** -// * @return void -// */ -// public function testDisableCreatedAt() -// { -// $schema = << -// -// -// -// -// -// -// -//
-// -//EOF; -// -// $builder = new QuickBuilder(); -// $builder->setSchema($schema); -// $builder->build(); -// -// $this->assertFalse(method_exists('TableWithoutCreatedAt', 'getCreatedAt')); -// $this->assertFalse(method_exists('TableWithoutCreatedAt', 'setCreatedAt')); -// $this->assertTrue(method_exists('TableWithoutCreatedAt', 'getUpdatedAt')); -// $this->assertTrue(method_exists('TableWithoutCreatedAt', 'setUpdatedAt')); -// -// $obj = new TableWithoutCreatedAt(); -// $obj->setName('Peter'); -// $this->assertNull($obj->getUpdatedAt()); -// $this->assertEquals(1, $obj->save()); -// $this->assertNotNull($obj->getUpdatedAt()); -// } + private function assertTimeEquals($expected, $actual, $message = '') + { + // accept $expected or ($expected + 1s) + return $this->assertThat( + $actual, + $this->logicalOr( + $this->equalTo($expected), + $this->equalTo($expected + 1) + ), + $message + ); + } + + /** + * @return void + */ + public function testParameters() + { + $table2 = Table2TableMap::getTableMap(); + $this->assertEquals(count($table2->getColumns()), 4, 'Timestampable adds two columns by default'); + $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table2', 'getCreatedAt'), 'Timestampable adds a created_at column by default'); + $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table2', 'getUpdatedAt'), 'Timestampable adds an updated_at column by default'); + $table1 = Table1TableMap::getTableMap(); + $this->assertEquals(count($table1->getColumns()), 4, 'Timestampable does not add two columns when they already exist'); + $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table1', 'getCreatedOn'), 'Timestampable allows customization of create_column name'); + $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table1', 'getUpdatedOn'), 'Timestampable allows customization of update_column name'); + } + + /** + * @return void + */ + public function testPreSave() + { + $t1 = new Table2(); + $this->assertNull($t1->getUpdatedAt()); + $t1->save(); + $this->assertEquals( + $t1->getUpdatedAt(), + $t1->getCreatedAt(), + 'Timestampable sets updated_column same created_column on creation' + ); + sleep(1); + $t1->setTitle('foo'); + $tupdate = time(); + $t1->save(); + $this->assertTimeEquals($tupdate, $t1->getUpdatedAt('U'), 'Timestampable changes updated_column to time() on update'); + } + + /** + * @return void + */ + public function testPreSaveNoChange() + { + $t1 = new Table2(); + $this->assertNull($t1->getUpdatedAt()); + $tsave = time(); + $t1->save(); + $this->assertTimeEquals($tsave, $t1->getUpdatedAt('U'), 'Timestampable sets updated_column to time() on creation'); + sleep(1); + $tupdate = time(); + $t1->save(); + $this->assertTimeEquals($tsave, $t1->getUpdatedAt('U'), 'Timestampable only changes updated_column if the object was modified'); + } + + /** + * @return void + */ + public function testPreSaveManuallyUpdated() + { + $t1 = new Table2(); + $t1->setUpdatedAt(time() - 10); + $tsave = time(); + $t1->save(); + $this->assertLessThan($tsave, $t1->getUpdatedAt('U'), 'Timestampable does not set updated_column to time() on creation when it is set by the user'); + // tip: if I set it to time()-10 a second time, the object sees that I want to change it to the same value + // and skips the update, therefore the updated_at is not in the list of modified columns, + // and the behavior changes it to the current date... let's say it's an edge case + $t1->setUpdatedAt(time() - 15); + $tupdate = time(); + $t1->save(); + $this->assertLessThan($tupdate, $t1->getUpdatedAt('U'), 'Timestampable does not change updated_column to time() on update when it is set by the user'); + } + + /** + * @return void + */ + public function testPreInsert() + { + $t1 = new Table2(); + $this->assertNull($t1->getCreatedAt()); + $tsave = time(); + $t1->save(); + $this->assertTimeEquals($tsave, $t1->getCreatedAt('U'), 'Timestampable sets created_column to time() on creation'); + sleep(1); + $t1->setTitle('foo'); + $tupdate = time(); + $t1->save(); + $this->assertTimeEquals($tsave, $t1->getCreatedAt('U'), 'Timestampable does not update created_column on update'); + } + + /** + * @return void + */ + public function testPreInsertManuallyUpdated() + { + $t1 = new Table2(); + $t1->setCreatedAt(time() - 10); + $tsave = time(); + $t1->save(); + $this->assertLessThan($tsave, $t1->getCreatedAt('U'), 'Timestampable does not set created_column to time() on creation when it is set by the user'); + } + + /** + * @return void + */ + public function testObjectKeepUpdateDateUnchanged() + { + $t1 = new Table2(); + $t1->setUpdatedAt(time() - 10); + $tsave = time(); + $t1->save(); + $this->assertLessThan($tsave, $t1->getUpdatedAt('U')); + // let's save it a second time; the updated_at should be changed + $t1->setTitle('foo'); + $tsave = time(); + $t1->save(); + $this->assertTimeEquals($tsave, $t1->getUpdatedAt('U')); + + // now let's do this a second time + $t1 = new Table2(); + $t1->setUpdatedAt(time() - 10); + $tsave = time(); + $t1->save(); + $this->assertLessThan($tsave, $t1->getUpdatedAt('U')); + // let's save it a second time; the updated_at should be changed + $t1->keepUpdateDateUnchanged(); + $t1->setTitle('foo'); + $tsave = time(); + $t1->save(); + $this->assertLessThan($tsave, $t1->getUpdatedAt('U'), 'keepUpdateDateUnchanged() prevents the behavior from updating the update date'); + } + + /** + * @return void + */ + protected function populateUpdatedAt() + { + Table2Query::create()->deleteAll(); + $ts = new ObjectCollection(); + $ts->setModel('\Propel\Tests\Bookstore\Behavior\Table2'); + for ($i = 0; $i < 10; $i++) { + $t = new Table2(); + $t->setTitle('UpdatedAt' . $i); + /* additional -30 in case the check is done in the same second (which we can't guarantee, so no assert(8 ...) below).*/ + $t->setUpdatedAt(time() - $i * 24 * 60 * 60 - 30); + $ts[] = $t; + } + $ts->save(); + } + + /** + * @return void + */ + protected function populateCreatedAt() + { + Table2Query::create()->deleteAll(); + $ts = new ObjectCollection(); + $ts->setModel('\Propel\Tests\Bookstore\Behavior\Table2'); + for ($i = 0; $i < 10; $i++) { + $t = new Table2(); + $t->setTitle('CreatedAt' . $i); + $t->setCreatedAt(time() - $i * 24 * 60 * 60 - 30); + $ts[] = $t; + } + $ts->save(); + } + + /** + * @return void + */ + public function testQueryRecentlyUpdated() + { + $q = Table2Query::create()->recentlyUpdated(); + $this->assertTrue($q instanceof Table2Query, 'recentlyUpdated() returns the current Query object'); + $this->populateUpdatedAt(); + $ts = Table2Query::create()->recentlyUpdated()->count(); + $this->assertEquals(7, $ts, 'recentlyUpdated() returns the elements updated in the last 7 days by default'); + $ts = Table2Query::create()->recentlyUpdated(5)->count(); + $this->assertEquals(5, $ts, 'recentlyUpdated() accepts a number of days as parameter'); + } + + /** + * @return void + */ + public function testQueryRecentlyCreated() + { + $q = Table2Query::create()->recentlyCreated(); + $this->assertTrue($q instanceof Table2Query, 'recentlyCreated() returns the current Query object'); + $this->populateCreatedAt(); + $ts = Table2Query::create()->recentlyCreated()->count(); + $this->assertEquals(7, $ts, 'recentlyCreated() returns the elements created in the last 7 days by default'); + $ts = Table2Query::create()->recentlyCreated(5)->count(); + $this->assertEquals(5, $ts, 'recentlyCreated() accepts a number of days as parameter'); + } + + /** + * @return void + */ + public function testQueryLastUpdatedFirst() + { + $q = Table2Query::create()->lastUpdatedFirst(); + $this->assertTrue($q instanceof Table2Query, 'lastUpdatedFirst() returns the current Query object'); + $this->populateUpdatedAt(); + $t = Table2Query::create()->lastUpdatedFirst()->findOne(); + $this->assertEquals('UpdatedAt0', $t->getTitle(), 'lastUpdatedFirst() returns element with most recent update date first'); + } + + /** + * @return void + */ + public function testQueryFirstUpdatedFirst() + { + $q = Table2Query::create()->firstUpdatedFirst(); + $this->assertTrue($q instanceof Table2Query, 'firstUpdatedFirst() returns the current Query object'); + $this->populateUpdatedAt(); + $t = Table2Query::create()->firstUpdatedFirst()->findOne(); + $this->assertEquals('UpdatedAt9', $t->getTitle(), 'firstUpdatedFirst() returns the element with oldest updated date first'); + } + + /** + * @return void + */ + public function testQueryLastCreatedFirst() + { + $q = Table2Query::create()->lastCreatedFirst(); + $this->assertTrue($q instanceof Table2Query, 'lastCreatedFirst() returns the current Query object'); + $this->populateCreatedAt(); + $t = Table2Query::create()->lastCreatedFirst()->findOne(); + $this->assertEquals('CreatedAt0', $t->getTitle(), 'lastCreatedFirst() returns element with most recent create date first'); + } + + /** + * @return void + */ + public function testQueryFirstCreatedFirst() + { + $q = Table2Query::create()->firstCreatedFirst(); + $this->assertTrue($q instanceof Table2Query, 'firstCreatedFirst() returns the current Query object'); + $this->populateCreatedAt(); + $t = Table2Query::create()->firstCreatedFirst()->findOne(); + $this->assertEquals('CreatedAt9', $t->getTitle(), 'firstCreatedFirst() returns the element with oldest create date first'); + } + + /** + * @return void + */ + public function testDisableUpdatedAt() + { + $schema = << + + + + + + + +
+ +EOF; + + $builder = new QuickBuilder(); + $builder->setSchema($schema); + $builder->build(); + + $this->assertTrue(method_exists('TableWithoutUpdatedAt', 'getCreatedAt')); + $this->assertTrue(method_exists('TableWithoutUpdatedAt', 'setCreatedAt')); + $this->assertFalse(method_exists('TableWithoutUpdatedAt', 'getUpdatedAt')); + $this->assertFalse(method_exists('TableWithoutUpdatedAt', 'setUpdatedAt')); + + $obj = new TableWithoutUpdatedAt(); + $obj->setName('Peter'); + $this->assertNull($obj->getCreatedAt()); + $this->assertEquals(1, $obj->save()); + $this->assertNotNull($obj->getCreatedAt()); + } + + /** + * @return void + */ + public function testDisableCreatedAt() + { + $schema = << + + + + + + + +
+ +EOF; + + $builder = new QuickBuilder(); + $builder->setSchema($schema); + $builder->build(); + + $this->assertFalse(method_exists('TableWithoutCreatedAt', 'getCreatedAt')); + $this->assertFalse(method_exists('TableWithoutCreatedAt', 'setCreatedAt')); + $this->assertTrue(method_exists('TableWithoutCreatedAt', 'getUpdatedAt')); + $this->assertTrue(method_exists('TableWithoutCreatedAt', 'setUpdatedAt')); + + $obj = new TableWithoutCreatedAt(); + $obj->setName('Peter'); + $this->assertNull($obj->getUpdatedAt()); + $this->assertEquals(1, $obj->save()); + $this->assertNotNull($obj->getUpdatedAt()); + } /** * @return void From c9beaea81b0ad593670bc3232552541a7b260bc7 Mon Sep 17 00:00:00 2001 From: Steve Mareigner Date: Mon, 18 Sep 2023 17:31:49 +0200 Subject: [PATCH 3/5] test(timestampable): Adapt tests to reflect new behavior's parameter --- tests/Propel/Tests/Generator/Builder/Om/TableMapBuilderTest.php | 1 + tests/Propel/Tests/Resources/blog-schema.xml | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/Propel/Tests/Generator/Builder/Om/TableMapBuilderTest.php b/tests/Propel/Tests/Generator/Builder/Om/TableMapBuilderTest.php index 3350558526..8d33536465 100644 --- a/tests/Propel/Tests/Generator/Builder/Om/TableMapBuilderTest.php +++ b/tests/Propel/Tests/Generator/Builder/Om/TableMapBuilderTest.php @@ -314,6 +314,7 @@ public function testBehaviors() 'update_column' => 'updated_on', 'disable_created_at' => 'false', 'disable_updated_at' => 'false', + 'is_timestamp' => 'true' ], ]; $this->assertEquals( diff --git a/tests/Propel/Tests/Resources/blog-schema.xml b/tests/Propel/Tests/Resources/blog-schema.xml index acf1143130..050550a487 100644 --- a/tests/Propel/Tests/Resources/blog-schema.xml +++ b/tests/Propel/Tests/Resources/blog-schema.xml @@ -36,6 +36,7 @@ + From ba4387852b335f56ee10b7d6cafbf25b532d6823 Mon Sep 17 00:00:00 2001 From: Steve Mareigner Date: Mon, 18 Sep 2023 17:34:21 +0200 Subject: [PATCH 4/5] test(timestampable): Adapt tests to reflect new behavior's parameter --- tests/Propel/Tests/Resources/blog-database.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Propel/Tests/Resources/blog-database.xml b/tests/Propel/Tests/Resources/blog-database.xml index 60e0a508da..6bc91295ca 100644 --- a/tests/Propel/Tests/Resources/blog-database.xml +++ b/tests/Propel/Tests/Resources/blog-database.xml @@ -23,6 +23,7 @@ + From 85745f9bcbeadef81cf22a97c2e5f428977ff110 Mon Sep 17 00:00:00 2001 From: Steve Mareigner Date: Mon, 18 Sep 2023 17:37:20 +0200 Subject: [PATCH 5/5] test(timestampable): Adapt tests to reflect new behavior's parameter --- tests/Propel/Tests/Generator/Model/BehaviorTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Propel/Tests/Generator/Model/BehaviorTest.php b/tests/Propel/Tests/Generator/Model/BehaviorTest.php index 72ca41b301..61b93f4cdd 100644 --- a/tests/Propel/Tests/Generator/Model/BehaviorTest.php +++ b/tests/Propel/Tests/Generator/Model/BehaviorTest.php @@ -155,7 +155,8 @@ public function testSchemaReader() 'leParameterList' => [ ['leListItem1Value' => 'leValue1'], ['leListItem2Value1' => 'leValue2.1', 'leListItem2Value2' => 'leValue2.2'], - ] + ], + 'is_timestamp' => 'true' ]; $this->assertEquals($expectedParameters, $behavior->getParameters(), 'SchemaReader sets the behavior parameters correctly'); }