Skip to content

Commit 62ee8d9

Browse files
authored
ApiTestCase and assertions docs (#875)
* ApiTestCase and assertions docs * Change Title
1 parent a52707f commit 62ee8d9

File tree

3 files changed

+242
-179
lines changed

3 files changed

+242
-179
lines changed

core/testing.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Testing Utilities
2+
3+
API Platform Core provides a set of useful utilities dedicated to API testing.
4+
For an overview of how to test an API Platform app, be sure to read [the testing cookbook first](../distribution/testing.md).
5+
6+
## The Test HttpClient
7+
8+
API Platform provides its own implementation of the [Symfony HttpClient](https://symfony.com/doc/current/components/http_client.html)'s interfaces, tailored to be used directly in [PHPUnit](https://phpunit.de/) test classes.
9+
10+
While all the convenient features of Symfony HttpClient are available and usable directly, under the hood the API Platform implementation manipulates [the Symfony HttpKernel](https://symfony.com/doc/current/components/http_kernel.html) directly to simulate HTTP requests and responses.
11+
This approach results in a huge performance boost compared to triggering real network requests.
12+
It also allows access to the [Symfony HttpKernel](https://symfony.com/doc/current/components/http_kernel.html and to all your services via the [Dependency Injection Container](https://symfony.com/doc/current/testing.html#accessing-the-container).
13+
Reuse them to run, for instance, SQL queries or requests to external APIs directly from your tests.
14+
15+
Install the `symfony/http-client` and `symfony/browser-kit` packages to enabled the API Platform test client:
16+
17+
$ docker-compose exec php composer require symfony/http-client symfony/browser-kit
18+
19+
To use the testing client, your test class must extend the `ApiTestCase` class:
20+
21+
```php
22+
<?php
23+
// api/tests/BooksTest.php
24+
25+
namespace App\Tests;
26+
27+
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
28+
29+
class BooksTest extends ApiTestCase
30+
{
31+
public function testGetCollection(): void
32+
{
33+
$response = static::createClient()->request('GET', '/books');
34+
// your assertions here...
35+
}
36+
}
37+
```
38+
39+
Refer to [the Symfony HttpClient documentation](https://symfony.com/doc/current/components/http_client.html) to discover all the features of the client (custom headers, JSON encoding and decoding, HTTP Basic and Bearer authentication and cookies support, among other things).
40+
41+
42+
## API Test Assertions
43+
44+
In addition to [the built-in ones](https://phpunit.readthedocs.io/fr/latest/assertions.html), API Platform provides convenient PHPUnit assertions dedicated to API testing:
45+
46+
```php
47+
<?php
48+
// api/tests/MyTest.php
49+
50+
namespace App\Tests;
51+
52+
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
53+
54+
class MyTest extends ApiTestCase
55+
{
56+
public function testSomething(): void
57+
{
58+
// static::createClient()->request(...);
59+
60+
// Asserts that the returned JSON is equal to the passed one
61+
$this->assertJsonEquals(/* a JSON document as an array or as a string */);
62+
63+
// Asserts that the returned JSON is a superset of the passed one
64+
$this->assertJsonContains(/* a JSON document as an array or as a string */);
65+
66+
// justinrainbow/json-schema must be installed to use the following assertions
67+
68+
// Asserts that the returned JSON matches the passed JSON Schema
69+
$this->assertMatchesJsonSchema(/* a JSON Schema as an array or as a string */);
70+
71+
// Asserts that the returned JSON is validated by the JSON Schema generated for this resource by API Platform
72+
73+
// For collections
74+
$this->assertMatchesResourceCollectionJsonSchema(YourApiResource::class);
75+
// And for items
76+
$this->assertMatchesResourceItemJsonSchema(YourApiResource::class);
77+
}
78+
}
79+
```
80+
81+
## HTTP Test Assertions
82+
83+
All tests assertions provided by Symfony (assertions for status codes, headers, cookies, XML documents...) can be used out of the box with the API Platform test client:
84+
85+
```php
86+
<?php
87+
// api/tests/BooksTest.php
88+
89+
namespace App\Tests;
90+
91+
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
92+
93+
class BooksTest extends ApiTestCase
94+
{
95+
public function testGetCollection(): void
96+
{
97+
static::createClient()->request('GET', '/books');
98+
99+
$this->assertResponseIsSuccessful();
100+
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
101+
}
102+
}
103+
```
104+
105+
[Check out the dedicated Symfony documentation entry](https://symfony.com/doc/current/testing/functional_tests_assertions.html).

distribution/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ API Platform uses these model classes to expose a web API having a bunch of buil
3838
* filtering
3939
* sorting
4040
* hypermedia/[HATEOAS](https://en.wikipedia.org/wiki/HATEOAS) and content negotiation support ([JSON-LD](http://json-ld.org),
41-
[HAL](http://blog.stateless.co/post/13296666138/json-linking-with-hal), [JSON API](http://jsonapi.org/))
41+
[HAL](http://blog.stateless.co/post/13296666138/json-linking-with-hal), [JSON API](http://jsonapi.org/)...)
4242
* [GraphQL support](http://graphql.org/)
43-
* Nice UI and machine-readable documentations ([Swagger/OpenAPI](https://swagger.io), [Hydra](http://hydra-cg.com))
43+
* Nice UI and machine-readable documentations ([Swagger UI/OpenAPI](https://swagger.io), [Hydra](http://hydra-cg.com))
4444
* authentication ([Basic HTTP](https://en.wikipedia.org/wiki/Basic_access_authentication), cookies as well as [JWT](https://jwt.io/)
4545
and [OAuth](https://oauth.net/) through extensions)
4646
* [CORS headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS)
@@ -49,7 +49,7 @@ API Platform uses these model classes to expose a web API having a bunch of buil
4949
* and basically everything needed to build modern APIs.
5050

5151
One more thing, before we start: as the API Platform distribution includes [the Symfony framework](https://symfony.com),
52-
it is compatible with most [Symfony bundles](https://symfony.com/blog/the-30-most-useful-symfony-bundles-and-making-them-even-better)
52+
it is compatible with most [Symfony bundles](https://flex.symfony.com)
5353
(plugins) and benefits from the numerous extensions points provided by this rock-solid foundation (events, DIC...).
5454
Adding features like custom, service-oriented, API endpoints, JWT or OAuth authentication, HTTP caching, mail sending or
5555
asynchronous jobs to your APIs is straightforward.

0 commit comments

Comments
 (0)