nojimage / cakephp-test-datasource-switcher
TestDatasourceSwitcher plugin for CakePHP 3
Installs: 4 989
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Type:cakephp-plugin
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-10-09 19:27:25 UTC
README
Switching datasouce connections to test
, when the request have a specific Cookie.
Installation
You can install this plugin into your CakePHP application using composer.
The recommended way to install composer packages is:
composer require --dev nojimage/cakephp-test-datasource-switcher
CakePHP >= 3.3 in 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 in config/bootstrap.php
if (Configure::read('debug')) {
\Cake\Routing\DispatcherFactory::add(new \TestDatasourceSwitcher\Routing\Filter\Switcher(['priority' => 1]));
}
Usage
eg. For the Codeception functional testcases.
(This sample is always connect to 'test' datasouces.
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...)
License
This software is released under the MIT License.
Copyright (c) 2015 ELASTIC Consultants Inc. https://elasticconsultants.com/