For non-CakePHP applications, you may use the method proposed by your framework to manage the test database, or opt for the universal test database cleaner.
You should define your DB connections in your test bootstrap.php
file as described
in the cookbook.
To be able to bake your factories,
load the CakephpFixtureFactories plugin in your src/Application.php
file:
protected function bootstrapCli(): void
{
// Load more plugins here
if (Configure::read('debug')) {
$this->addPlugin('CakephpFixtureFactories');
}
}
We recommend using migrations for managing the schema of your test DB with the CakePHP Migrator tool.
For CakePHP anterior to 4.3 applications, you will need to use the CakePHP test suite light plugin to clean-up the test database prior to each test.
Make sure you replace the native CakePHP listener by the following one inside your phpunit.xml
(or phpunit.xml.dist
) config file,
per default located in the root folder of your application:
<!-- Setup a listener for fixtures -->
<listeners>
<listener class="CakephpTestSuiteLight\FixtureInjector">
<arguments>
<object class="CakephpTestSuiteLight\FixtureManager" />
</arguments>
</listener>
</listeners>
The following command will do that for you.
bin/cake fixture_factories_setup
You can specify a plugin (-p
) and a specific file (-f
), if different from phpunit.xml.dist
.
Between each test, the package will truncate all the test tables that have been used during the previous test.
We recommend using migrations for maintaining your test DB with the Migrator tool.