Browse Source

Unit test "AutoTimestampTest" has been reworked to use static model class.

tags/2.0.0-beta
Klimov Paul 12 years ago
parent
commit
2fdfacc918
  1. 90
      tests/unit/framework/behaviors/AutoTimestampTest.php

90
tests/unit/framework/behaviors/AutoTimestampTest.php

@ -1,5 +1,8 @@
<?php <?php
namespace yiiunit\framework\behaviors;
use Yii;
use yiiunit\TestCase; use yiiunit\TestCase;
use yii\db\Connection; use yii\db\Connection;
use yii\db\ActiveRecord; use yii\db\ActiveRecord;
@ -15,21 +18,12 @@ class AutoTimestampTest extends TestCase
* @var Connection test db connection * @var Connection test db connection
*/ */
protected $dbConnection; protected $dbConnection;
/**
* @var string test table name.
*/
protected static $testTableName = 'test_table';
/**
* @var string test Active Record class name.
*/
protected static $testActiveRecordClassName;
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {
if (!extension_loaded('pdo') || !extension_loaded('pdo_sqlite')) { if (!extension_loaded('pdo') || !extension_loaded('pdo_sqlite')) {
static::markTestSkipped('PDO and SQLite extensions are required.'); static::markTestSkipped('PDO and SQLite extensions are required.');
} }
static::$testActiveRecordClassName = get_called_class() . '_TestActiveRecord_' . sha1(uniqid());
} }
public function setUp() { public function setUp() {
@ -49,9 +43,7 @@ class AutoTimestampTest extends TestCase
'create_time' => 'integer', 'create_time' => 'integer',
'update_time' => 'integer', 'update_time' => 'integer',
); );
Yii::$app->getDb()->createCommand()->createTable(self::$testTableName, $columns)->execute(); Yii::$app->getDb()->createCommand()->createTable('test_auto_timestamp', $columns)->execute();
$this->declareTestActiveRecordClass();
} }
public function tearDown() public function tearDown()
@ -60,54 +52,13 @@ class AutoTimestampTest extends TestCase
parent::tearDown(); 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 = <<<EOL
class {$className} extends {$activeRecordClassName}
{
public function behaviors()
{
return array(
'timestamp' => 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 : // Tests :
public function testNewRecord() public function testNewRecord()
{ {
$currentTime = time(); $currentTime = time();
$className = static::$testActiveRecordClassName; $model = new ActiveRecordAutoTimestamp();
$model = new $className();
$model->save(false); $model->save(false);
$this->assertTrue($model->create_time >= $currentTime); $this->assertTrue($model->create_time >= $currentTime);
@ -121,8 +72,7 @@ EOL;
{ {
$currentTime = time(); $currentTime = time();
$className = static::$testActiveRecordClassName; $model = new ActiveRecordAutoTimestamp();
$model = new $className();
$model->save(false); $model->save(false);
$enforcedTime = $currentTime - 100; $enforcedTime = $currentTime - 100;
@ -135,3 +85,31 @@ EOL;
$this->assertTrue($model->update_time >= $currentTime, 'Update time has NOT 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';
}
}
Loading…
Cancel
Save