Skip to content

Commit

Permalink
api: add snapshot tests of the responses
Browse files Browse the repository at this point in the history
That we are sure that we don't accidentally change the interface
for the frontend which may lead to n+1 requests.
  • Loading branch information
BacLuc committed Jul 15, 2023
1 parent 18e23f3 commit 1de2171
Show file tree
Hide file tree
Showing 15 changed files with 2,630 additions and 1 deletion.
5 changes: 5 additions & 0 deletions api/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"phpstan/phpstan": "1.10.25",
"phpunit/phpunit": "9.6.10",
"rector/rector": "0.17.2",
"spatie/phpunit-snapshot-assertions": "4.2.16",
"symfony/browser-kit": "6.3.0",
"symfony/css-selector": "6.3.0",
"symfony/debug-bundle": "6.3.0",
Expand Down Expand Up @@ -112,6 +113,10 @@
"Composer\\Config::disableProcessTimeout",
"bin/phpunit -d memory_limit=-1"
],
"update-snapshots": [
"Composer\\Config::disableProcessTimeout",
"bin/phpunit -d memory_limit=-1 --update-snapshots"
],
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
Expand Down
70 changes: 69 additions & 1 deletion api/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions api/tests/Api/Activities/CreateActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
use App\Entity\Activity;
use App\Entity\User;
use App\Tests\Api\ECampApiTestCase;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;

/**
* @internal
Expand Down Expand Up @@ -76,6 +81,19 @@ public function testCreateActivityIsAllowedForManager() {
$this->assertJsonContains($this->getExampleReadPayload());
}

/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testCreateActivityResponseMatchesSnapshot() {
$response = static::createClientWithCredentials()->request('POST', '/activities', ['json' => $this->getExampleWritePayload()]);

$this->assertMatchesResponseSnapshot($response);
}

public function testCreateActivityInCampPrototypeIsDeniedForUnrelatedUser() {
static::createClientWithCredentials()->request('POST', '/activities', ['json' => $this->getExampleWritePayload([
'category' => $this->getIriFor('category1campPrototype'),
Expand Down
18 changes: 18 additions & 0 deletions api/tests/Api/Activities/ListActivitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
namespace App\Tests\Api\Activities;

use App\Tests\Api\ECampApiTestCase;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;

/**
* @internal
Expand Down Expand Up @@ -40,6 +45,19 @@ public function testListActivitiesIsAllowedForLoggedInUserButFiltered() {
], $response->toArray()['_links']['items']);
}

/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testListActivitiesMatchesSnapshot() {
$response = static::createClientWithCredentials()->request('GET', '/activities');

$this->assertMatchesResponseSnapshot($response);
}

public function testListActivitiesFilteredByCampIsAllowedForCollaborator() {
$camp = static::$fixtures['camp1'];
$response = static::createClientWithCredentials()->request('GET', '/activities?camp=%2Fcamps%2F'.$camp->getId());
Expand Down
20 changes: 20 additions & 0 deletions api/tests/Api/Activities/ReadActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

use App\Entity\Activity;
use App\Tests\Api\ECampApiTestCase;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;

/**
* @internal
Expand Down Expand Up @@ -116,6 +121,21 @@ public function testGetSingleActivityIsAllowedForManager() {
]);
}

/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testGetSingleMatchesSnapshot() {
/** @var Activity $activity */
$activity = static::$fixtures['activity1'];
$response = static::createClientWithCredentials()->request('GET', '/activities/'.$activity->getId());

$this->assertMatchesResponseSnapshot($response);
}

public function testGetSingleActivityFromCampPrototypeIsAllowedForUnrelatedUser() {
/** @var Activity $activity */
$activity = static::$fixtures['activity1campPrototype'];
Expand Down
23 changes: 23 additions & 0 deletions api/tests/Api/Activities/UpdateActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
namespace App\Tests\Api\Activities;

use App\Tests\Api\ECampApiTestCase;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;

/**
* @internal
Expand Down Expand Up @@ -106,6 +111,24 @@ public function testPatchActivityIsAllowedForManager() {
]);
}

/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testPatchActivityMatchesSnapshot() {
$activity = static::$fixtures['activity1'];
$response = static::createClientWithCredentials()->request('PATCH', '/activities/'.$activity->getId(), ['json' => [
'title' => 'Hello World',
'location' => 'Stoos',
'category' => $this->getIriFor('category2'),
], 'headers' => ['Content-Type' => 'application/merge-patch+json']]);

$this->assertMatchesResponseSnapshot($response);
}

public function testPatchActivityFromCampPrototypeIsDeniedForUnrelatedUser() {
$activity = static::$fixtures['activity1campPrototype'];
static::createClientWithCredentials()->request('PATCH', '/activities/'.$activity->getId(), ['json' => [
Expand Down
Loading

0 comments on commit 1de2171

Please sign in to comment.