diff --git a/src/Controller/Api/SubjectController.php b/src/Controller/Api/SubjectController.php new file mode 100644 index 0000000..f2359ae --- /dev/null +++ b/src/Controller/Api/SubjectController.php @@ -0,0 +1,38 @@ +json($output->success(['subjects' => $repo->findAll()])); + } + + #[Route('/api/subjects', methods: ['POST'])] + public function create( + Request $request, + EntityManagerInterface $em, + ): JsonResponse { + $subject = new Restaurant(); + $subject->setName($request->getPayload()->get('name')); + $subject->setCreatedBy($this->getUser()); + + $em->persist($subject); + $em->flush(); + + return $this->json(null, 201); + } +} diff --git a/src/Controller/SubjectController.php b/src/Controller/SubjectController.php deleted file mode 100644 index 459b246..0000000 --- a/src/Controller/SubjectController.php +++ /dev/null @@ -1,55 +0,0 @@ -setName("MyRestaurant"); - - $form = $this->createFormBuilder($subject) - ->add('name', TextType::class) - ->add('save', SubmitType::class) - ->getForm(); - - $form->handleRequest($request); - - if ($form->isSubmitted() && $form->isValid()) { - $subject->setCreatedBy($this->getUser()); - $em->persist($subject); - $em->flush(); - - return $this->redirectToRoute('app_subject_index'); - } - - $subjects = $repo->findAll(); - - return $this->render('index/page.html.twig', [ - 'form' => $form, - 'subjects' => $subjects, - ]); - } - - #[Route('/', 'subject_new', methods: ["POST"])] - public function save(): RedirectResponse - { - return $this->redirect('/'); - } -} diff --git a/src/Entity/Subject.php b/src/Entity/Subject.php index 48167c6..4a6b712 100644 --- a/src/Entity/Subject.php +++ b/src/Entity/Subject.php @@ -10,7 +10,7 @@ use Doctrine\ORM\Mapping as ORM; #[ORM\InheritanceType('SINGLE_TABLE')] #[ORM\DiscriminatorColumn(name: 'type', type: 'string')] #[ORM\DiscriminatorMap(['restaurant' => Restaurant::class])] -class Subject +abstract class Subject { #[ORM\Id] #[ORM\GeneratedValue] diff --git a/src/Factory/SubjectFactory.php b/src/Factory/SubjectFactory.php new file mode 100644 index 0000000..bbd240f --- /dev/null +++ b/src/Factory/SubjectFactory.php @@ -0,0 +1,54 @@ + + */ +final class SubjectFactory extends PersistentObjectFactory +{ + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services + * + * @todo inject services if required + */ + public function __construct() {} + + #[\Override] + public static function class(): string + { + return Restaurant::class; + } + + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories + * + * @todo add your default values here + */ + #[\Override] + protected function defaults(): array|callable + { + return [ + 'name' => self::faker()->text(255), + ]; + } + + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization + */ + #[\Override] + protected function initialize(): static + { + return $this + // ->afterInstantiate(function(Subject $subject): void {}) + ; + } +} diff --git a/src/Services/OutputService.php b/src/Services/OutputService.php new file mode 100644 index 0000000..cca505c --- /dev/null +++ b/src/Services/OutputService.php @@ -0,0 +1,24 @@ + 'success', + 'payload' => $output, + 'errors' => null, + ]; + } + + public function error(array $errors): array + { + return [ + 'status' => 'error', + 'payload' => null, + 'errors' => $errors, + ]; + } +} diff --git a/tests/Integration/Subject/AddSubjectTest.php b/tests/Integration/Subject/AddSubjectTest.php deleted file mode 100644 index dc3b663..0000000 --- a/tests/Integration/Subject/AddSubjectTest.php +++ /dev/null @@ -1,38 +0,0 @@ -loginUser($user); - - $client->request('GET', '/'); - $client->submitForm('form_save', ['form[name]' => 'Riviera']); - - $this->assertResponseRedirects(); - - $crawler = $client->followRedirect(); - - $subject = static::getContainer() - ->get(SubjectRepository::class) - ->findOneBy(['name' => 'Riviera']); - - $this->assertNotNull($subject); - $this->assertInstanceOf(Restaurant::class, $subject); - $this->assertNotNull($subject->getCreatedBy()); - $this->assertSame($user->getId(), $subject->getCreatedBy()->getId()); - - $this->assertSelectorTextContains('ul li', 'Riviera'); - } -} diff --git a/tests/Integration/Subject/ListSubjectsTest.php b/tests/Integration/Subject/ListSubjectsTest.php deleted file mode 100644 index b2f92d9..0000000 --- a/tests/Integration/Subject/ListSubjectsTest.php +++ /dev/null @@ -1,20 +0,0 @@ -request('GET', '/'); - - $this->assertSelectorTextContains('ul li', $subject->getName()); - } -} diff --git a/tests/Unit/Controller/SubjectControllerTest.php b/tests/Unit/Controller/SubjectControllerTest.php new file mode 100644 index 0000000..af8748e --- /dev/null +++ b/tests/Unit/Controller/SubjectControllerTest.php @@ -0,0 +1,71 @@ +loginUser($user); + + SubjectFactory::createMany(5, [ + 'createdBy' => $user, + ]); + + static::getContainer() + ->get(SubjectRepository::class) + ->findOneBy(['name' => 'Riviera']); + + $client->request( + 'GET', + '/api/subjects', + server: ['CONTENT_TYPE' => 'application/json'], + content: json_encode(['name' => 'Riviera']), + ); + + $data = json_decode($client->getResponse()->getContent(), true); + $this->assertResponseIsSuccessful(); + $this->assertCount(5, $data['payload']['subjects']); + } + + public function testAddingNewSubjectViaApi(): void + { + $client = static::createClient(); + + $repo = static::getContainer()->get(SubjectRepository::class); + + $user = UserFactory::createOne(); + $client->loginUser($user); + + $this->assertEmpty($repo->findAll()); + + $client->request( + 'POST', + '/api/subjects', + server: ['CONTENT_TYPE' => 'application/json'], + content: json_encode(['name' => 'Riviera']), + ); + + $this->assertResponseIsSuccessful(); + $this->assertResponseStatusCodeSame(201); + $this->assertCount(1, $repo->findAll()); + + $subject = static::getContainer() + ->get(SubjectRepository::class) + ->findOneBy(['name' => 'Riviera']); + + $this->assertNotNull($subject); + $this->assertInstanceOf(Restaurant::class, $subject); + $this->assertNotNull($subject->getCreatedBy()); + $this->assertSame($user->getId(), $subject->getCreatedBy()->getId()); + } +}