Integration test means: test that multiple parts work together. Here the route, the JSON response, and the service all work together.
Summary: Test a real API endpoint.
Here is the PHP API code, which we want to test:
if ($method === 'GET' && $path === '/api/lessons') {
$dashboard = $this->service->dashboard();
return $this->jsonResponse([
'lessons' => $dashboard['lessons'],
]);
}Now we can create a test like this:
$lessonsResponse = $client->request('GET', $server['url'] . '/api/lessons');
$lessons = json_decode($lessonsResponse['body'], true);
$this->assertSame(200, $lessonsResponse['status']);
$this->assertTrue(count($lessons['lessons']) > 0);Why this test works
- The main class behind this is App in src/App.php.
- At the top of App.php you will also see namespace SmallLearning;.
- App uses LearningService, and LearningService uses LessonRepository.
- In an integration test you often do not create these classes by hand. You start the app and test it from the outside.
- Put this file in tests/http/ApiIntegrationTest.php because it uses real HTTP.
- This test knows what to hit because it starts the local PHP server and then requests /api/lessons.
- We do not test one class directly here. We test the whole connection between route and response.
- Simple rule: send request, read JSON, assert status and data.
Real-life examples
- Example: on a shop website, call GET /api/products and check that status 200 is returned and the JSON contains products.
- Example: on a travel website, call GET /api/search?city=Rome and check that the JSON contains hotel results.
Remember this
- Open src/App.php first
- Think in endpoints, not classes
- Put file in tests/http
- Start local server
- Run with php tests/run.php integration