diff --git a/src/Entity/Post.php b/src/Entity/Post.php index d2f968b..0b54550 100644 --- a/src/Entity/Post.php +++ b/src/Entity/Post.php @@ -2,6 +2,7 @@ namespace App\Entity; +use ApiPlatform\Doctrine\Orm\Filter\SearchFilter; use ApiPlatform\Metadata; use ApiPlatform\Metadata\ApiProperty; use ApiPlatform\Metadata\ApiResource; @@ -18,6 +19,7 @@ use Symfony\Component\Serializer\Attribute\Groups; normalizationContext: ['groups' => ['post:collection:read', 'post:read']], )] #[GetCollection(normalizationContext: ['groups' => ['post:collection:read']])] +#[Metadata\ApiFilter(filterClass: SearchFilter::class, properties: ['species' => 'exact'])] class Post { #[ORM\Id] @@ -149,6 +151,8 @@ class Post #[ORM\PreUpdate] public function setPublicationDateValue(): void { - $this->publicationDate = new \DateTimeImmutable(); + if ($this->publicationDate === null) { + $this->publicationDate = new \DateTimeImmutable(); + } } } diff --git a/tests/Api/PostApiTest.php b/tests/Api/PostApiTest.php new file mode 100644 index 0000000..9304115 --- /dev/null +++ b/tests/Api/PostApiTest.php @@ -0,0 +1,114 @@ +getContainer()->get('doctrine')->getManager(); + $species = (new Species()) + ->setVernacularName('Oak') + ->setScientificName('Quercus') + ->setRegion('Europe'); + $post = (new Post()) + ->setPublicationDate(new \DateTimeImmutable('2024-06-07')) + ->setFoundDate(new \DateTimeImmutable('2024-06-06')) + ->setCommentary("maple") + ->setSpecies($species); + $manager->persist($species); + $manager->persist($post); + $manager->flush(); + $this->speciesId = $species->getId(); + $this->postId = $post->getId(); + } + + public function testFindInSpecies(): void + { + $response = static::createClient()->request('GET', '/api/species/' . $this->speciesId); + + $this->assertResponseIsSuccessful(); + $this->assertJsonEquals([ + '@context' => '/api/contexts/Species', + '@id' => '/api/species/' . $this->speciesId, + '@type' => 'Species', + 'id' => $this->speciesId, + 'vernacular_name' => 'Oak', + 'scientific_name' => 'Quercus', + 'region' => 'Europe', + 'posts' => ['/api/posts/' . $this->postId], + ]); + } + + public function testGetExisting(): void + { + $response = static::createClient()->request('GET', '/api/posts/' . $this->postId); + + $this->assertResponseIsSuccessful(); + $this->assertJsonEquals([ + '@context' => '/api/contexts/Post', + '@id' => '/api/posts/' . $this->postId, + '@type' => 'Post', + 'id' => $this->postId, + 'foundDate' => '2024-06-06T00:00:00+00:00', + 'publicationDate' => '2024-06-07T00:00:00+00:00', + 'commentary' => 'maple', + 'species' => '/api/species/' . $this->speciesId, + ]); + } + + public function testFilterBySpecies(): void + { + $response = static::createClient()->request('GET', '/api/posts', [ + 'query' => [ + 'species' => $this->speciesId, + ], + ]); + + $this->assertResponseIsSuccessful(); + $this->assertJsonContains([ + '@context' => '/api/contexts/Post', + '@id' => '/api/posts', + '@type' => 'hydra:Collection', + 'hydra:totalItems' => 1, + 'hydra:member' => [ + [ + '@type' => 'Post', + 'id' => $this->postId, + 'foundDate' => '2024-06-06T00:00:00+00:00', + 'species' => '/api/species/' . $this->speciesId, + ], + ], + ]); + } + + public function testPostSetPublicationDate(): void + { + $response = static::createClient()->request('POST', '/api/posts', [ + 'json' => [ + 'foundDate' => '2024-06-06', + 'publicationDate' => '2024-06-07', + 'commentary' => 'maple', + 'species' => '/api/species/' . $this->speciesId, + ], + ]); + + $this->assertResponseIsSuccessful(); + $this->assertJsonContains([ + '@context' => '/api/contexts/Post', + '@type' => 'Post', + 'foundDate' => '2024-06-06T00:00:00+00:00', + 'commentary' => 'maple', + 'species' => '/api/species/' . $this->speciesId, + ]); + $this->assertArrayHasKey('publicationDate', $response->toArray(false)); + } +}