php - 如何使用装置测试 cake3 插件

标签 php cakephp testing cakephp-3.0

我在尝试使用固定装置测试插件时遇到了麻烦。

我已经在tests/Fixture下创建了fixture文件,其结构作为文档:

namespace Files\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

/**
 * FiledFixture
 *
 */
class FiledFixture extends TestFixture {

    /**
     * Table name
     *
     * @var string
     */
    public $table = 'filed';

    /**
     * Fields
     *
     * @var array
     */
    public $fields = [
        'id' => [
            'type' => 'integer',
            'length' => 11,
            ...
        ],
        'foreign_key' => [
            'type' => 'integer',
            ...
        ],
        ...
    ];

    /**
     * Records
     *
     * @var array
     */
    public $records = [
        [
            'id' => 1,
            'foreign_key' => 1,
            'file_id' => 1,
            'filedtype_id' => 1,
            'model' => 'Lorem ipsum dolor sit amet',
            'language' => 'Lore',
            'created' => '2014-11-01 19:21:54',
            'modified' => '2014-11-01 19:21:54'
        ],
    ];
}

然后在测试用例中我使用公共(public)变量固定装置加载固定装置:

<?php
namespace Files\Test\TestCase\Model\Table;

use Cake\ORM\TableRegistry;
use Files\Model\Table\FiledTable;
use Cake\TestSuite\TestCase;

/**
 * Files\Model\Table\FiledTable Test Case
 */
class FiledTableTest extends TestCase {

    /**
     * Fixtures
     *
     * @var array
     */
    public $fixtures = [
        'plugin.files.filed',
        'plugin.files.files',
        'plugin.files.filedtypes',
        'plugin.files.thumbs'
    ];
...

当我尝试运行 phpunit 时,出现下一个错误:

..Exception: Referenced fixture class "Files\Test\Fixture\FiledFixture" not found. Fixture "plugin.files.filed" was referenced in test case "Files\Test\TestCase\Model\Table\FiledTableTest". in [/home/genar/src/metropolitan-web-2015/vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureManager.php, line 180]

我尝试创建一个自定义 bootstrap.php 进行测试,但我不知道那里必须有什么。我复制了这个https://github.com/Xety/Cake3-Upload/blob/master/tests/bootstrap.php但它会引发很多错误。

我也用谷歌搜索过,但找不到任何有关插件测试的信息,也找不到官方文档。

关于如何实现这一目标有什么想法吗?

最佳答案

好吧,最后我认为错误出在生成 Composer 自动加载器的方式上。我一直在研究如何在运行 composer dump-autoloadautoload_psr4.php 上添加我的 Test\Fixture\命名空间。 因此,我解决此问题的方法是在插件的 composer.json 上添加正确的行,并运行 composer update 以很好地生成自动加载。

注意,当插件安装在供应商下时,composer dump-autoload 不会更新您的插件 psr4 路由。

关于php - 如何使用装置测试 cake3 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31056333/

相关文章:

php - 访问对象变量上的关系会更改变量

php - symfony 2 中搜索功能的最佳方式

javascript - 如何在ajax url中使用laravel路由

php - 用于 php5.5.0 的内存缓存

cakephp - 在自定义组件中使用 session 组件

php - 在 CakePHP 中使用下拉菜单时的外键约束

cakephp - 我有一些关于加入 Cakephp 的问题

php - 通过测试中的方法传递请求

java - 如何在 Hazelcast 中实现自定义时钟进行单元测试?

php - 测试2个mysql数据库之间的内容