mockApplication([ 'components' => [ 'db' => [ 'class' => '\yii\db\Connection', 'dsn' => 'sqlite::memory:', ] ] ]); $columns = [ 'id' => 'pk', 'created_at' => 'integer', 'updated_at' => 'integer', ]; Yii::$app->getDb()->createCommand()->createTable('test_auto_timestamp', $columns)->execute(); } public function tearDown() { Yii::$app->getDb()->close(); parent::tearDown(); } // Tests : public function testNewRecord() { $currentTime = time(); $model = new ActiveRecordTimestamp(); $model->save(false); $this->assertTrue($model->created_at >= $currentTime); $this->assertTrue($model->updated_at >= $currentTime); } /** * @depends testNewRecord */ public function testUpdateRecord() { $currentTime = time(); $model = new ActiveRecordTimestamp(); $model->save(false); $enforcedTime = $currentTime - 100; $model->created_at = $enforcedTime; $model->updated_at = $enforcedTime; $model->save(false); $this->assertEquals($enforcedTime, $model->created_at, 'Create time has been set on update!'); $this->assertTrue($model->updated_at >= $currentTime, 'Update time has NOT been set on update!'); } } /** * Test Active Record class with [[TimestampBehavior]] behavior attached. * * @property integer $id * @property integer $created_at * @property integer $updated_at */ class ActiveRecordTimestamp extends ActiveRecord { public function behaviors() { return [ TimestampBehavior::className(), ]; } public static function tableName() { return 'test_auto_timestamp'; } }