Compare commits
No commits in common. "afe2a9fe92e7d91da768529185cbb7b8e5ecac4e" and "9f42085d5d6e148891499f4d355a0507f2259405" have entirely different histories.
afe2a9fe92
...
9f42085d5d
11 changed files with 114 additions and 379 deletions
|
|
@ -1,38 +0,0 @@
|
|||
<?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);
|
||||
}
|
||||
}
|
||||
55
src/Controller/SubjectController.php
Normal file
55
src/Controller/SubjectController.php
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
<?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('/');
|
||||
}
|
||||
}
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Repository\RatingRepository;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity(repositoryClass: RatingRepository::class)]
|
||||
class Rating
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\ManyToOne(inversedBy: 'ratings')]
|
||||
#[ORM\JoinColumn(nullable: false)]
|
||||
private ?Subject $subject_id = null;
|
||||
|
||||
#[ORM\Column(type: Types::SMALLINT, nullable: true)]
|
||||
private ?int $rating = null;
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getSubjectId(): ?Subject
|
||||
{
|
||||
return $this->subject_id;
|
||||
}
|
||||
|
||||
public function setSubjectId(?Subject $subject_id): static
|
||||
{
|
||||
$this->subject_id = $subject_id;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getRating(): ?int
|
||||
{
|
||||
return $this->rating;
|
||||
}
|
||||
|
||||
public function setRating(?int $rating): static
|
||||
{
|
||||
$this->rating = $rating;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
@ -4,15 +4,13 @@ namespace App\Entity;
|
|||
|
||||
use App\Entity\Restaurant;
|
||||
use App\Repository\SubjectRepository;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity(repositoryClass: SubjectRepository::class)]
|
||||
#[ORM\InheritanceType('SINGLE_TABLE')]
|
||||
#[ORM\DiscriminatorColumn(name: 'type', type: 'string')]
|
||||
#[ORM\DiscriminatorMap(['restaurant' => Restaurant::class])]
|
||||
abstract class Subject
|
||||
class Subject
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
|
|
@ -26,17 +24,6 @@ abstract class Subject
|
|||
#[ORM\JoinColumn(nullable: true)]
|
||||
private ?User $createdBy = null;
|
||||
|
||||
/**
|
||||
* @var Collection<int, Rating>
|
||||
*/
|
||||
#[ORM\OneToMany(targetEntity: Rating::class, mappedBy: 'subject_id', orphanRemoval: true)]
|
||||
private Collection $ratings;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->ratings = new ArrayCollection();
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
|
|
@ -65,34 +52,4 @@ abstract class Subject
|
|||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, Rating>
|
||||
*/
|
||||
public function getRatings(): Collection
|
||||
{
|
||||
return $this->ratings;
|
||||
}
|
||||
|
||||
public function addRating(Rating $rating): static
|
||||
{
|
||||
if (!$this->ratings->contains($rating)) {
|
||||
$this->ratings->add($rating);
|
||||
$rating->setSubjectId($this);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeRating(Rating $rating): static
|
||||
{
|
||||
if ($this->ratings->removeElement($rating)) {
|
||||
// set the owning side to null (unless already changed)
|
||||
if ($rating->getSubjectId() === $this) {
|
||||
$rating->setSubjectId(null);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,53 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Factory;
|
||||
|
||||
use App\Entity\Rating;
|
||||
use App\Repository\RatingRepository;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Zenstruck\Foundry\Persistence\PersistentObjectFactory;
|
||||
use Zenstruck\Foundry\Persistence\Proxy;
|
||||
use Zenstruck\Foundry\Persistence\ProxyRepositoryDecorator;
|
||||
|
||||
/**
|
||||
* @extends PersistentObjectFactory<Rating>
|
||||
*/
|
||||
final class RatingFactory 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 Rating::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 [
|
||||
'subject_id' => SubjectFactory::new(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
|
||||
*/
|
||||
#[\Override]
|
||||
protected function initialize(): static
|
||||
{
|
||||
return $this
|
||||
// ->afterInstantiate(function(Rating $rating): void {})
|
||||
;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,54 +0,0 @@
|
|||
<?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 {})
|
||||
;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,43 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Entity\Rating;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<Rating>
|
||||
*/
|
||||
class RatingRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Rating::class);
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @return Rating[] Returns an array of Rating objects
|
||||
// */
|
||||
// public function findByExampleField($value): array
|
||||
// {
|
||||
// return $this->createQueryBuilder('r')
|
||||
// ->andWhere('r.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->orderBy('r.id', 'ASC')
|
||||
// ->setMaxResults(10)
|
||||
// ->getQuery()
|
||||
// ->getResult()
|
||||
// ;
|
||||
// }
|
||||
|
||||
// public function findOneBySomeField($value): ?Rating
|
||||
// {
|
||||
// return $this->createQueryBuilder('r')
|
||||
// ->andWhere('r.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->getQuery()
|
||||
// ->getOneOrNullResult()
|
||||
// ;
|
||||
// }
|
||||
}
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
<?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,
|
||||
];
|
||||
}
|
||||
}
|
||||
38
tests/Integration/Subject/AddSubjectTest.php
Normal file
38
tests/Integration/Subject/AddSubjectTest.php
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
<?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');
|
||||
}
|
||||
}
|
||||
20
tests/Integration/Subject/ListSubjectsTest.php
Normal file
20
tests/Integration/Subject/ListSubjectsTest.php
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<?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());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,71 +0,0 @@
|
|||
<?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