From 24f50f6c15fffe94cd8fd189eb6a184e59db019e Mon Sep 17 00:00:00 2001 From: Hecht Date: Mon, 5 Feb 2024 22:30:58 +0100 Subject: [PATCH] Added another test and added an exception mapping --- config/packages/api_platform.yaml | 2 ++ config/packages/doctrine.yaml | 2 +- tests/DojoTest.php | 53 +++++++++++++++++++++++++------ 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/config/packages/api_platform.yaml b/config/packages/api_platform.yaml index 3d8f218..b6d52ff 100644 --- a/config/packages/api_platform.yaml +++ b/config/packages/api_platform.yaml @@ -8,6 +8,8 @@ api_platform: vary: ['Content-Type', 'Authorization', 'Origin'] extra_properties: standard_put: true + exception_to_status: + Doctrine\DBAL\Exception\UniqueConstraintViolationException: 409 formats: json: ['application/json'] html: ['text/html'] diff --git a/config/packages/doctrine.yaml b/config/packages/doctrine.yaml index 8610fd8..63d32af 100644 --- a/config/packages/doctrine.yaml +++ b/config/packages/doctrine.yaml @@ -4,7 +4,7 @@ doctrine: # IMPORTANT: You MUST configure your server version, # either here or in the DATABASE_URL env var (see .env file) - server_version: '%env(resolve:DATABASE_VERSION)%' + # server_version: '%env(resolve:DATABASE_VERSION)%' profiling_collect_backtrace: '%kernel.debug%' use_savepoints: true diff --git a/tests/DojoTest.php b/tests/DojoTest.php index a33ba5d..da20010 100644 --- a/tests/DojoTest.php +++ b/tests/DojoTest.php @@ -2,7 +2,8 @@ namespace App\Tests; use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; -use ApiPlatform\Symfony\Bundle\Test\Response; +use App\Factory\DojoFactory; +use App\Factory\UserFactory; use App\Repository\UserRepository; use Zenstruck\Foundry\Test\Factories; use Zenstruck\Foundry\Test\ResetDatabase; @@ -25,27 +26,59 @@ class DojoTest extends ApiTestCase return sodium_bin2base64(sodium_crypto_sign($message, $sign_secret), SODIUM_BASE64_VARIANT_URLSAFE); } + /** + * Requirement: A user should be able to create a dojo! + */ public function testCreateDojo(): void { - /** - * - * @var Response $response - */ - $response = static::createClient()->request('POST', '/api/dojos', + $userName = "FooBarFigher"; + $dojoName = "BigFightDojo"; + $userRepository = $this->getContainer()->get(UserRepository::class); + + $this->assertCount(0, $userRepository->findByAuthName($userName)); + + static::createClient()->request('POST', '/api/dojos', [ 'headers' => [ 'accept' => 'application/json', - 'X-AUTH-TOKEN' => $this->generateAuthToken('blablabla') + 'X-AUTH-TOKEN' => $this->generateAuthToken($userName) ], 'json' => [ - 'name' => 'FooBar' + 'name' => $dojoName ] ]); $this->assertResponseStatusCodeSame(201); - $userRepository = $this->getContainer()->get(UserRepository::class); - $this->assertCount(1, $userRepository->findAll()); + $this->assertCount(1, $userRepository->findByAuthName($userName)); + } + + /** + * Requirement: A user should NOT be able to create more than one dojos! + */ + public function testUserCannotCreateMultipleDojos(): void + { + $userName = "FooBarFigher"; + $dojoName = "BigFightDojo"; + DojoFactory::createOne([ + 'name' => $dojoName, + 'owner' => UserFactory::createOne([ + 'authName' => $userName + ]) + ]); + + static::createClient()->request('POST', '/api/dojos', + [ + 'headers' => [ + 'accept' => 'application/json', + 'X-AUTH-TOKEN' => $this->generateAuthToken($userName) + ], + 'json' => [ + 'name' => $dojoName + ] + ]); + + $this->assertResponseStatusCodeSame(409); // 409 Conflict } }