Added another test and added an exception mapping

pull/1/head
Hecht 10 months ago
parent 8e2afe640d
commit e8476b6507

@ -8,6 +8,8 @@ api_platform:
vary: ['Content-Type', 'Authorization', 'Origin'] vary: ['Content-Type', 'Authorization', 'Origin']
extra_properties: extra_properties:
standard_put: true standard_put: true
exception_to_status:
Doctrine\DBAL\Exception\UniqueConstraintViolationException: 409
formats: formats:
json: ['application/json'] json: ['application/json']
html: ['text/html'] html: ['text/html']

@ -4,7 +4,7 @@ doctrine:
# IMPORTANT: You MUST configure your server version, # IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file) # 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%' profiling_collect_backtrace: '%kernel.debug%'
use_savepoints: true use_savepoints: true

@ -2,7 +2,8 @@
namespace App\Tests; namespace App\Tests;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; 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 App\Repository\UserRepository;
use Zenstruck\Foundry\Test\Factories; use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase; 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); 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 public function testCreateDojo(): void
{ {
/** $userName = "FooBarFigher";
* $dojoName = "BigFightDojo";
* @var Response $response $userRepository = $this->getContainer()->get(UserRepository::class);
*/
$response = static::createClient()->request('POST', '/api/dojos', $this->assertCount(0, $userRepository->findByAuthName($userName));
static::createClient()->request('POST', '/api/dojos',
[ [
'headers' => [ 'headers' => [
'accept' => 'application/json', 'accept' => 'application/json',
'X-AUTH-TOKEN' => $this->generateAuthToken('blablabla') 'X-AUTH-TOKEN' => $this->generateAuthToken($userName)
], ],
'json' => [ 'json' => [
'name' => 'FooBar' 'name' => $dojoName
] ]
]); ]);
$this->assertResponseStatusCodeSame(201); $this->assertResponseStatusCodeSame(201);
$userRepository = $this->getContainer()->get(UserRepository::class); $this->assertCount(1, $userRepository->findByAuthName($userName));
$this->assertCount(1, $userRepository->findAll()); }
/**
* 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
} }
} }

Loading…
Cancel
Save