diff --git a/src/Controller/Api/SubjectController.php b/src/Controller/Api/SubjectController.php index befc727..57a6d41 100644 --- a/src/Controller/Api/SubjectController.php +++ b/src/Controller/Api/SubjectController.php @@ -39,4 +39,50 @@ class SubjectController extends AbstractController return $this->json(null, 201); } + + #[Route('/api/subjects/{id}', methods: ['PATCH'])] + public function update( + int $id, + Request $request, + EntityManagerInterface $em, + OutputService $output, + SubjectRepository $repository, + ): JsonResponse { + $subject = $repository->findById($id); + + if ($subject === null) { + return $this->json( + $output->error(["UNKNOWN_RATING"]) + ); + } + + $payload = $request->getPayload(); + + if ($payload->has('name')) { + $subject->setName($payload->getString('name')); + } + + $em->flush(); + + return $this->json(null, 202); + } + + #[Route('/api/subjects/{id}', methods: ['DELETE'])] + public function delete( + int $id, + EntityManagerInterface $em, + OutputService $output, + SubjectRepository $repository, + ): JsonResponse { + $subject = $repository->findById($id); + + if ($subject === null) { + return $this->json($output->error(["UNKNOWN_RATING"])); + } + + $em->remove($subject); + $em->flush(); + + return $this->json(null, 204); + } } diff --git a/tests/Application/Controller/Api/SubjectControllerTest.php b/tests/Application/Controller/Api/SubjectControllerTest.php index 3105b33..340060a 100644 --- a/tests/Application/Controller/Api/SubjectControllerTest.php +++ b/tests/Application/Controller/Api/SubjectControllerTest.php @@ -2,10 +2,15 @@ namespace App\Tests\Unit\Controller\Api; +use App\Entity\Rating; use App\Entity\Restaurant; +use App\Entity\Subject; +use App\Factory\RatingFactory; +use App\Factory\RestaurantFactory; use App\Factory\SubjectFactory; use App\Factory\UserFactory; use App\Repository\SubjectRepository; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class SubjectControllerTest extends WebTestCase @@ -37,7 +42,7 @@ class SubjectControllerTest extends WebTestCase $this->assertCount(5, $data['payload']['subjects']); } - public function testAddingNewSubjectViaApi(): void + public function testCreate(): void { $client = static::createClient(); @@ -68,4 +73,67 @@ class SubjectControllerTest extends WebTestCase $this->assertNotNull($subject->getCreatedBy()); $this->assertSame($user->getId(), $subject->getCreatedBy()->getId()); } + + public function testUpdate(): void + { + $client = static::createClient(); + + $user = UserFactory::createOne(); + $client->loginUser($user); + + $subject = RestaurantFactory::createOne([ + 'name' => 'Old name', + 'createdBy' => $user, + ]); + + $name = 'New name'; + + $client->request( + 'PATCH', + '/api/subjects/' . $subject->getId(), + server: ['CONTENT_TYPE' => 'application/json'], + content: json_encode([ + 'name' => $name, + ]), + ); + + $this->assertResponseIsSuccessful(); + $this->assertResponseStatusCodeSame(202); + + $em = static::getContainer()->get(EntityManagerInterface::class); + $em->clear(); + $updated = $em->getRepository(Subject::class)->find($subject->getId()); + + $this->assertSame($name, $updated->getName()); + } + + public function testDelete(): void + { + $client = static::createClient(); + + $user = UserFactory::createOne(); + $client->loginUser($user); + + $subject = RestaurantFactory::createOne([ + 'name' => 'My Restaurant', + 'createdBy' => $user, + ]); + $id = $subject->getId(); + + $client->request( + 'DELETE', + '/api/subjects/' . $id, + server: ['CONTENT_TYPE' => 'application/json'], + ); + + $this->assertResponseIsSuccessful(); + $this->assertResponseStatusCodeSame(204); + + $em = static::getContainer()->get(EntityManagerInterface::class); + $em->clear(); + + $this->assertNull( + $em->getRepository(Subject::class)->find($id) + ); + } }