fr3nch13 / cakephp-pta
一个用于测试插件时模拟通用应用的 CakePHP 插件。
Requires
- php: ~7.4||~8.0
- cakephp/bake: ^2.8
- cakephp/cakephp: ~4.4.8
- cakephp/cakephp-codesniffer: ~4.6
- cakephp/debug_kit: ~4.9
- cakephp/migrations: ~3.6
- cakephp/plugin-installer: ~1.3
- dereuromark/cakephp-ide-helper: ^1.18
- mobiledetect/mobiledetectlib: ^2.8
- mockery/mockery: ^1.5
- phpcompatibility/php-compatibility: ^9.3
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9.5
- staabm/annotate-pull-request-from-checkstyle: ^1.8
- symfony/dotenv: ~5.0||~6.0
- vishnubob/wait-for-it: dev-master
README
此 CakePHP 插件用于在测试您的 CakePHP 插件时模拟通用应用。
我使用 cakephp/app 作为最新的骨架应用,我的许多插件共享许多相同的内容。此插件旨在保持它们的简洁。
例如:test/bootstrap.php
文件。当需要更改/添加该文件中的内容时,必须在超过 10 个 CakePHP 插件中执行此操作。只需在这里放置公共内容,并包含此处的 tests/plugin_bootstrap.php
,就像 baked 插件与核心的 test/bootstrap.php
一样。
安装
要使用此插件的引导程序,请将以下行添加到您的 tests/bootstrap.php
文件中的底部附近。
// define any constants you need to overwrite above these lines. $root = dirname(__DIR__); chdir($root); require_once $root . '/vendor/fr3nch13/cakephp-pta/tests/plugin_bootstrap.php';
用法
您不再需要将此项目的 tests/test_app
复制到您的项目中。相反,有其他方法可以将您的设置/插件等注入到此插件的测试应用程序中。
添加您的插件
要将您的插件添加到测试应用程序,您需要像这样更新您的 test/bootstrap.php
。
# test/bootstrap.php // ... code ... Configure::write('Tests.Plugins', [ 'Namespace/PluginName', // the name of your plugin 'Fr3nch13/Jira', // Using one of my plusins as an example ]); // plugins just for the command line Configure::write('Tests.PluginsCli', [ 'Namespace/PluginName', // the name of your plugin ]); // ... code ... require_once $root . '/vendor/fr3nch13/cakephp-pta/tests/plugin_bootstrap.php';
从 .env 添加您的变量
在此插件的 tests/plugin_bootstrap.php
文件中,它将在您的插件的两个地方寻找您的 .env
或 .env.test
文件:
- 在:
tests/.env
或tests/.env.test
- 在:
config/.env
或config/.env.test
请参阅 tests/.env.example
作为示例。
如果您需要在包含上述 plugin_bootstrap.php
之前导入您的变量,则可以这样做。
# test/bootstrap.php // ... code ... $dotenv = new \Symfony\Component\Dotenv\Dotenv(); $dotenv->load(TESTS . '.env'); // ... code ... require_once $root . '/vendor/fr3nch13/cakephp-pta/tests/plugin_bootstrap.php';
数据库设置。
默认情况下,此插件使用内存中的 sqlite,但如果您想定义自己的数据库设置,您可以在 tests/bootstrap.php
文件中这样做,在包含 plugin_bootstrap.php
文件之前,因为数据库连接是在 plugin_bootstrap.php
文件中设置的。
# test/bootstrap.php // ... code ... Configure::write('Tests.DbConfig', [ 'className' => 'Cake\Database\Connection', 'driver' => 'Cake\Database\Driver\Mysql', 'persistent' => false, 'host' => 'database_hostname', 'port' => 'non_standard_port_number', 'username' => 'database_username', 'password' => 'database_password', 'database' => 'database_schema', 'encoding' => 'utf8', 'timezone' => 'UTC', 'flags' => [], 'cacheMetadata' => true, 'log' => false, 'quoteIdentifiers' => true, ]); // ... code ... require_once $root . '/vendor/fr3nch13/cakephp-pta/tests/plugin_bootstrap.php';
添加迁移。
您还可以在 bootstrap.php
文件中定义您的迁移。这使用来自 cakephp/migrations
的 Migrations\TestSuite\Migrator
工具。迁移在 tests/plugin_bootstrap.php
文件中运行。
# test/bootstrap.php // ... code ... Configure::write('Tests.Migrations', [ ['plugin' => 'Namespace/PluginName'], // just using some of my plugins as an example ['plugin' => 'Fr3nch13/Jira'], ['plugin' => 'Fr3nch13/Utilities'], ['plugin' => 'Fr3nch13/Excel'], ]); // ... code ... require_once $root . '/vendor/fr3nch13/cakephp-pta/tests/plugin_bootstrap.php';
添加 Html 辅助函数
如果您想包含 View/Helpers,您也可以在 bootstrap.php
文件中定义它们。这些将在 tests/test_app/src/View/AppView.php
中包含。
# test/bootstrap.php // ... code ... Configure::write('Tests.Helpers', [ 'HelperName' => ['className' => 'Namespace/PluginName.HelperName'], // loads the jira helper as an example. 'Jira' => ['className' => 'Fr3nch13/Jira.Jira'], ]); // ... code ... require_once $root . '/vendor/fr3nch13/cakephp-pta/tests/plugin_bootstrap.php';
添加中间件
您还可以定义要包含的中间件。这些将在 tests/test_app/src/Application.php
的 middleware()
方法中包含。
# test/bootstrap.php // ... code ... Configure::write('Tests.Middleware', [ 'MiddlewhereName' => [ 'config_key' => 'config_value', ], ]); // ... code ... require_once $root . '/vendor/fr3nch13/cakephp-pta/tests/plugin_bootstrap.php';
您的插件的完整 tests/bootstrap.php
示例
#tests/bootstrap.php <?php declare(strict_types=1); /** * Test suite bootstrap. * * These are the specific settings for this plugin. * This uses fr3nch13/cakephp-pta to provide a generic application for testing. * Setting passed to cakephp-pta's bootstrap and application are defined here. */ use Cake\Core\Configure; // Configure your stuff here for the plugin_bootstrap.php below. define('TESTS', __DIR__ . DS); $dotenv = new \Symfony\Component\Dotenv\Dotenv(); $dotenv->load(TESTS . '.env'); Configure::write('Tests.DbConfig', [ 'className' => 'Cake\Database\Connection', 'driver' => 'Cake\Database\Driver\Mysql', 'persistent' => false, 'host' => env('CI_HOST', null), 'username' => env('CI_USERNAME', null), 'password' => env('CI_PASSWORD', null), 'database' => env('CI_DATABASE', null), 'encoding' => 'utf8', 'timezone' => 'UTC', 'flags' => [], 'cacheMetadata' => true, 'log' => false, 'quoteIdentifiers' => true, 'url' => env('DATABASE_URL', null), ]); Configure::write('Tests.Plugins', [ 'Namespace/PluginName', ]); Configure::write('Tests.Migrations', [ ['plugin' => 'Namespace/PluginName'], ]); Configure::write('Tests.Helpers', [ 'HelperName' => ['className' => 'Namespace/PluginName.HelperName'], ]); ////// Ensure we can setup an environment for the Test Application instance. $root = dirname(__DIR__); chdir($root); require_once $root . '/vendor/fr3nch13/cakephp-pta/tests/plugin_bootstrap.php';
版本兼容性
主要版本锁定到 CakePHP 的主要版本。
- PTA 1.x 锁定到 CakePHP ^3.8
- PTA 2.x 锁定到 CakePHP ^4.0 并需要 php 7.3 或更高版本。