设置CakePHP 3插件测试 [英] Setting up CakePHP 3 Plugin testing

查看:210
本文介绍了设置CakePHP 3插件测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 bin / cake bake plugin PluginName 创建一个插件。它的一部分是它创建 phpunit.xml.dist ,但是bake不会创建文件夹结构或 tests / bootstrap.php 档案。



问题



当我运行 phpunit 时发出未执行测试消息:

  $ phpunit 
PHPUnit 5.1.3由Sebastian Bergmann和贡献者。

时间:239 ms,内存:4.50Mb

不执行测试!

背景信息



我在我的插件文件夹 tests / TestCase 下创建了我的测试。我不认为他们是这个问题,但我会在最后发布。



我使用默认的 phpunit.xml .dist 文件,我正在使用 tests / bootstrap.php

  $ findRoot = function($ root){
do {
$ lastRoot = $ root;
$ root = dirname($ root);
if(is_dir($ root。'/ vendor / cakephp / cakephp')){
return $ root;
}
} while($ root!== $ lastRoot);
throw new Exception(找不到应用程序的根,无法运行测试);
};
$ root = $ findRoot(__ FILE__);
unset($ findRoot);
chdir($ root)

define('ROOT',$ root);
define('APP_DIR','App');
define('WEBROOT_DIR','webroot');
define('APP',ROOT。'/ tests / App /');
define('CONFIG',ROOT。'/ tests / config /');
define('WWW_ROOT',ROOT。DS。WEBROOT_DIR。DS);
define('TESTS',ROOT。DS。'tests'。DS);
define('TMP',ROOT。DS。'tmp'。DS);
define('LOGS',TMP。'logs'。DS);
define('CACHE',TMP。'cache'。DS);
define('CAKE_CORE_INCLUDE_PATH',ROOT。'/ vendor / cakephp / cakephp');
define('CORE_PATH',CAKE_CORE_INCLUDE_PATH。DS);
define('CAKE',CORE_PATH。'src'。DS);

需要ROOT。 '/vendor/autoload.php';
需要CORE_PATH。 'config / bootstrap.php';

单元测试



这存在于 tests / TestCase / Color.php

 <?php 

namespace Contrast \TestCase;

使用Cake \TestSuite\TestCase;
use Contrast \Text\Color;

/ **
*对比文本颜色测试
* /
class TextColorTest extends TestCase
{
/ **
* @test
* @return void
* /
public function testShouldHandleVarietyOfColors()
{
#返回黑色
$ this-> assertEquals :: getBlackWhiteContrast('#FFFFFF'),'black');

//为什么你不会失败?
$ this-> assertEquals(true,false);
}


解决方案

>

基本命名空间应该是 Contrast \Test ,这也是bake应该添加到您的应用程序 composer.json files autoload autoload-dev 已烘焙的插件 composer.json 文件。如果您拒绝编辑yout composer.json 文件,则应手动添加自动载入条目

 autoload:{
psr-4:{
// ...
Contrast\\:./plugins/Contrast / src
}
},
autoload-dev:{
psr-4:{
// ...
Contrast \\Test\\:./plugins/Contrast/tests
}
},

并重新转储自动加载器

  $ composer dump-autoload 

因此,您的示例测试的命名空间应为 Contrast\ Test \TestCase



此外,测试文件需要后缀为 Test PHPUnit识别它们的顺序。为了使文件可以自动加载,你应该坚持PSR-4,即文件应该具有与类相同的名称,即你的测试文件应该命名为 TextColorTest.php ,而不是 Color.php



测试作为应用程序的一部分



当测试一个插件作为应用程序的一部分时,不一定需要一个引导文件在插件测试(bake应该,实际上生成一个),因为你可以运行它们与您的应用程序的配置,它有一个测试引导文件( tests / bootstrap.php ),包括您的应用程序引导文件( config / bootstrap。因此,测试将从您的应用程序基本文件夹运行,您必须传递插件路径,例如

/ p>

  $ vendor / bin / phpunit plugins / Contrast 

或者您可以在应用程序的主要phpunit配置文件中添加一个额外的插件测试套件,其中<您的插件套件 - >

 < testsuite name = Contrast Test Suite> 
< directory> ./ plugins / Contrast / tests / TestCase< / directory>
< / testsuite>

这样,插件测试将与您的应用测试一起运行。



最后,您还可以从plugin目录运行测试,假定存在正确的引导程序文件。默认值为:

 <?php 
/ **
*测试套件对比度。
*
*此函数用于查找CakePHP的位置,无论CakePHP
*是否已作为插件的依赖关系安装,或者插件本身是
*安装为应用程序的依赖性。
* /
$ findRoot = function($ root){
do {
$ lastRoot = $ root;
$ root = dirname($ root)
if(is_dir($ root。'/ vendor / cakephp / cakephp')){
return $ root;
}
} while($ root!== $ lastRoot);

throw new Exception(找不到应用程序的根目录,无法运行测试);
};
$ root = $ findRoot(__ FILE__);
unset($ findRoot);

chdir($ root);
require $ root。 '/config/bootstrap.php';

另请参阅





测试独立开发的插件



以独立方式开发插件时,这是当你真的需要一个引导文件设置环境,这是由bake生成的插件 composer.json 文件正在使用的地方。默认情况下,后者看起来像

  {
name:your-name- here / Contrast
description:CakePHP的对比插件,
type:cakephp-plugin,
require:{
php:> 5.4.16,
cakephp / cakephp:〜3.0
},
require-dev:{
phpunit / phpunit:* b $ b},
autoload:{
psr-4:{
Contrast\\:src
}
} ,
autoload-dev:{
psr-4:{
Contrast \\Test\\:tests,
Cake \\Test\\:./vendor/cakephp/cakephp/tests
}
}
}

烘焙插件时生成的测试引导文件不能开箱即用,因为它会尝试加载 config / bootstrap.php 插件文件,默认情况下不存在。



测试引导文件需要做什么取决于插件当然在做什么,但至少它应该




  • 定义基本常量和配置使用的核心

  • 需要composer自动加载器

  • 需要CakePHP核心引导程序文件




例如,这里有一个 tests / bootstrap.php 应用程序模板的副本,即它基本上配置一个完整的应用程序环境(应用程序定义为在中找到)测试/ TestApp 文件夹):

  //从`config / paths.php`

if(!defined('DS')){
define('DS',DIRECTORY_SEPARATOR);
}
define('ROOT',dirname(__ DIR__));
define('APP_DIR','test_app');
define('APP',ROOT。DS。'tests'。DS。APP_DIR。DS);
define('CONFIG',ROOT。DS。'config'。DS);
define('WWW_ROOT',APP。'webroot'。DS);
define('TESTS',ROOT。DS。'tests'。DS);
define('TMP',ROOT。DS。'tmp'。DS);
define('LOGS',TMP。'logs'。DS);
define('CACHE',TMP。'cache'。DS);
define('CAKE_CORE_INCLUDE_PATH',ROOT。DS。'vendor'。DS。'cakephp'。DS。'cakephp');
define('CORE_PATH',CAKE_CORE_INCLUDE_PATH。DS);
define('CAKE',CORE_PATH。'src'。DS);


//从`config / app.default.php`和`config / bootstrap.php`

使用Cake \Cache \Cache;
使用Cake\Console\ConsoleErrorHandler;
使用Cake \Core\App;
使用Cake\Core\Configure;
使用Cake\Core\Configure\Engine\PhpConfig;
使用Cake \Core\Plugin;
使用Cake \Database\Type;
使用Cake\Datasource\ConnectionManager;
使用Cake\Error\ErrorHandler;
使用Cake \Log\Log;
使用Cake\Mailer\Email;
使用Cake\Network\Request;
使用Cake \Routing\DispatcherFactory;
使用Cake\Utility\Inflector;
使用Cake\Utility\Security;

需要ROOT。 DS。 'vendor'。 DS。 'autoload.php';
需要CORE_PATH。 'config'。 DS。 'bootstrap.php';

$ config = [
'debug'=> true,

'App'=> [
'namespace'=> 'App',
'encoding'=> env('APP_ENCODING','UTF-8'),
'defaultLocale'=> env('APP_DEFAULT_LOCALE','en_US'),
'base'=> false,
'dir'=> 'src',
'webroot'=> 'webroot',
'wwwRoot'=> WWW_ROOT,
'fullBaseUrl'=> false,
'imageBaseUrl'=> 'img /',
'cssBaseUrl'=> 'css /',
'jsBaseUrl'=> 'js /',
'paths'=> [
'plugins'=> [根 。 DS。 'plugins'。 DS],
'templates'=> [APP。 '模板'。 DS],
'locales'=> [APP。 'locale'。 DS],
],
],

'Asset'=> [
//'timestamp'=> true,
],

'Security'=> [
'salt'=> env('SECURITY_SALT','_SALT__'),
],

'Cache'=> [
'default'=> [
'className'=> 'File',
'path'=> CACHE,
'url'=> env('CACHE_DEFAULT_URL',null),
],

'_cake_core_'=> [
'className'=> 'File',
'prefix'=> 'myapp_cake_core_',
'path'=> CACHE。 'persistent /',
'serialize'=> true,
'duration'=> '+2分钟',
'url'=> env('CACHE_CAKECORE_URL',null),
],

'_cake_model_'=> [
'className'=> 'File',
'prefix'=> 'myapp_cake_model_',
'path'=> CACHE。 'models /',
'serialize'=> true,
'duration'=> '+2分钟',
'url'=> env('CACHE_CAKEMODEL_URL',null),
],
],

'Error'=> [
'errorLevel'=> E_ALL& 〜E_DEPRECATED,
'exceptionRenderer'=> 'Cake\Error\ExceptionRenderer',
'skipLog'=> [],
'log'=> true,
'trace'=> true,
],

'EmailTransport'=> [
'default'=> [
'className'=> 'mail',
//以下密钥用于SMTP传输
'host'=> 'localhost',
'port'=> 25,
'timeout'=> 30,
'username'=> 'user',
'password'=> 'secret',
'client'=> null,
'tls'=> null,
'url'=> env('EMAIL_TRANSPORT_DEFAULT_URL',null),
],
],

'电子邮件'=> [
'default'=> [
'transport'=> 'default',
'from'=> 'you @ localhost',
//'charset'=> 'utf-8',
//'headerCharset'=> 'utf-8',
],
],

'Datasources'=> [
'test'=> [
'className'=> 'Cake \Database \Connection',
'driver'=> 'Cake\Database\Driver\Mysql',
'persistent'=> false,
'host'=> 'localhost',
//'port'=> 'non_standard_port_number',
'username'=> 'my_app',
'password'=> 'secret',
'database'=> 'test_myapp',
'encoding'=> 'utf8',
'timezone'=> 'UTC',
'cacheMetadata'=> true,
'quoteIdentifiers'=> false,
'log'=> false,
//'init'=> ['SET GLOBAL innodb_stats_on_metadata = 0'],
'url'=> env('DATABASE_TEST_URL',null),
],
],

'Log'=> [
'debug'=> [
'className'=> 'Cake\Log\Engine\FileLog',
'path'=> LOGS,
'file'=> 'debug',
'levels'=> ['notice','info','debug'],
'url'=> env('LOG_DEBUG_URL',null),
],
'error'=> [
'className'=> 'Cake\Log\Engine\FileLog',
'path'=> LOGS,
'file'=> 'error',
'levels'=> ['warning','error','critical','alert','emergency'],
'url'=> env('LOG_ERROR_URL',null),
],
],

'Session'=> [
'defaults'=> 'php',
],
];
Configure :: write($ config);

date_default_timezone_set('UTC');
mb_internal_encoding(Configure :: read('App.encoding'));
ini_set('intl.default_locale',Configure :: read('App.defaultLocale'));

Cache :: config(Configure :: consume('Cache'));
ConnectionManager :: config(Configure :: consume('Datasources'));
Email :: configTransport(Configure :: consume('EmailTransport'));
Email :: config(Configure :: consume('Email'));
Log :: config(Configure :: consume('Log'));
Security :: salt(Configure :: consume('Security.salt'));

DispatcherFactory :: add('Asset');
DispatcherFactory :: add('Routing');
DispatcherFactory :: add('ControllerFactory');

Type :: build('time')
- > useImmutable()
- > useLocaleParser
Type :: build('date')
- > useImmutable()
- > useLocaleParser
Type :: build('datetime')
- > useImmutable()
- > useLocaleParser


//使用自定义路径加载/注册插件

Plugin :: load('Contrast',['path'=> ROOT] );

为了让类可以从测试应用程序文件夹自动加载,你必须添加一个相应的在 composer.json 文件中自动加载条目(并重新转储自动加载器),如:

 autoload-dev:{
psr-4:{
Contrast \\Test\\:tests,
Contrast \\TestApp\\:tests / test_app / src,//<这里我们去
Cake\\Test\\:./vendor/cakephp/cakephp/tests
}
}

现在使用bake创建的插件,环境配置和安装的CakePHP和PHPUnit依赖项,你应该能够运行你的测试您将使用应用程序,即

  $ vendor / bin / phpunit 


I've used bin/cake bake plugin PluginName to create a plugin. Part of it is that it creates phpunit.xml.dist, but bake doesn't create the folder structure or tests/bootstrap.php file that's required.

The Problem

I get a "No tests executed" message when I run phpunit:

$ phpunit
PHPUnit 5.1.3 by Sebastian Bergmann and contributors.

Time: 239 ms, Memory: 4.50Mb

No tests executed!

Background information

I've created my tests in my plugin folder under tests/TestCase. I don't think they are the issue, but I'll post them at the end.

I'm using the default phpunit.xml.dist file, and I'm using this for tests/bootstrap.php:

$findRoot = function ($root) {
    do {
        $lastRoot = $root;
        $root = dirname($root);
        if (is_dir($root . '/vendor/cakephp/cakephp')) {
            return $root;
        }
    } while ($root !== $lastRoot);
    throw new Exception("Cannot find the root of the application, unable to run tests");
};
$root = $findRoot(__FILE__);
unset($findRoot);
chdir($root);

define('ROOT', $root);
define('APP_DIR', 'App');
define('WEBROOT_DIR', 'webroot');
define('APP', ROOT . '/tests/App/');
define('CONFIG', ROOT . '/tests/config/');
define('WWW_ROOT', ROOT . DS . WEBROOT_DIR . DS);
define('TESTS', ROOT . DS . 'tests' . DS);
define('TMP', ROOT . DS . 'tmp' . DS);
define('LOGS', TMP . 'logs' . DS);
define('CACHE', TMP . 'cache' . DS);
define('CAKE_CORE_INCLUDE_PATH', ROOT . '/vendor/cakephp/cakephp');
define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
define('CAKE', CORE_PATH . 'src' . DS);

require ROOT . '/vendor/autoload.php';
require CORE_PATH . 'config/bootstrap.php';

The Unit Test

This lives in tests/TestCase/Color.php

<?php

namespace Contrast\TestCase;

use Cake\TestSuite\TestCase;
use Contrast\Text\Color;

/**
 * Contrast Text Color tests
 */
class TextColorTest extends TestCase
{
    /**
     * @test
     * @return void
     */
    public function testShouldHandleVarietyOfColors()
    {
        # Returns black
        $this->assertEquals(Color::getBlackWhiteContrast('#FFFFFF'), 'black');

        // Why won't you fail??
        $this->assertEquals(true, false);
    }

解决方案

First things first

The base namespace should be Contrast\Test, which is also what bake should have added to your applications composer.json files autoload and autoload-dev sections, as well as to the baked plugins composer.json file. In case you denied bake to edit yout composer.json file, you should add the autoload entry manually

"autoload": {
    "psr-4": {
        // ...
        "Contrast\\": "./plugins/Contrast/src"
    }
},
"autoload-dev": {
    "psr-4": {
        // ...
        "Contrast\\Test\\": "./plugins/Contrast/tests"
    }
},

and re-dump the autoloader

$ composer dump-autoload

So the namespace for your example test should be Contrast\Test\TestCase.

Also test files need to be postfixed with Test in order for PHPUnit to recognize them. And in order for the files to be autoloadable, you should stick to PSR-4, ie the files should have the same name as the class, ie your test file should be named TextColorTest.php, not Color.php.

Testing as a part of an application

When testing a plugin as a part on an application, there is not necessarily a need for a bootstrap file in the plugin tests (bake should, actually does generate one though), as you could run them with the configuration of your application, which has a test bootstrap file (tests/bootstrap.php) that includes your applications bootstrap file (config/bootstrap.php).

Consequently the tests would be run from your applications base folder, and you'd have to pass the plugin path, like

$ vendor/bin/phpunit plugins/Contrast

or you'd add an additional plugin testsuite in your apps main phpunit configuration file, where it says <!-- Add your plugin suites -->

<testsuite name="Contrast Test Suite">
    <directory>./plugins/Contrast/tests/TestCase</directory>
</testsuite>

That way the plugin tests will be run together with your app tests.

Finally, you can also run the tests from the plugin directory, given that a proper bootstrap file exists. The default one currently looks like:

<?php
/**
 * Test suite bootstrap for Contrast.
 *
 * This function is used to find the location of CakePHP whether CakePHP
 * has been installed as a dependency of the plugin, or the plugin is itself
 * installed as a dependency of an application.
 */
$findRoot = function ($root) {
    do {
        $lastRoot = $root;
        $root = dirname($root);
        if (is_dir($root . '/vendor/cakephp/cakephp')) {
            return $root;
        }
    } while ($root !== $lastRoot);

    throw new Exception("Cannot find the root of the application, unable to run tests");
};
$root = $findRoot(__FILE__);
unset($findRoot);

chdir($root);
require $root . '/config/bootstrap.php';

See also

Testing standalone developed plugins

When developing plugins in a standalone fashion, this is when you really need a bootstrap file that sets up the environment, and this is where the plugins composer.json file generated by bake is being used. By default the latter would look like

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

The test bootstrap file generated when baking a plugin doesn't work out of the box, as all it would do would be trying to load the config/bootstrap.php file of your plugin, which by default doesn't even exist.

What the test bootstrap file needs to do depends on what the plugin is doing of course, but at the very least it should

  • define the basic constants and configuration that is used by the core
  • require the composer autoloader
  • require the CakePHP core bootstrap file
  • and load/register your plugin.

For example purposes, here's a tests/bootstrap.php example with a copy of what the current cakephp/app application template does, ie it basically configures a full application environment (with the application defined to be found in the tests/TestApp folder):

// from `config/paths.php`

if (!defined('DS')) {
    define('DS', DIRECTORY_SEPARATOR);
}
define('ROOT', dirname(__DIR__));
define('APP_DIR', 'test_app');
define('APP', ROOT . DS . 'tests' . DS . APP_DIR . DS);
define('CONFIG', ROOT . DS . 'config' . DS);
define('WWW_ROOT', APP . 'webroot' . DS);
define('TESTS', ROOT . DS . 'tests' . DS);
define('TMP', ROOT . DS . 'tmp' . DS);
define('LOGS', TMP . 'logs' . DS);
define('CACHE', TMP . 'cache' . DS);
define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . 'vendor' . DS . 'cakephp' . DS . 'cakephp');
define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
define('CAKE', CORE_PATH . 'src' . DS);


// from `config/app.default.php` and `config/bootstrap.php`

use Cake\Cache\Cache;
use Cake\Console\ConsoleErrorHandler;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Configure\Engine\PhpConfig;
use Cake\Core\Plugin;
use Cake\Database\Type;
use Cake\Datasource\ConnectionManager;
use Cake\Error\ErrorHandler;
use Cake\Log\Log;
use Cake\Mailer\Email;
use Cake\Network\Request;
use Cake\Routing\DispatcherFactory;
use Cake\Utility\Inflector;
use Cake\Utility\Security;

require ROOT . DS . 'vendor' . DS . 'autoload.php';
require CORE_PATH . 'config' . DS . 'bootstrap.php';

$config = [
    'debug' => true,

    'App' => [
        'namespace' => 'App',
        'encoding' => env('APP_ENCODING', 'UTF-8'),
        'defaultLocale' => env('APP_DEFAULT_LOCALE', 'en_US'),
        'base' => false,
        'dir' => 'src',
        'webroot' => 'webroot',
        'wwwRoot' => WWW_ROOT,
        'fullBaseUrl' => false,
        'imageBaseUrl' => 'img/',
        'cssBaseUrl' => 'css/',
        'jsBaseUrl' => 'js/',
        'paths' => [
            'plugins' => [ROOT . DS . 'plugins' . DS],
            'templates' => [APP . 'Template' . DS],
            'locales' => [APP . 'Locale' . DS],
        ],
    ],

    'Asset' => [
        // 'timestamp' => true,
    ],

    'Security' => [
        'salt' => env('SECURITY_SALT', '__SALT__'),
    ],

    'Cache' => [
        'default' => [
            'className' => 'File',
            'path' => CACHE,
            'url' => env('CACHE_DEFAULT_URL', null),
        ],

        '_cake_core_' => [
            'className' => 'File',
            'prefix' => 'myapp_cake_core_',
            'path' => CACHE . 'persistent/',
            'serialize' => true,
            'duration' => '+2 minutes',
            'url' => env('CACHE_CAKECORE_URL', null),
        ],

        '_cake_model_' => [
            'className' => 'File',
            'prefix' => 'myapp_cake_model_',
            'path' => CACHE . 'models/',
            'serialize' => true,
            'duration' => '+2 minutes',
            'url' => env('CACHE_CAKEMODEL_URL', null),
        ],
    ],

    'Error' => [
        'errorLevel' => E_ALL & ~E_DEPRECATED,
        'exceptionRenderer' => 'Cake\Error\ExceptionRenderer',
        'skipLog' => [],
        'log' => true,
        'trace' => true,
    ],

    'EmailTransport' => [
        'default' => [
            'className' => 'Mail',
            // The following keys are used in SMTP transports
            'host' => 'localhost',
            'port' => 25,
            'timeout' => 30,
            'username' => 'user',
            'password' => 'secret',
            'client' => null,
            'tls' => null,
            'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
        ],
    ],

    'Email' => [
        'default' => [
            'transport' => 'default',
            'from' => 'you@localhost',
            //'charset' => 'utf-8',
            //'headerCharset' => 'utf-8',
        ],
    ],

    'Datasources' => [
        'test' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'host' => 'localhost',
            //'port' => 'non_standard_port_number',
            'username' => 'my_app',
            'password' => 'secret',
            'database' => 'test_myapp',
            'encoding' => 'utf8',
            'timezone' => 'UTC',
            'cacheMetadata' => true,
            'quoteIdentifiers' => false,
            'log' => false,
            //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
            'url' => env('DATABASE_TEST_URL', null),
        ],
    ],

    'Log' => [
        'debug' => [
            'className' => 'Cake\Log\Engine\FileLog',
            'path' => LOGS,
            'file' => 'debug',
            'levels' => ['notice', 'info', 'debug'],
            'url' => env('LOG_DEBUG_URL', null),
        ],
        'error' => [
            'className' => 'Cake\Log\Engine\FileLog',
            'path' => LOGS,
            'file' => 'error',
            'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
            'url' => env('LOG_ERROR_URL', null),
        ],
    ],

    'Session' => [
        'defaults' => 'php',
    ],
];
Configure::write($config);

date_default_timezone_set('UTC');
mb_internal_encoding(Configure::read('App.encoding'));
ini_set('intl.default_locale', Configure::read('App.defaultLocale'));

Cache::config(Configure::consume('Cache'));
ConnectionManager::config(Configure::consume('Datasources'));
Email::configTransport(Configure::consume('EmailTransport'));
Email::config(Configure::consume('Email'));
Log::config(Configure::consume('Log'));
Security::salt(Configure::consume('Security.salt'));

DispatcherFactory::add('Asset');
DispatcherFactory::add('Routing');
DispatcherFactory::add('ControllerFactory');

Type::build('time')
    ->useImmutable()
    ->useLocaleParser();
Type::build('date')
    ->useImmutable()
    ->useLocaleParser();
Type::build('datetime')
    ->useImmutable()
    ->useLocaleParser();


// finally load/register the plugin using a custom path

Plugin::load('Contrast', ['path' => ROOT]);

In order for classes to be autoloadable from the test application folder, you'd have to add a corresponding autoload entry in your composer.json file (and again re-dump the autoloader), like:

    "autoload-dev": {
        "psr-4": {
            "Contrast\\Test\\": "tests",
            "Contrast\\TestApp\\": "tests/test_app/src", // < here we go
            "Cake\\Test\\": "./vendor/cakephp/cakephp/tests"
        }
    }

So now with the plugin been created using bake, the environment configured and the CakePHP and PHPUnit dependencies installed, you should be able to run your tests as you would with an application, ie

$ vendor/bin/phpunit

这篇关于设置CakePHP 3插件测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆