2022-01-05 17:44:02 +01:00
# Testing
2022-01-17 16:02:15 +01:00
1. Add a `cerebrate_test` database to the database:
2022-01-07 13:45:52 +01:00
```mysql
CREATE DATABASE cerebrate_test;
GRANT ALL PRIVILEGES ON cerebrate_test.* to cerebrate@localhost;
FLUSH PRIVILEGES;
QUIT;
```
2. Add a the test database to your `config/app_local.php` config file and set `debug` mode to `true` .
2022-01-05 17:44:02 +01:00
```php
'debug' => true,
'Datasources' => [
'default' => [
...
],
/*
* The test connection is used during the test suite.
*/
'test' => [
'host' => 'localhost',
'username' => 'cerebrate',
'password' => 'cerebrate',
'database' => 'cerebrate_test',
],
],
```
## Runing the tests
```
$ composer install
2022-01-17 16:02:15 +01:00
$ composer test
> sh ./tests/Helper/wiremock/start.sh
WireMock 1 started on port 8080
> phpunit
[ * ] Running DB migrations, it may take some time ...
The WireMock server is started .....
port: 8080
enable-browser-proxying: false
disable-banner: true
no-request-journal: false
verbose: false
2022-01-05 17:44:02 +01:00
PHPUnit 8.5.22 by Sebastian Bergmann and contributors.
2022-01-17 16:02:15 +01:00
2022-01-05 17:44:02 +01:00
..... 5 / 5 (100%)
Time: 11.61 seconds, Memory: 26.00 MB
OK (5 tests, 15 assertions)
```
Running a specific suite:
```
2022-01-10 16:19:58 +01:00
$ vendor/bin/phpunit --testsuite=api --testdox
2022-01-05 17:44:02 +01:00
```
Available suites:
* `app` : runs all test suites
* `api` : runs only api tests
* `controller` : runs only controller tests
* _to be continued ..._
2022-01-17 16:02:15 +01:00
By default the database is re-generated before running the test suite, to skip this step and speed up the test run set the following env variable in `phpunit.xml` :
```xml
< php >
...
< env name = "SKIP_DB_MIGRATIONS" value = "1" / >
< / php >
2022-01-05 17:44:02 +01:00
```
2022-01-17 16:02:15 +01:00
## Extras
### WireMock
Some integration tests perform calls to external APIs, we use WireMock to mock the response of these API calls.
To download and run WireMock run the following script in a separate terminal:
```
sh ./tests/Helper/wiremock/start.sh
```
You can also run WireMock with docker, check the official docs: http://wiremock.org/docs/docker/
2022-01-07 17:08:00 +01:00
2022-01-17 16:02:15 +01:00
> NOTE: When running the tests with `composer test` WireMock is automatically started and stoped after the tests finish.
The default `hostname` and `port` for WireMock are set in `phpunit.xml` as environment variables:
```xml
< php >
...
< env name = "WIREMOCK_HOST" value = "localhost" / >
< env name = "WIREMOCK_PORT" value = "8080" / >
< / php >
```
### Coverage
2022-01-07 17:08:00 +01:00
HTML:
```
$ vendor/bin/phpunit --coverage-html tmp/coverage
```
XML:
```
$ vendor/bin/phpunit --verbose --coverage-clover=coverage.xml
```
2022-01-17 16:02:15 +01:00
### OpenAPI validation
API tests can assert the API response matches the OpenAPI specification, after the request add this line:
```php
$this->assertResponseMatchesOpenApiSpec(self::ENDPOINT);
```
The default OpenAPI spec path is set in `phpunit.xml` as a environment variablea:
```xml
< php >
...
< env name = "OPENAPI_SPEC" value = "webroot/docs/openapi.yaml" / >
< / php >
```
2022-01-19 15:15:49 +01:00
### Debugging tests
```
$ export XDEBUG_CONFIG="idekey=IDEKEY"
$ phpunit
```