Testing the API

Exercise 5 of 6 in chapter 6 · 240 XP · 5 objectives checked by tests

Topics covered: jsonRequest()API testsJSONAccess token

The scrolls' API has become the tavern's main reservation channel. A regression there would be costly. And a leak of a guild's seal would be even costlier.

Your mission

In tests/Taverne/ApiTest.php, prepare a guild (with the seal self::SCEAU, sceau means seal), a dish and a review, then test:

  1. GET /api/dishes: JSON, and only the menu fields (no reviews, no rating);
  2. POST /api/reservations with the seal: 201, and the response names the guild;
  3. that response never contains the seal;
  4. without a seal: 401;
  5. 13 adventurers: 422, with a violation on personnes.

Testing an API

$this->client->jsonRequest('GET', '/api/dishes');
$this->client->jsonRequest('POST', '/api/reservations', ['nom' => 'Gimli', …], ['HTTP_AUTHORIZATION' => 'Bearer …']);

$this->assertResponseStatusCodeSame(201);
$json = json_decode($this->client->getResponse()->getContent(), true);

Be precise

For an API, “it responds” is not enough: the details make up the contract with the scrolls. The exact code (201 is not 200), the exact list of fields, and what must never show up.

Four mutants 🐛, one per detail.