Add update+delete endpoints
This commit is contained in:
parent
3e297f4d5c
commit
f3d5d729bf
2 changed files with 115 additions and 1 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue