From 09d84f11a786f52d2b9690515ee32178000f4469 Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Sat, 6 Jul 2013 00:07:39 +0300 Subject: [PATCH 1/2] Unit test for "yii\behaviors\AutoTimestamp" has been created --- .../unit/framework/behaviors/AutoTimestampTest.php | 137 +++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 tests/unit/framework/behaviors/AutoTimestampTest.php diff --git a/tests/unit/framework/behaviors/AutoTimestampTest.php b/tests/unit/framework/behaviors/AutoTimestampTest.php new file mode 100644 index 0000000..51bbe2f --- /dev/null +++ b/tests/unit/framework/behaviors/AutoTimestampTest.php @@ -0,0 +1,137 @@ +mockApplication( + array( + 'components' => array( + 'db' => array( + 'class' => '\yii\db\Connection', + 'dsn' => 'sqlite::memory:', + ) + ) + ) + ); + + $columns = array( + 'id' => 'pk', + 'create_time' => 'integer', + 'update_time' => 'integer', + ); + Yii::$app->getDb()->createCommand()->createTable(self::$testTableName, $columns)->execute(); + + $this->declareTestActiveRecordClass(); + } + + public function tearDown() + { + Yii::$app->getDb()->close(); + parent::tearDown(); + } + + /** + * Declares test Active Record class with auto timestamp behavior attached. + */ + protected function declareTestActiveRecordClass() + { + $className = static::$testActiveRecordClassName; + if (class_exists($className, false)) { + return true; + } + + $activeRecordClassName = ActiveRecord::className(); + $behaviorClassName = AutoTimestamp::className(); + $tableName = static::$testTableName; + + $classDefinitionCode = << array( + 'class' => '{$behaviorClassName}', + 'attributes' => array( + static::EVENT_BEFORE_INSERT => array('create_time', 'update_time'), + static::EVENT_BEFORE_UPDATE => 'update_time', + ), + ), + ); + } + + public static function tableName() + { + return '{$tableName}'; + } +} +EOL; + eval($classDefinitionCode); + return true; + } + + // Tests : + + public function testNewRecord() + { + $currentTime = time(); + + $className = static::$testActiveRecordClassName; + $model = new $className(); + $model->save(false); + + $this->assertTrue($model->create_time >= $currentTime); + $this->assertTrue($model->update_time >= $currentTime); + } + + /** + * @depends testNewRecord + */ + public function testUpdateRecord() + { + $currentTime = time(); + + $className = static::$testActiveRecordClassName; + $model = new $className(); + $model->save(false); + + $enforcedTime = $currentTime - 100; + + $model->create_time = $enforcedTime; + $model->update_time = $enforcedTime; + $model->save(false); + + $this->assertEquals($enforcedTime, $model->create_time, 'Create time has been set on update!'); + $this->assertTrue($model->update_time >= $currentTime, 'Update time has NOT been set on update!'); + } +} \ No newline at end of file From 2fdfacc9186f3f374b7be3f7435bed45fca2d675 Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Sat, 6 Jul 2013 16:04:24 +0300 Subject: [PATCH 2/2] Unit test "AutoTimestampTest" has been reworked to use static model class. --- .../unit/framework/behaviors/AutoTimestampTest.php | 90 ++++++++-------------- 1 file changed, 34 insertions(+), 56 deletions(-) diff --git a/tests/unit/framework/behaviors/AutoTimestampTest.php b/tests/unit/framework/behaviors/AutoTimestampTest.php index 51bbe2f..de96630 100644 --- a/tests/unit/framework/behaviors/AutoTimestampTest.php +++ b/tests/unit/framework/behaviors/AutoTimestampTest.php @@ -1,5 +1,8 @@ 'integer', 'update_time' => 'integer', ); - Yii::$app->getDb()->createCommand()->createTable(self::$testTableName, $columns)->execute(); - - $this->declareTestActiveRecordClass(); + Yii::$app->getDb()->createCommand()->createTable('test_auto_timestamp', $columns)->execute(); } public function tearDown() @@ -60,54 +52,13 @@ class AutoTimestampTest extends TestCase parent::tearDown(); } - /** - * Declares test Active Record class with auto timestamp behavior attached. - */ - protected function declareTestActiveRecordClass() - { - $className = static::$testActiveRecordClassName; - if (class_exists($className, false)) { - return true; - } - - $activeRecordClassName = ActiveRecord::className(); - $behaviorClassName = AutoTimestamp::className(); - $tableName = static::$testTableName; - - $classDefinitionCode = << array( - 'class' => '{$behaviorClassName}', - 'attributes' => array( - static::EVENT_BEFORE_INSERT => array('create_time', 'update_time'), - static::EVENT_BEFORE_UPDATE => 'update_time', - ), - ), - ); - } - - public static function tableName() - { - return '{$tableName}'; - } -} -EOL; - eval($classDefinitionCode); - return true; - } - // Tests : public function testNewRecord() { $currentTime = time(); - $className = static::$testActiveRecordClassName; - $model = new $className(); + $model = new ActiveRecordAutoTimestamp(); $model->save(false); $this->assertTrue($model->create_time >= $currentTime); @@ -121,8 +72,7 @@ EOL; { $currentTime = time(); - $className = static::$testActiveRecordClassName; - $model = new $className(); + $model = new ActiveRecordAutoTimestamp(); $model->save(false); $enforcedTime = $currentTime - 100; @@ -134,4 +84,32 @@ EOL; $this->assertEquals($enforcedTime, $model->create_time, 'Create time has been set on update!'); $this->assertTrue($model->update_time >= $currentTime, 'Update time has NOT been set on update!'); } +} + +/** + * Test Active Record class with [[AutoTimestamp]] behavior attached. + * + * @property integer $id + * @property integer $create_time + * @property integer $update_time + */ +class ActiveRecordAutoTimestamp extends ActiveRecord +{ + public function behaviors() + { + return array( + 'timestamp' => array( + 'class' => AutoTimestamp::className(), + 'attributes' => array( + static::EVENT_BEFORE_INSERT => array('create_time', 'update_time'), + static::EVENT_BEFORE_UPDATE => 'update_time', + ), + ), + ); + } + + public static function tableName() + { + return 'test_auto_timestamp'; + } } \ No newline at end of file