nebkam / fluent-test
A few helpers to ease functional API testing in Symfony
Installs: 22 988
Dependents: 0
Suggesters: 0
Security: 0
Stars: 10
Watchers: 3
Forks: 2
Open Issues: 1
Requires
- php: ^7.2 || ^8.0
- ext-json: *
- symfony/browser-kit: ^4.4.7 || ^5.0.7 || 6.* || 7.*
- symfony/framework-bundle: ^4.4.7 || ^5.0.7 || 6.* || 7.*
- symfony/http-foundation: ^4.4.7 || ^5.0.7 || 6.* || 7.*
Requires (Dev)
- symfony/phpunit-bridge: ^5.0 || 6.* || 7.*
README
Fluent Test Helper
Few classes to make your Symfony tests more readable
Symfony 5 & 6
composer require --dev nebkam/fluent-test
Symfony 3 & 4
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.
Usage
// 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();
What about $server parameter?
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)
setHeader
// Before $response = $client->request($method, $uri, $parameters, $files, [ 'CONTENT_TYPE' => $value ], $content); // After $response = RequestBuilder::create($client) ->setHeader('CONTENT_TYPE', $value) ...
setHttpHeader
// Before $response = $client->request($method, $uri, $parameters, $files, [ 'HTTP_X-Custom-Header' => $value ], $content); // After $response = RequestBuilder::create($client) ->setHttpHeader('X-Custom-Header', $value) ...
setCredentials
// 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) ...
Dynamic URIs
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
Usage
// 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());
List of issers
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();