nojimage / cakephp-test-datasource-switcher
CakePHP 3 的 TestDatasourceSwitcher 插件
v1.0.1
2019-04-19 10:48 UTC
Requires
- php: >=5.4.16
- cakephp/cakephp: ~3.0
Requires (Dev)
- cakephp/cakephp-codesniffer: ^3.1
- phpunit/phpunit: ^5.7|^6.5
This package is auto-updated.
Last update: 2024-09-09 19:13:40 UTC
README
当请求具有特定 Cookie 时,切换数据源连接到 test
。
安装
您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。
安装 composer 包的推荐方法是
composer require --dev nojimage/cakephp-test-datasource-switcher
CakePHP >= 3.3 在 src/Application.php
public function middleware($middlewareQueue)
{
$middlewareQueue
// Catch any exceptions in the lower layers,
// and make an error page/response
->add(ErrorHandlerMiddleware::class)
// vvv ADD THIS LINE vvv
->add(new \TestDatasourceSwitcher\Middleware\DatasourceSwitchMiddleware())
// Handle plugin/theme assets like CakePHP normally does.
->add(AssetMiddleware::class)
// Add routing middleware.
->add(new RoutingMiddleware($this));
return $middlewareQueue;
}
CakePHP <= 3.2 在 config/bootstrap.php
if (Configure::read('debug')) {
\Cake\Routing\DispatcherFactory::add(new \TestDatasourceSwitcher\Routing\Filter\Switcher(['priority' => 1]));
}
用法
例如,对于 Codeception 功能测试用例。
(此示例始终连接到 'test' 数据源。
class FunctionalHelper extends \Codeception\Module
{
public function _before(TestCase $test)
{
$driver = $this->getModule('WebDriver');
/* @var $dirver WebDriver */
$driver->amOnPage('/');
$driver->setCookie('__cakephp_test_connection', '1');
}
(...snip...)
许可协议
此软件根据 MIT 许可协议发布。
版权所有 (c) 2015 ELASTIC Consultants Inc. https://elasticconsultants.com/