You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
2.5 KiB
111 lines
2.5 KiB
11 years ago
|
<?php
|
||
|
|
||
11 years ago
|
namespace yiiunit\framework\behaviors;
|
||
|
|
||
|
use Yii;
|
||
11 years ago
|
use yiiunit\TestCase;
|
||
|
use yii\db\Connection;
|
||
|
use yii\db\ActiveRecord;
|
||
11 years ago
|
use yii\behaviors\TimestampBehavior;
|
||
11 years ago
|
|
||
|
/**
|
||
11 years ago
|
* Unit test for [[\yii\behaviors\TimestampBehavior]].
|
||
11 years ago
|
* @see TimestampBehavior
|
||
11 years ago
|
*
|
||
|
* @group behaviors
|
||
11 years ago
|
*/
|
||
11 years ago
|
class TimestampBehaviorTest extends TestCase
|
||
11 years ago
|
{
|
||
11 years ago
|
/**
|
||
|
* @var Connection test db connection
|
||
|
*/
|
||
|
protected $dbConnection;
|
||
|
|
||
|
public static function setUpBeforeClass()
|
||
|
{
|
||
|
if (!extension_loaded('pdo') || !extension_loaded('pdo_sqlite')) {
|
||
|
static::markTestSkipped('PDO and SQLite extensions are required.');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function setUp()
|
||
|
{
|
||
|
$this->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!');
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Test Active Record class with [[TimestampBehavior]] behavior attached.
|
||
11 years ago
|
*
|
||
|
* @property integer $id
|
||
11 years ago
|
* @property integer $created_at
|
||
|
* @property integer $updated_at
|
||
11 years ago
|
*/
|
||
11 years ago
|
class ActiveRecordTimestamp extends ActiveRecord
|
||
11 years ago
|
{
|
||
11 years ago
|
public function behaviors()
|
||
|
{
|
||
|
return [
|
||
|
TimestampBehavior::className(),
|
||
|
];
|
||
|
}
|
||
|
|
||
|
public static function tableName()
|
||
|
{
|
||
|
return 'test_auto_timestamp';
|
||
|
}
|
||
11 years ago
|
}
|