Files
crowd-fund-api/tests/Entity/ProjectApi/CreateTest.php
T

134 lines
3.8 KiB
PHP

<?php
namespace App\Tests\Entity\ProjectApi;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use App\Entity\User\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Response;
use Zenstruck\Foundry\Test\ResetDatabase;
class CreateTest extends ApiTestCase
{
use ResetDatabase;
private EntityManagerInterface $entityManager;
private const USER_EMAIL = 'testuser@example.com';
private const USER_PASSWORD = 'projectapitestuserpassword';
public function setUp(): void
{
self::bootKernel();
$this->entityManager = static::getContainer()->get(EntityManagerInterface::class);
}
private function createTestUser(): User
{
$user = new User();
$user->setHandle('test_user');
$user->setEmail(self::USER_EMAIL);
$passwordHasher = static::getContainer()->get('security.user_password_hasher');
$user->setPassword($passwordHasher->hashPassword($user, self::USER_PASSWORD));
return $user;
}
private function prepareTestUser(): void
{
$this->entityManager->persist($this->createTestUser());
$this->entityManager->flush();
}
private function getValidToken()
{
$client = static::createClient();
$client->request(
'POST',
'/v4/user_tokens',
[
'json' => [
'identifier' => self::USER_EMAIL,
'password' => self::USER_PASSWORD,
],
]
);
return json_decode($client->getResponse()->getContent(), true)['token'];
}
// TESTS
public function testPostWithValidToken(): void
{
$this->prepareTestUser();
$expectedData = [
'title' => 'New Education Project',
'subtitle' => 'Education for the Future',
'category' => 'education',
'territory' => ['country' => 'ES'],
'description' => 'Detailed project description',
'deadline' => 'minimum',
'video' => 'https://www.youtube.com/watch?v=bnrVQHEXmOk',
];
$client = static::createClient();
$client->request('POST', '/v4/projects', [
'headers' => [
'Authorization' => "Bearer " . $this->getValidToken(),
'Content-Type' => 'application/json'
],
'json' => $expectedData,
]);
$this->assertResponseStatusCodeSame(Response::HTTP_CREATED);
$responseData = json_decode($client->getResponse()->getContent(), true);
$this->assertArrayHasKey('id', $responseData);
$this->assertEquals($expectedData['title'], $responseData['title']);
$this->assertEquals($expectedData['subtitle'], $responseData['subtitle']);
$this->assertEquals($expectedData['category'], $responseData['category']);
}
public function testPostUnauthorized()
{
$client = static::createClient();
$client->request(
'POST',
'/v4/projects',
[
'json' => [
'title' => 'ProjectApiTest Project',
],
]
);
$this->assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED);
}
public function testPostWithInvalidToken()
{
$client = static::createClient();
$client->request(
'POST',
'/v4/projects',
[
'headers' => [
'Authorization' => "Bearer invalid_token",
'Content-Type' => 'application/json',
],
'json' => [
'title' => 'ProjectApiTest Project',
],
]
);
$this->assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED);
}
}