Skip to content

Commit 55c4aa1

Browse files
authored
docs: modernize the testing tutorial (#1693)
1 parent 70d8133 commit 55c4aa1

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

distribution/testing.md

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ Let's learn how to use them!
1111

1212
In this article you'll learn how to use:
1313

14-
* [PHPUnit](https://phpunit.de/index.html), a testing framework to cover your classes with unit tests and to write
14+
* [PHPUnit](https://phpunit.de), a testing framework to cover your classes with unit tests and to write
1515
API-oriented functional tests thanks to its API Platform and [Symfony](https://symfony.com/doc/current/testing.html) integrations.
1616
* [Alice](https://github.com/nelmio/alice) and [its Symfony
1717
integration](https://github.com/theofidry/AliceBundle#database-testing), an expressive fixtures generator to write data fixtures.
1818

19-
Official [Symfony recipes](https://flex.symfony.com/) are provided for both tools.
20-
2119
## Creating Data Fixtures
2220

2321
Before creating your functional tests, you will need a dataset to pre-populate your API and be able to test it.
@@ -69,17 +67,20 @@ The list of available generators as well as a cookbook explaining how to create
6967

7068
## Writing Functional Tests
7169

72-
Now that you have some data fixtures for your API, you are ready to write functional tests with [PHPUnit](https://phpunit.de/index.html).
70+
Now that you have some data fixtures for your API, you are ready to write functional tests with [PHPUnit](https://phpunit.de).
71+
72+
The API Platform test client implements the interfaces of the [Symfony HttpClient](https://symfony.com/doc/current/components/http_client.html). HttpClient is shipped with the API Platform distribution. The [Symfony test pack](https://github.com/symfony/test-pack/blob/main/composer.json), which includes PHPUnit as well as Symfony components useful for testing, is also included.
7373

74-
Install the Symfony test pack (which includes PHPUnit and [PHPUnit Bridge](https://symfony.com/doc/current/components/phpunit_bridge.html)), [Symfony HttpClient](https://symfony.com/doc/current/components/http_client.html)
75-
(the API Platform test client is built on top of Symfony HttpClient, and allows to leverage all its features) and [JSON Schema for PHP](https://github.com/justinrainbow/json-schema) (used by API Platform to provide [JSON Schema](https://json-schema.org/) test assertions):
74+
If you don't use the distribution, run `composer require --dev symfony/test-pack symfony/http-client` to install them.
75+
76+
Optionally, you can install [JSON Schema for PHP](https://github.com/justinrainbow/json-schema) if you want to use the [JSON Schema](https://json-schema.org) test assertions provided by API Platform:
7677

7778
```console
7879
docker compose exec php \
79-
composer require --dev symfony/test-pack symfony/http-client justinrainbow/json-schema
80+
composer require --dev justinrainbow/json-schema
8081
```
8182

82-
Your API is ready to be functionally tested. Create your test classes under the `tests/` directory.
83+
Your API is now ready to be functionally tested. Create your test classes under the `tests/` directory.
8384

8485
Here is an example of functional tests specifying the behavior of [the bookstore API you created in the tutorial](index.md):
8586

@@ -245,31 +246,35 @@ Check out the [testing documentation](../core/testing.md) to discover the full r
245246
## Writing Unit Tests
246247

247248
In addition to integration tests written using the helpers provided by `ApiTestCase`, all the classes of your project should be covered by [unit tests](https://en.wikipedia.org/wiki/Unit_testing).
248-
To do so, learn how to write unit tests with [PHPUnit](https://phpunit.de/index.html) and [its Symfony/API Platform integration](https://symfony.com/doc/current/testing.html).
249+
To do so, learn how to write unit tests with [PHPUnit](https://phpunit.de/) and [its Symfony/API Platform integration](https://symfony.com/doc/current/testing.html).
250+
251+
## Continuous Integration, Continuous Delivery and Continuous Deployment
252+
253+
Running your test suite in your [CI/CD pipeline](https://en.wikipedia.org/wiki/Continuous_integration) is important to ensure good quality and delivery time.
254+
255+
The API Platform distribution is [shipped with a GitHub Actions workflow](https://github.com/api-platform/api-platform/blob/main/.github/workflows/ci.yml) that builds the Docker images, does a [smoke test](https://en.wikipedia.org/wiki/Smoke_testing_(software)) to check that the application's entrypoint is accessible, and runs PHPUnit.
256+
257+
The API Platform Demo [contains a CD worklow](https://github.com/api-platform/demo/tree/main/.github/workflows) that uses [the Helm chart provided with the distribution](../deployment/kubernetes.md) to deploy the app on a Kubernetes cluster.
249258

250259
## Additional and Alternative Testing Tools
251260

252261
You may also be interested in these alternative testing tools (not included in the API Platform distribution):
253262

254-
* [Behat](http://behat.org/en/latest/) and its [Behatch extension](https://github.com/Behatch/contexts), a
263+
* [Foundry](https://github.com/zenstruck/foundry), a modern fixtures library that will replace Alice as the recommended fixtures library soon;
264+
* [Hoppscotch](https://docs.hoppscotch.io/features/tests), create functional test for your API
265+
Platform project using a nice UI, benefit from its Swagger integration and run tests in the CI using [the CLI tool](https://docs.hoppscotch.io/cli);
266+
* [Behat](http://behat.org), a
255267
[behavior-driven development (BDD)](https://en.wikipedia.org/wiki/Behavior-driven_development) framework to write the API
256268
specification as user stories and in natural language then execute these scenarios against the application to validate
257269
its behavior;
258270
* [Blackfire Player](https://blackfire.io/player), a nice DSL to crawl HTTP services, assert responses, and extract data
259-
from HTML/XML/JSON responses ([see example in API Platform Demo](https://github.com/api-platform/demo/blob/master/test-api.bkf));
260-
* [Postman tests](https://www.getpostman.com/docs/writing_tests) (proprietary), create functional test for your API
261-
Platform project using a nice UI, benefit from [the Swagger integration](https://www.getpostman.com/docs/importing_swagger)
262-
and run tests in the CI using [newman](https://github.com/postmanlabs/newman);
271+
from HTML/XML/JSON responses;
263272
* [PHP Matcher](https://github.com/coduo/php-matcher), the Swiss Army knife of JSON document testing.
264273

265-
## Using the API Platform Distribution for End-to-end Testing
274+
## Using the API Platform Distribution for End-to-End Testing
266275

267276
If you would like to verify that your stack (including services such as the DBMS, web server, [Varnish](https://varnish-cache.org/))
268-
works, you need [end-to-end (E2E) testing](https://wiki.c2.com/?EndToEndPrinciple).
269-
270-
It is also useful to do a [smoke test](https://en.wikipedia.org/wiki/Smoke_testing_(software)) to check that your application
271-
is working; for example, that the application's entrypoint is accessible. This could be used as a quick test after each
272-
Docker build to ensure that the Docker images are not broken.
277+
works, you need [end-to-end (E2E) testing](https://wiki.c2.com/?EndToEndPrinciple). To do so, we recommend using [Playwright](https://playwright.dev) if you use have PWA/JavaScript-heavy app, or [Symfony Panther](https://github.com/symfony/panther) if you mostly use Twig.
273278

274-
Usually, this should be done with a production-like setup. For your convenience, you may [run our Docker Compose setup
279+
Usually, E2E testing should be done with a production-like setup. For your convenience, you may [run our Docker Compose setup
275280
for production locally](../deployment/docker-compose.md#running-the-docker-compose-setup-for-production-locally).

0 commit comments

Comments
 (0)