|
| 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). |
0 commit comments