The test directory is:
The fixture directory is:
To start all tests, run:
To start all tests with code coverage, run:
The code coverage output directory is:
Testing units in isolation of its dependencies.
Unit tests should test the behavior and not the implementation details of your classes. Make sure that unit tests are running in-memory only, because they have to be very fast.
When testing Slim applications, you may wish to “mock” certain aspects of your application so they are not actually executed during a test. For example, when testing a service that needs a repository, you may wish to mock the repository so that its not actually executed queries during the test.
AppTestTrait provides methods for mocking objects into the container.
For better IDE support you may better use the
$this->mockMethod([UserReaderRepository::class, 'getUserById']) ->willReturn(['example' => 'data']);
AppTestTrait provides methods for making HTTP requests to your Slim application and examining the output.
Creating a GET request:
$request = $this->createRequest('GET', '/users/1');
Creating a POST request:
$request = $this->createRequest('POST', '/users');
Creating a JSON (
application/json) request with payload:
$request = $this->createJsonRequest('POST', '/users', ['name' => 'Sally']);
Creating a form (
$request = $this->createFormRequest('POST', '/users', ['name' => 'Sally']);
Make request and get the response. This method traverses the application middleware stack and then returns the resultant Response object.
$response = $this->app->handle($request);
Asserting the HTTP status code:
Asserting a JSON response:
$this->assertJsonData($response, [ 'user_id' => 1, 'username' => 'admin', 'first_name' => 'John', 'last_name' => 'Doe', 'email' => 'firstname.lastname@example.org', ]);
Take a look at the examples in:
DatabaseTestTrait provides methods for all these stages of a database test:
- Import the database schema (table structure)
- Insert the fixtures (rows) required for the test.
- Execute the test
- Verify the state of the tables
- Cleanup the tables for each new test
Take a look at these examples:
To debug tests in PhpStorm, you have to mark the
tests/ directory as test sources root.
- Open the project in PhpStorm
- Right click the directory
Mark directory as
Test Sources Root
- Set a breakpoint within a test method
- Right click
Debug (tests) PHPUnit