CakePHP3插件测试Class' Cake \ TestSuite \ IntegrationTestCase'未找到

时间:2015-06-05 12:43:59

标签: cakephp phpunit composer-php cakephp-3.0

我是使用此框架的新手,我在测试插件时发现了一个问题。

PHP Fatal error:  Class 'Cake\TestSuite\IntegrationTestCase' not found in /var/www/MyApp/plugins/MyPlugin/tests/TestCase/Controller/UsersControllerTest.php on line 11

运行主应用程序效果很好,但是我对插件的所有测试类都有同样的问题。

测试课程的负责人类似于:

<?php
namespace MyPlugin\Test\TestCase\Controller;

use Cake\TestSuite\IntegrationTestCase;
use MyPlugin\Controller\UsersController;

class UsersControllerTest extends IntegrationTestCase
{

插件的phpunit.xml.dist:

<?xml version="1.0" encoding="UTF-8"?><phpunit
colors="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="./tests/bootstrap.php"
>
<php>
    <ini name="memory_limit" value="-1"/>
    <ini name="apc.enable_cli" value="1"/>
</php>

<!-- Add any additional test suites you want to run here -->
<testsuites>
    <testsuite name="MyPlugin Test Suite">
        <directory>./tests/TestCase</directory>
    </testsuite>
</testsuites>

<!-- Setup a listener for fixtures -->
<listeners>
    <listener
    class="\Cake\TestSuite\Fixture\FixtureInjector"
    file="../../vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php">
        <arguments>
            <object class="\Cake\TestSuite\Fixture\FixtureManager" />
        </arguments>
    </listener>
</listeners>

<!-- Prevent coverage reports from looking in tests and vendors -->
<filter>
    <blacklist>
        <directory suffix=".php">./vendor/</directory>
        <directory suffix=".ctp">./vendor/</directory>

        <directory suffix=".php">./tests/</directory>
        <directory suffix=".ctp">./tests/</directory>
    </blacklist>
</filter></phpunit>

还有composer.json:

{
"name": "your-name-here/MyPlugin",
"description": "MyPlugin plugin for CakePHP",
"type": "cakephp-plugin",
"require": {
    "php": ">=5.4.16",
    "cakephp/cakephp": "~3.0"
},
"require-dev": {
    "phpunit/phpunit": "*"
},
"autoload": {
    "psr-4": {
        "MyPlugin\\": "src"
    }
},
"autoload-dev": {
    "psr-4": {
        "MyPlugin\\Test\\": "tests",
        "Cake\\Test\\": "/var/www/MyApp/vendor/cakephp/cakephp/tests",
    }
}
}

提前致谢!! ;)

3 个答案:

答案 0 :(得分:1)

最后我修好了。在插件的phpunit.xml.dist文件中,我将引导点指向主应用程序之一:

<?xml version="1.0" encoding="UTF-8"?><phpunit
colors="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="../../config/bootstrap.php"
>

此外,在同一个文件中,不要忘记将同一文件中的监听器指向主应用程序的供应商,这样您就不必在插件中重新安装整个CakePHP套件:

<!-- Setup a listener for fixtures -->
<listeners>
    <listener
    class="\Cake\TestSuite\Fixture\FixtureInjector"
    file="../../vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php">
        <arguments>
            <object class="\Cake\TestSuite\Fixture\FixtureManager" />
        </arguments>
    </listener>
</listeners>

默认情况下,在烘焙插件时,这两个元素的设置方式并不正确。

答案 1 :(得分:0)

您需要在插件文件夹中运行composer install,以便在<your-plugin>/vendor/cakephp/cakephp下安装CakePHP库

答案 2 :(得分:0)

从CakePHP 3.7开始,您应该使用

Application::addPlugin()

因此在setUp()方法中,它将类似于:

public function setUp()
{
    parent::setUp();
    (new \App\Application(APP . 'config' . DS))->addPlugin('YourPlugin');
    //other options
}