Added another test and added an exception mapping

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

@ -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']

@ -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

@ -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
}
}

Loading…
Cancel
Save