Few classes to make your Symfony tests more readable
composer require --dev nebkam/fluent-test
composer require --dev nebkam/fluent-test:"^2.0"
RequestBuilder
Since Symfony\Bundle\FrameworkBundle\KernelBrowser::request
has 7 optional parameters, arbitrary ordered, this class follows a builder pattern to construct the request using semantic methods.
// Before
$response = $client->request($method, $uri, $parameters, $files, $server, $content);
// After
$response = RequestBuilder::create($client)
->setMethod($method)
->setUri($uri)
->setParameters($parameters)
->setFiles($files)
->setContent($content)
->getResponse();
There's no RequestBuilder::setServer
method, since it seemed to general to be semantic.
Instead, you can use more specific methods (Thanks, @KristijanKanalas):
setHeader
setHttpHeader
setCredentials
(if you think of some other uses of server variables, feel free to write a semantic method for it in a PR)
// Before
$response = $client->request($method, $uri, $parameters, $files, [
'CONTENT_TYPE' => $value
], $content);
// After
$response = RequestBuilder::create($client)
->setHeader('CONTENT_TYPE', $value)
...
// Before
$response = $client->request($method, $uri, $parameters, $files, [
'HTTP_X-Custom-Header' => $value
], $content);
// After
$response = RequestBuilder::create($client)
->setHttpHeader('X-Custom-Header', $value)
...
// Before
$response = $client->request($method, $uri, $parameters, $files, [
'PHP_AUTH_USER' => $username,
'PHP_AUTH_PW' => $password
], $content);
//After
$response = RequestBuilder::create($client)
->setCredentials($username, $password)
...
setJsonContent
Send a JSON encoded payload with the request
// Before
$response = $client->request($method, $uri, $parameters, $files, $server, json_encode($content));
// After
$response = RequestBuilder::create($client)
->setJsonContent($content)
...
setUri
accepts either a plain string
or sprintf -compatible parameters (format and values)
// This works
$response = RequestBuilder::create($client)
->setUri('/users/'. $email .'/details')
...
// This is more readable
$response = RequestBuilder::create($client)
->setUri('/users/%s/details', $email)
...
ResponseWrapper
A decorator for Symfony\Component\HttpFoundation\Response
that wraps the response and provides few semantic issers to make asserts more readable
// Before
$client->request($method, $uri, $parameters, $files, $server, $content);
$response = $client->getResponse();
$this->assertEquals(200,$response->getStatusCode())
// After
$response = RequestBuilder::create($client)
...
->getResponse();
$this->assertTrue($response->isOk());
isBadRequest
isCreated
isEmpty
isForbidden
isNotFound
isOk
isUnauthorized
isUnprocessable
getJsonContent
Get a JSON decoded body from the response
// Before
$response = $client->request($method, $uri, $parameters, $files, $server, $content);
$data = json_decode($client->getResponse());
// After
$data = RequestBuilder::create($client)
...
->getResponse()
->getJsonContent();