From c867cd5fe3ee1239bd58e893251bb04d47d85bb9 Mon Sep 17 00:00:00 2001 From: Daniel Subiabre Date: Wed, 20 Nov 2024 12:28:12 +0100 Subject: [PATCH] Improve Project api test case coverage --- tests/Entity/ProjectApiTest.php | 51 ++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/tests/Entity/ProjectApiTest.php b/tests/Entity/ProjectApiTest.php index e5579be..e2d8055 100644 --- a/tests/Entity/ProjectApiTest.php +++ b/tests/Entity/ProjectApiTest.php @@ -3,6 +3,10 @@ namespace App\Tests\Entity; use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; +use App\Entity\Project; +use App\Entity\ProjectStatus; +use App\Entity\User; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\Response; use Zenstruck\Foundry\Test\ResetDatabase; @@ -10,25 +14,64 @@ class ProjectApiTest extends ApiTestCase { use ResetDatabase; + private EntityManagerInterface $entityManager; + + public function setUp(): void + { + self::bootKernel(); + + $this->entityManager = static::getContainer()->get(EntityManagerInterface::class); + } + public function testGetCollection(): void { static::createClient()->request('GET', '/v4/projects'); $this->assertResponseIsSuccessful(); $this->assertJsonContains(['@id' => '/v4/projects']); + $this->assertJsonContains(['@type' => 'hydra:Collection']); + $this->assertJsonContains(['hydra:totalItems' => 0]); $this->assertJsonContains(['hydra:member' => []]); + + $owner = new User(); + $owner->setUsername('test_user'); + $owner->setEmail('testuser@example.com'); + $owner->setPassword('projectapitestuserpassword'); + + $project = new Project(); + $project->setTitle('Test Project'); + $project->setOwner($owner); + $project->setStatus(ProjectStatus::Editing); + + $this->entityManager->persist($owner); + $this->entityManager->persist($project); + $this->entityManager->flush(); + + static::createClient()->request('GET', '/v4/projects'); + + $this->assertResponseIsSuccessful(); + $this->assertJsonContains(['hydra:totalItems' => 1]); + $this->assertJsonContains(['hydra:member' => [ + [ + 'title' => 'Test Project', + 'accounting' => [], + 'status' => ProjectStatus::Editing->value + ] + ]]); } - public function testPostUnauthorized(): void + public function testPostUnauthorized() { $client = static::createClient(); $client->request( 'POST', '/v4/projects', - ['json' => [ - 'title' => 'ProjectApiTest Project', - ]] + [ + 'json' => [ + 'title' => 'ProjectApiTest Project', + ] + ] ); $this->assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED);