Transform subject controller to API
This commit is contained in:
parent
9f42085d5d
commit
f0d17e9b44
8 changed files with 188 additions and 114 deletions
38
src/Controller/Api/SubjectController.php
Normal file
38
src/Controller/Api/SubjectController.php
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller\Api;
|
||||
|
||||
use App\Entity\Restaurant;
|
||||
use App\Repository\SubjectRepository;
|
||||
use App\Services\OutputService;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
class SubjectController extends AbstractController
|
||||
{
|
||||
#[Route('/api/subjects', methods: ['GET'])]
|
||||
public function index(
|
||||
SubjectRepository $repo,
|
||||
OutputService $output,
|
||||
): JsonResponse {
|
||||
return $this->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);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,55 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Restaurant;
|
||||
use App\Repository\SubjectRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
class SubjectController extends AbstractController
|
||||
{
|
||||
#[Route('/')]
|
||||
public function index(
|
||||
Request $request,
|
||||
EntityManagerInterface $em,
|
||||
SubjectRepository $repo,
|
||||
): Response {
|
||||
$subject = new Restaurant();
|
||||
$subject->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('/');
|
||||
}
|
||||
}
|
||||
|
|
@ -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]
|
||||
|
|
|
|||
54
src/Factory/SubjectFactory.php
Normal file
54
src/Factory/SubjectFactory.php
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
namespace App\Factory;
|
||||
|
||||
use App\Entity\Restaurant;
|
||||
use App\Entity\Subject;
|
||||
use App\Repository\SubjectRepository;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Zenstruck\Foundry\Persistence\PersistentObjectFactory;
|
||||
use Zenstruck\Foundry\Persistence\Proxy;
|
||||
use Zenstruck\Foundry\Persistence\ProxyRepositoryDecorator;
|
||||
|
||||
/**
|
||||
* @extends PersistentObjectFactory<Subject>
|
||||
*/
|
||||
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 {})
|
||||
;
|
||||
}
|
||||
}
|
||||
24
src/Services/OutputService.php
Normal file
24
src/Services/OutputService.php
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
class OutputService
|
||||
{
|
||||
public function success(array $output): array
|
||||
{
|
||||
return [
|
||||
'status' => 'success',
|
||||
'payload' => $output,
|
||||
'errors' => null,
|
||||
];
|
||||
}
|
||||
|
||||
public function error(array $errors): array
|
||||
{
|
||||
return [
|
||||
'status' => 'error',
|
||||
'payload' => null,
|
||||
'errors' => $errors,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Tests\Integration\Subject;
|
||||
|
||||
use App\Entity\Restaurant;
|
||||
use App\Factory\UserFactory;
|
||||
use App\Repository\SubjectRepository;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
class AddSubjectTest extends WebTestCase
|
||||
{
|
||||
public function testAddingNewSubject(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
|
||||
$user = UserFactory::createOne();
|
||||
|
||||
$client->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');
|
||||
}
|
||||
}
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Tests\Integration\Subject;
|
||||
|
||||
use App\Factory\RestaurantFactory;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
class ListSubjectsTest extends WebTestCase
|
||||
{
|
||||
public function testSubjectList(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
|
||||
$subject = RestaurantFactory::createOne();
|
||||
|
||||
$client->request('GET', '/');
|
||||
|
||||
$this->assertSelectorTextContains('ul li', $subject->getName());
|
||||
}
|
||||
}
|
||||
71
tests/Unit/Controller/SubjectControllerTest.php
Normal file
71
tests/Unit/Controller/SubjectControllerTest.php
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
|
||||
namespace App\Tests\Integration\Subject;
|
||||
|
||||
use App\Entity\Restaurant;
|
||||
use App\Factory\SubjectFactory;
|
||||
use App\Factory\UserFactory;
|
||||
use App\Repository\SubjectRepository;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
class SubjectControllerTest extends WebTestCase
|
||||
{
|
||||
public function testList(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
|
||||
$user = UserFactory::createOne();
|
||||
$client->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());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue