M
M
Messi2018-11-22 12:12:36
This
Messi, 2018-11-22 12:12:36

Why is fixture data not being removed from the database?

Hello! I am familiar with testing ( Yii2 basic ), so far unit tests. I connected a fixture for the User class, users are added to the database (test) during the test, but after execution they are not deleted from there. Tell me, please, why?
In the config folder there is a test_db.php file, I set up a test database.
Installed codeception. The tests folder appeared. Added fixtures to unit.suite.yml .
Available:
Model User

<?php

namespace app\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\web\IdentityInterface;

/**
 * This is the model class for table "user".
 *
 * @property int $id
 * @property string $username
 * @property int $created_at
 *
 * @property Bill[] $bills
 * @property Transaction[] $transactions
 * @property Transaction[] $transactions0
 */
class User extends \yii\db\ActiveRecord implements IdentityInterface
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['username'], 'required'],
            [['created_at'], 'integer'],
            [['username'], 'string', 'max' => 255],
            [['username'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'username' => 'Username',
            'created_at' => 'Created At',
        ];
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'updatedAtAttribute' => false
            ],
        ];
    }

    public function afterSave($insert, $changedAttributes){
        parent::afterSave($insert, $changedAttributes);
        // Add bill for new user;
        Bill::create($this->id);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getBill()
    {
        return $this->hasOne(Bill::className(), ['user_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getRecipient()
    {
        return $this->hasMany(Transaction::className(), ['recipient_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getSender()
    {
        return $this->hasMany(Transaction::className(), ['sender_id' => 'id']);
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        // TODO: Implement findIdentityByAccessToken() method.
    }

    /**
     * @inheritdoc
     */
    public function getAuthKey()
    {
        // TODO: Implement findIdentityByAccessToken() method.
    }

    /**
     * @inheritdoc
     */
    public function validateAuthKey($authKey)
    {
        // TODO: Implement validateAuthKey() method.
    }

    /**
     * @param int|string $id
     * @return null|IdentityInterface|static
     */
    public static function findIdentity($id)
    {
        return self::findOne($id);
    }

    public function getId()
    {
        return $this->id;
    }

    /**
     * Finds user by username
     *
     * @param string $username
     * @return static|null
     */
    public static function findByUsername($username)
    {
        return $user = User::findOne(['username' => $username]);
    }
}

Testing the Site class , SiteTest.php
<?php

namespace tests;

use app\models\form\LoginForm;
use app\tests\fixtures\UserFixture;

class SiteTest extends \Codeception\Test\Unit
{
    /**
     * @var \UnitTester
     */
    protected $tester;

    public function _fixtures()
    {
        return ['users' => UserFixture::className()];
    }

    public function testLogin()
    {
        $user = $this->tester->grabFixture('users', 'user1');

        $login = new LoginForm();
        $login->username = $user->username;
    }
}

Fixture User
<?php

namespace app\tests\fixtures;

use yii\test\ActiveFixture;

class UserFixture extends ActiveFixture
{
    public $modelClass = 'app\models\User';
}

Fixture file user.php:
<?php

    return [
        'user1' => [
            'id' => 1,
            'username' => 'test1',
            'created_at' => 1542814772
        ],
        'user2' => [
            'id' => 2,
            'username' => 'test2',
            'created_at' => 1542814773
        ],
        'user3' => [
            'id' => 3,
            'username' => 'test3',
            'created_at' => 1542814774
        ]
    ];

I run ./vendor/bin/codecept run unit SiteTest, for the first time it works out and adds users to the test database, after the end of the test, the data remains in the database, and is not deleted. The next test run does not add data if it is in the database.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question