如何加载Yii 2夹具?

时间:2016-06-12 09:22:01

标签: php unit-testing yii2 fixtures codeception

我使用一个简单的fixture来填充测试数据库中的表。但是当我尝试加载它时,检查完整性代码会引发异常'表未找到:[]'。

我的装备:

<?php

namespace tests\unit\fixtures;

use yii\test\ActiveFixture;

class IdentityDocumentFixture extends ActiveFixture
{
    public $modelClass = 'backend\modules\persons\models\IdentityDocument';
}

要加载灯具,我使用@ app / tests:yii fixture/load IdentityDocument

中的此命令
C:\php-projects\ais\tests\codeception\bin>yii fixture/load IdentityDocument
Fixtures namespace is:
        tests\unit\fixtures

Global fixtures will be used:

        1. yii\test\InitDb

Fixtures below will be loaded:

        1. IdentityDocument

Load above fixtures? (yes|no) [no]:yes

结果,我收到此错误消息:

  

异常&#39; yii \ base \ InvalidParamException&#39;消息&#39;表未找到:[]&#39; in   C:\ PHP-项目\ AIS \厂商\ yiisoft \ yii2 \分贝\ MSSQL \ QueryBuilder.php:180

堆栈追踪:

  

0 C:\ php-projects \ ais \ vendor \ yiisoft \ yii2 \ db \ Command.php(753):   yii \ db \ mssql \ QueryBuilder-&gt; checkIntegrity(false,&#39;&#39;,&#39;&#39;)1   C:\ PHP-项目\ AIS \厂商\ yiisoft \ yii2 \测试\ InitDbFixture.php(94):   yii \ db \ Command-&gt; checkIntegrity(false,&#39;&#39;)2   C:\ PHP-项目\ AIS \厂商\ yiisoft \ yii2 \测试\ InitDbFixture.php(76):   yii \ test \ InitDbFixture-&gt; checkIntegrity(false)3   C:\ PHP-项目\ AIS \厂商\ yiisoft \ yii2 \测试\ FixtureTrait.php(114):   yii \ test \ InitDbFixture-&gt; beforeUnload()4   C:\ PHP-项目\ AIS \厂商\ yiisoft \ yii2 \控制台\控制器\ FixtureController.php(147):   yii \ console \ controllers \ FixtureController-&gt; unloadFixtures(Array)5   [内部功能]:   YII \控制台\控制器\ FixtureController-&GT; actionLoad(&#39; IdentityDocumen ...&#39;)   6 C:\ php-projects \ ais \ vendor \ yiisoft \ yii2 \ base \ InlineAction.php(55):   call_user_func_array(Array,Array)7   C:\ PHP-项目\ AIS \厂商\ yiisoft \ yii2 \碱\ Controller.php这样(154):   yii \ base \ InlineAction-&gt; runWithParams(Array)8   C:\ PHP-项目\ AIS \厂商\ yiisoft \ yii2 \控制台\ Controller.php这样(91):   yii \ base \ Controller-&gt; runAction(&#39; load&#39;,Array)9   C:\ PHP-项目\ AIS \厂商\ yiisoft \ yii2 \碱\ Module.php(454):   yii \ console \ Controller-&gt; runAction(&#39; load&#39;,Array)10   C:\ PHP-项目\ AIS \厂商\ yiisoft \ yii2 \控制台\ Application.php(167):   yii \ base \ Module-&gt; runAction(&#39; fixture / load&#39;,Array)11   C:\ PHP-项目\ AIS \厂商\ yiisoft \ yii2 \控制台\ Application.php(143):   yii \ console \ Application-&gt; runAction(&#39; fixture / load&#39;,Array)12   C:\ PHP-项目\ AIS \厂商\ yiisoft \ yii2 \碱\ Application.php(375):   yii \ console \ Application-&gt; handleRequest(Object(yii \ console \ Request))13   C:\ PHP-项目\ AIS \测试\ codeception \ BIN \ YII(22):   yii \ base \ Application-&gt; run()14 {main}

如何加载夹具?存在测试数据库,数据源配置正确。

[更新]

表的真实姓名是tbl_identitydocument

CREATE TABLE [dbo].[tbl_identitydocument](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](100) NOT NULL,
    [FISID] [int] NULL,
 CONSTRAINT [PK_tbl_identitydocument] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

以下是模型tableName()的方法IdentityDocument

public static function tableName()
{
    return '{{%identitydocument}}';
}

完全模型代码:

<?php

namespace backend\modules\persons\models;

use Yii;
use backend\modules\persons\Module;

/**
 * This is the model class for table "{{%identitydocument}}".
 *
 * @property integer $ID
 * @property string $Name
 * @property integer $FISID
 */
class IdentityDocument extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%identitydocument}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['Name'], 'required'],
            [['Name'], 'string'],
            [['FISID'], 'integer']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ID' => Module::t('ML', 'ID'),
            'Name' => Module::t('ML', 'Name'),
            'FISID' => Module::t('ML', 'FISID'),
        ];
    }
}

在调试器下,我可以看到方法IdentityDocument::tableName()返回:

$tableNameTmp = IdentityDocument::tableName();

我看到$tableNameTmp等于{{%identitydocument}}逐字,返回此字符串)

[更新]

在调试器下我可以看到以下内容。在课程TestCase.php中有一个方法setUp()

/**
 * @inheritdoc
 */
protected function setUp()
{
    parent::setUp();
    $this->mockApplication();
    $this->unloadFixtures();
    $this->loadFixtures();
}

此处抛出异常:$this->unloadFixtures()

enter image description here

在屏幕截图中可以看到initScript引用了路径@app/tests/fixtures/initdb.php ,但没有这样的文件 .. schemas数组也包含一个元素""

[更新]

我使用@slinstj在此处撰写的说明 - Run fixture/load User fails to load User data。在我的本地项目(高级模式和MySQL rdbms)中,夹具成功加载:

enter image description here

1 个答案:

答案 0 :(得分:2)

1 - 检查您是否为tablePrefix设置了yii\db\Connection参数:

'components' => [
    'db' => [
        'class' => '\yii\db\Connection',
        'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8',
        'tablePrefix' => 'tbl_', // <<<<<<<<
    ],
],

来自docs的更多信息:

$tablePrefix

  

表名的公共前缀或后缀。如果表名称为{{%TableName}},则百分比字符%将替换为此属性值。例如,{{%post}}变为{{tbl_post}}。

OR

2 - 设置dataFile路径:

根据this

// In your IdentityDocumentFixture, set this:
public $dataFile = __DIR__ . '/path/to/your/data-file.php';