Download the PHP package brunty/api-testcase without Composer
On this page you can find all versions of the php package brunty/api-testcase. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package api-testcase
Brunty\ApiTestCase
Just some basic helper stuff to help test API endpoints.
Compatibility
- PHP 7.0 and above
- PHPUnit 6.0 and above
- Guzzlehttp 6.2 and above
Installation
composer require brunty/api-testcase --dev
Usage
Add an environment variable to your PHPUnit Configuration that's your API's base URL:
Extend the \Brunty\ApiTestCase
class. If you need to configure the client, call $this->configureClientOptions($options);
before calling parent::setUp()
:
Methods and requests available
The test case uses Guzzle (\GuzzleHttp\Client
) under the surface, so requests are effectively just made through that. If you need to access the client, you can do so with $this->client();
within your test class.
GET
get(string $path [, array $options])
POST
post(string $path [, array $options])
PATCH
patch(string $path [, array $options])
PUT
put(string $path [, array $options])
DELETE
delete(string $path [, array $options])
Headers & Responses
getHeader(string $name)
Returns a response header matching the name.
response()
Returns the response object.
statusCode()
Returns the status code from the response.
rawResponseBody()
Returns the contents of the body of the response.
responseBody($asArray)
Returns the response body, parsed into either an array (if $asArray
is true) or: \stdClass
if the response was JSON, \SimpleXmlElement
if the response was XML.
If the content type of the response cannot be determined to be either XML or JSON, a \Brunty\ContentTypeNotFound
exception will be thrown.
getContentType()
Returns the value of the first Content-Type
header element.
contentTypeIsXml()
Returns true
if the content type is XML, false
otherwise.
contentTypeIsJson()
Returns true
if the content type is JSON, false
otherwise.
The \Brunty\Response
class contains a list of constants for all HTTP status codes - these can help make status code assertions more readable - for example:
$this->assertResponseStatus(\Brunty\Response::HTTP_NO_CONTENT);
as opposed to $this->assertResponseStatus(204);
Assertions
Assertion | Notes | |
---|---|---|
assertResponseStatus($status) |
||
assertResponseOk() |
(Response code 200) | |
assertResponseWasSuccess() |
(200 <= Response Code < 300) | |
assertResponseWasRedirect() |
(300 <= Response Code < 400) _Note that you may need to set the allow_redirects option to false otherwise status codes of the page after the redirect can be used._ |
|
assertResponseWasClientError() |
(400 <= Response Code < 500) | |
assertResponseWasServerError() |
(500 <= Response Code) | |
assertResponseWasJson() |
||
assertResponseWasXml() |
||
assertResponseHasKey($key) |
||
assertNodeIsValue($xPathQuery, $value) |
Runs the xpath query against the result (yes, even for JSON - though | that's a bit experimental) and asserts that the value is correct - currently only works with strings. |
assertRedirectedTo($path) |
Path can be absolute, or relative to the root api_base_url |
Contributing
This started as a project of boredom one Friday evening, if you find yourself using this, and want more features, please feel free to suggest them, or submit a PR!
Although this project is small, openness and inclusivity are taken seriously. To that end the following code of conduct has been adopted.
Contributor Code of Conduct
All versions of api-testcase with dependencies
guzzlehttp/guzzle Version ^6.2
spatie/array-to-xml Version ^2.2
phpunit/phpunit Version ^6.0