TimestampBehaviorTest.php 2.51 KB
Newer Older
1 2
<?php

3 4 5
namespace yiiunit\framework\behaviors;

use Yii;
6 7 8
use yiiunit\TestCase;
use yii\db\Connection;
use yii\db\ActiveRecord;
9
use yii\behaviors\TimestampBehavior;
10 11

/**
12
 * Unit test for [[\yii\behaviors\TimestampBehavior]].
Qiang Xue committed
13
 * @see TimestampBehavior
14 15
 *
 * @group behaviors
16
 */
17
class TimestampBehaviorTest extends TestCase
18
{
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    /**
     * @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!');
    }
88 89 90
}

/**
Qiang Xue committed
91
 * Test Active Record class with [[TimestampBehavior]] behavior attached.
92 93
 *
 * @property integer $id
Alexander Kochetov committed
94 95
 * @property integer $created_at
 * @property integer $updated_at
96
 */
97
class ActiveRecordTimestamp extends ActiveRecord
98
{
99 100 101 102 103 104 105 106 107 108 109
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    public static function tableName()
    {
        return 'test_auto_timestamp';
    }
Alexander Makarov committed
110
}