Sitemap
Rodrigo Silveira

Rodrigo Silveira

Software engineer fascinated with learning, practicing, and teaching.

Integration Tests with PHPUnit Testing Framework

3 min readOct 2, 2017

My goal for this week was to do more test driven development. Given that our team implements our applications using an n-tiered architecture, most components are pretty straight forward to unit test.

Press enter or click to view image in full size

Our convention is to have the “controller” (the method that processes the request) do as little work as possible. This can be accomplished by injecting Service objects into the controller, then letting the controller delegate the work of generating the necessary data to the service objects. With the data from the service object(s), the controller returns a view object, which knows how to render itself given whatever data the controller passes it from the service layer.

The benefit of doing this is that the route handler layer now has very little logic to test. The service layer is injected with one or more provider objects, which can be easily mocked in a unit test.

<?phpuse Silex\Application;
use Silex\Api\ControllerProviderInterface;
class MovieListController implements ControllerProviderInterface
{
public function connect(Application $app)
{
$controller = $app['controllers_factory'];
$controller->get('/latest/{max}', function ($max) use ($app) {
$service = $app['MovieService'];
$data = $service->getLatest($max);
return $app['templating']->render(
self::MAIN_TEMPLATE,
$response);
});
return $controller;
}
}

At this point, although we have great isolated test coverage on the MovieService class, we have something like the following image deployed to production:

The service class will receive its data from the provider, which will in turn either call a database or some API. What our unit tests don’t cover is the case where 1/5 of these requests fails as the controller is gathering all the data it needs. One solution would be to mock out a lot of this scenario, but that seems like a lot of detailed knowledge about the implementation of this scenario would be needed. This might make it for a brittle unit test.

Get Rodrigo Silveira’s stories in your inbox

Join Medium for free to get updates from this writer.

What we’ve opted for instead is to have a longer test, which will actually instantiate the entire Silex $app and exercise that controller. We’ll be making real network requests, and asserting that the output contains the expected data we want. With this approach, we could also easily extend our providers to force a failure at specific points.

To simplify testing even further, we decided to test both the rendered view, as well as just the data it receives from the controller. To separate those two, we’ve modified the of the controller slightly to allow for data to be returned within a controlled environment:

$controller->get('/latest/{max}', function ($max) use ($app) {
$service = $app['MovieService'];
$data = $service->getLatest($max);
if (getenv('CTRL_AS_DATA')) {
return $app->json($data);
}
return $app['templating']->render(
self::MAIN_TEMPLATE,
$response);
});

Now the integration test can set that environment variable, and get a hold of the data passed to our view. We can test it using PHPUnit as follows:

<?phpuse Silex\WebTestCase;class ArticleControllerTest extends WebTestCase
{
/**
* Creates the application.
*
* @return \Symfony\Component\HttpKernel\HttpKernelInterface
*/
public function createApplication()
{
$app = getAppInstance();
$app['session.test'] = true;
return $app;
}
/**
* @test
* @large
*/
public function getLatestMovies_DataIsParsedProperly()
{
$client = $this->createClient();
putenv('CTRL_AS_DATA=1');
$client->request('GET', '/latest/10');
$json = $client->getResponse()->getContent();
$data = json_decode($json, true);
self::assertEquals($data['title'], 'Latest Movies', 'Title mismatch');
self::assertNotEmpty($data['categories'], 'Missing categories list');
self::assertEquals(count($data['categories']), 5, 'Not enough categories');
}
}

--

--

Rodrigo Silveira
Rodrigo Silveira

Written by Rodrigo Silveira

Software engineer fascinated with learning, practicing, and teaching.