如何使用 fixtures 测试 cake3 插件
How to test cake3 plugins with fixtures
我在尝试使用固定装置测试插件时遇到问题。
我已经在 tests/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'
],
];
}
然后在 TestCase 中我用 public 变量 fixtures 加载 fixtures:
<?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 但它引发了很多错误。
我也用谷歌搜索过,但我找不到任何关于插件测试的信息,也找不到官方文档。
有什么想法可以实现吗?
好吧,最后我猜想错误是在生成作曲家自动加载器的过程中发生的。我一直在查看我的 Test\Fixture\ 命名空间如何未添加到 autoload_psr4.php
或者 运行 composer dump-autoload
。
所以我解决这个问题的方法是在插件的 composer.json
和 运行 composer update
上添加正确的行来很好地生成自动加载。
注意 composer dump-autoload
在供应商下安装插件时不会更新您的插件 psr4 路由。
我在尝试使用固定装置测试插件时遇到问题。
我已经在 tests/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'
],
];
}
然后在 TestCase 中我用 public 变量 fixtures 加载 fixtures:
<?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 但它引发了很多错误。
我也用谷歌搜索过,但我找不到任何关于插件测试的信息,也找不到官方文档。
有什么想法可以实现吗?
好吧,最后我猜想错误是在生成作曲家自动加载器的过程中发生的。我一直在查看我的 Test\Fixture\ 命名空间如何未添加到 autoload_psr4.php
或者 运行 composer dump-autoload
。
所以我解决这个问题的方法是在插件的 composer.json
和 运行 composer update
上添加正确的行来很好地生成自动加载。
注意 composer dump-autoload
在供应商下安装插件时不会更新您的插件 psr4 路由。