Yii2 fixtures 不卸载,不加载依赖项
Yii2 fixtures not unloading, not loading with dependencies
卸载问题
我正在尝试在 Yii2 中创建固定装置,以便能够用一些测试数据填充我的 tables。我还没有使用 Codeception。我正在关注固定装置上的 Yii2 guide。第一个 table 是用户 table:
namespace tests\unit\fixtures;
use yii\test\ActiveFixture;
/**
* User fixture
*/
class UserFixture extends ActiveFixture
{
public $modelClass = 'common\models\User';
}
当我通过 ssh 进入 Vagrant 并加载夹具时,这个工作正常,但在我卸载后条目仍然存在。根据终端输出,夹具已成功卸载。我在这里错过了什么?这应该开箱即用,还是应该创建自己的卸载函数?
编辑:
有用的是将它添加到用户固定装置中:
public function unload(){
parent::unload();
$this->resetTable();
}
无论如何我都希望它出现在卸载中,但我已经阅读了下面发布的 link 中的(非常慢的)讨论。我不知道 parent::unload() 行是否必要,它在没有行的情况下工作,但 BaseActiveFixture 定义它并清空 $this->data 和 $this->_models.
取决于问题
我的第二个夹具取决于用户夹具:
namespace tests\unit\fixtures;
use yii\test\ActiveFixture;
/**
* User Libraries fixture
*/
class UserLibrariesFixture extends ActiveFixture
{
public $modelClass = 'common\models\UserLibraries';
// Dependencies
public $depends = [
'tests\unit\fixtures\UserFixture',
];
}
这个也根据终端正确加载,但 UserLibraries table 仍然是空的。它没有说它会加载依赖项,但我不知道它是否应该说它会。
我使数据文件尽可能简单,正确的数据出现在用户 table 中。我只为 UserLibraries table 的必填字段添加了数据,所以我不知道这是否是个问题。是否有日志文件可供我检查有关固定装置的条目?
编辑:
UserLibraries fixture 现在可以在 User table(但不是 UserLibraries table)中创建数据,因此禁用外键检查适用于具有依赖性的 fixture。这让我觉得我的 UserLibraries 数据文件中有错误。检查我是否需要日志文件。
编辑2:
夹具加载问题解决方案
由于 table 名称中的下划线,无法加载灯具。当使用 Gii 创建时,table 名称 userLibraries 和 user_libraries 将导致模型、控制器和视图文件具有相同的文件名。使用驼峰名称 table 我可以加载固定装置。
卸载 fixtures 是一个问题"under discussion" (see here)。但这是我的 mysql 解决方法(我也在那里评论过)并且应该添加到每个具有一些依赖性的夹具模型中 table:
<?php
namespace tests\codeception\common\fixtures;
use yii\test\ActiveFixture;
class VariationFixture extends ActiveFixture
{
public $modelClass = 'common\models\Variation';
public function beforeLoad() {
parent::beforeLoad();
$this->db->createCommand()->setSql('SET FOREIGN_KEY_CHECKS = 0')->execute();
}
public function afterLoad() {
parent::afterLoad();
$this->db->createCommand()->setSql('SET FOREIGN_KEY_CHECKS = 1')->execute();
}
}
至于加载,使用codeception你可以使用/tests/codeception/common/_support/FixtureHelper::fixtures()
来定义你想要在每个测试用例之前加载的fixtures:
public function fixtures()
{
return [
'user' => [
'class' => UserFixture::className(),
'dataFile' => '@tests/codeception/common/fixtures/data/init_login.php',
],
'room' => [
'class' => RoomFixture::className(),
'dataFile' => '@tests/codeception/common/fixtures/company/data/room.php',
],
...
];
}
卸载问题
我正在尝试在 Yii2 中创建固定装置,以便能够用一些测试数据填充我的 tables。我还没有使用 Codeception。我正在关注固定装置上的 Yii2 guide。第一个 table 是用户 table:
namespace tests\unit\fixtures;
use yii\test\ActiveFixture;
/**
* User fixture
*/
class UserFixture extends ActiveFixture
{
public $modelClass = 'common\models\User';
}
当我通过 ssh 进入 Vagrant 并加载夹具时,这个工作正常,但在我卸载后条目仍然存在。根据终端输出,夹具已成功卸载。我在这里错过了什么?这应该开箱即用,还是应该创建自己的卸载函数?
编辑: 有用的是将它添加到用户固定装置中:
public function unload(){
parent::unload();
$this->resetTable();
}
无论如何我都希望它出现在卸载中,但我已经阅读了下面发布的 link 中的(非常慢的)讨论。我不知道 parent::unload() 行是否必要,它在没有行的情况下工作,但 BaseActiveFixture 定义它并清空 $this->data 和 $this->_models.
取决于问题
我的第二个夹具取决于用户夹具:
namespace tests\unit\fixtures;
use yii\test\ActiveFixture;
/**
* User Libraries fixture
*/
class UserLibrariesFixture extends ActiveFixture
{
public $modelClass = 'common\models\UserLibraries';
// Dependencies
public $depends = [
'tests\unit\fixtures\UserFixture',
];
}
这个也根据终端正确加载,但 UserLibraries table 仍然是空的。它没有说它会加载依赖项,但我不知道它是否应该说它会。
我使数据文件尽可能简单,正确的数据出现在用户 table 中。我只为 UserLibraries table 的必填字段添加了数据,所以我不知道这是否是个问题。是否有日志文件可供我检查有关固定装置的条目?
编辑: UserLibraries fixture 现在可以在 User table(但不是 UserLibraries table)中创建数据,因此禁用外键检查适用于具有依赖性的 fixture。这让我觉得我的 UserLibraries 数据文件中有错误。检查我是否需要日志文件。
编辑2: 夹具加载问题解决方案
由于 table 名称中的下划线,无法加载灯具。当使用 Gii 创建时,table 名称 userLibraries 和 user_libraries 将导致模型、控制器和视图文件具有相同的文件名。使用驼峰名称 table 我可以加载固定装置。
卸载 fixtures 是一个问题"under discussion" (see here)。但这是我的 mysql 解决方法(我也在那里评论过)并且应该添加到每个具有一些依赖性的夹具模型中 table:
<?php
namespace tests\codeception\common\fixtures;
use yii\test\ActiveFixture;
class VariationFixture extends ActiveFixture
{
public $modelClass = 'common\models\Variation';
public function beforeLoad() {
parent::beforeLoad();
$this->db->createCommand()->setSql('SET FOREIGN_KEY_CHECKS = 0')->execute();
}
public function afterLoad() {
parent::afterLoad();
$this->db->createCommand()->setSql('SET FOREIGN_KEY_CHECKS = 1')->execute();
}
}
至于加载,使用codeception你可以使用/tests/codeception/common/_support/FixtureHelper::fixtures()
来定义你想要在每个测试用例之前加载的fixtures:
public function fixtures()
{
return [
'user' => [
'class' => UserFixture::className(),
'dataFile' => '@tests/codeception/common/fixtures/data/init_login.php',
],
'room' => [
'class' => RoomFixture::className(),
'dataFile' => '@tests/codeception/common/fixtures/company/data/room.php',
],
...
];
}