带有不同表名和插件的Cakephp夹具

问题描述 投票:0回答:1

我刚开始使用CakePHP进行测试,现在正在创建一个新插件,希望为其创建单元测试。

新插件'MyPlugin'连接到多个数据库,其中一个数据库与不符合蛋糕命名标准的无蛋糕应用程序(迁移到蛋糕)共享。

我想要创建的第一个测试是从'AppApiKey'表中获取一些数据-但是数据库中的名称都是小写字母,没有'_,因此是'appapikey'而不是'app_api_key'。

在名为“ AppApiKeyFixture”的插件中创建了灯具)>

namespace MyPlugin\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

class AppApiKeyFixture extends TestFixture {
    public $connection = 'test_ctrller';
    public $table = 'appapikey';

    //public $import = [ 'table' => 'appapikey', 'connection' => 'ctrller' ];
    public $fields = [
        'id' => ['type' => 'string'],
        'key' => ['type' => 'string' ],
        'label' => 'text',
        'creationdstamp' => 'datetime',
        '_constraints' => [
            'primary' => ['type' => 'primary', 'columns' => ['id']]
        ]
    ];

    public $records = [ 
        [
            'id' => '03380d5b-1b05-467a-af32-ca8aecb4dcc6',
            'key' => '53b4359a-a7d9-d6d7-9853-e569f408eae9',
            'label' => 'MyLabel',
            'creationdstamp' => '2019-11-12 15:53:48.176576+00'
        ] 
    ];
}

我一直在尝试一些事情,无论您是否使用导入,似乎总会执行一个描述(为什么$ fields存在?]

[在'$ fields'和'$ records'中填写。据我从文档中了解和经验丰富:

  1. 描述是使用原始连接完成的
  2. 已创建表'test_xxx'表并填写在'test'/'test_xxx'连接中
  3. 测试在测试(_xxx)连接的表上运行(+ $ records数据)
  4. 但是上面的装置给了我

Cake \ Database \ Exception:无法描述app_api_key。它有0列。

是否在其他地方需要'$ table = xxx'?在文档中看不到它。

无论'$ table'或'$ import'的内容是什么,这种情况都会持续发生>

在应用程序中,此表类有效:

namespace MyPlugin\Model\Table;

use Cake\ORM\Table;

class AppApiKeyTable extends Table { 
    public function initialize(array $config) {
        parent::initialize($config);
        $this->setTable('appapikey');
    }

    public static function defaultConnectionName() {
        return 'ctrller';
    }
}

TestCase调用了我要测试的功能:

namespace MyPlugin\Auth\ApiKey;

use Cake\ORM\TableRegistry;

class ApiKeyAuthenticator {
    public function getApiKeyInfo($pKey) {
        return TableRegistry::getTableLocator()->get('AppApiKey')->findByKey($pKey)->first();
    }
}

[另外发现奇怪的是,在另一个治具中,我不得不使用'$ import',没有它我也会得到一个:]]

Cake \ Database \ Exception:无法描述软件ID。它有0列

灯具是:

namespace MyPlugin\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

class SoftwareidFixture extends TestFixture {
    //public $import = [ 'table' => 'softwareid', 'connection' => 'default' ];
    public $fields = [
        'id' => ['type' => 'string'],
        'softwareid' => ['type' => 'string' ],
        'name' => 'text',
        'created' => 'datetime',
        '_constraints' => [
            'primary' => ['type' => 'primary', 'columns' => ['id']]
        ]
    ];

    public $records = [ 
        [
            'id' => '5e26b612-2b74-40ed-aaa5-b48cb4b1d946',
            'softwareid' => '228ebfe2-4a2b-49ec-ab75-43f44007b68d',
            'name' => 'MySoftwarePackage',
            'created' => '2019-10-31 12:08:10+00'
        ] 
    ];
}

上面是默认连接中的表,'$ fields'和'$ records'应该足够了吗,不需要定义'$ import'

我刚开始使用CakePHP进行测试,现在正在创建一个新插件,希望为其创建单元测试。新的插件'MyPlugin'连接到多个数据库,其中一个数据库与....>

因此,这对导入的两个表都起作用,在我的情况下,这是一个很好的解决方法,因为一旦迁移完成(长期项目),将有70到100个表

[最简单的方法是定义模型,它在文档中,但是对文档中提到的'$ table','$ connection'和'$ fields'视而不见。

所以治具以下列结尾:

namespace MyPlugin\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

class AppApiKeyFixture extends TestFixture {
    public $import = [ 'model' => 'AppApiKey', 'connection' => 'ctrller' ];
    public $records = [ 
        [
            'id' => '03380d5b-1b05-467a-af32-ca8aecb4dcc6',
            'key' => '53b4359a-a7d9-d6d7-9853-e569f408eae9',
            'label' => 'MyLabel',
            'creationdstamp' => '2019-11-12 15:53:48.176576+00'
        ] 
    ];
}

对于任何在使用不同的表名和连接进行单元测试时遇到问题的人,引用模型似乎是可行的方法。

cakephp phpunit fixtures
1个回答
0
投票

因此,这对导入的两个表都起作用,在我的情况下,这是一个很好的解决方法,因为一旦迁移完成(长期项目),将有70到100个表

[最简单的方法是定义模型,它在文档中,但是对文档中提到的'$ table','$ connection'和'$ fields'视而不见。

© www.soinside.com 2019 - 2024. All rights reserved.