From 1dc528f269d9a536fcdf39d4354c241c8f891537 Mon Sep 17 00:00:00 2001 From: clfreville2 Date: Fri, 7 Jun 2024 17:30:57 +0200 Subject: [PATCH] Fix tests --- src/Controller/PostController.php | 8 +-- src/Controller/SpeciesController.php | 8 +-- src/DataFixtures/AppFixtures.php | 14 +++- src/Entity/Post.php | 9 +++ src/Repository/UserRepository.php | 10 +++ tests/Controller/PostControllerTest.php | 80 +++++++++------------- tests/Controller/SpeciesControllerTest.php | 35 ++++++---- 7 files changed, 93 insertions(+), 71 deletions(-) diff --git a/src/Controller/PostController.php b/src/Controller/PostController.php index 2c71a98..c8cdd5f 100644 --- a/src/Controller/PostController.php +++ b/src/Controller/PostController.php @@ -24,7 +24,7 @@ class PostController extends AbstractController } #[Route('/new', name: 'app_post_new', methods: ['GET', 'POST'])] - #[IsGranted('ROLE_USER', message: 'You must be logged in to access this page.')] + #[IsGranted('ROLE_USER')] public function new(Request $request, EntityManagerInterface $entityManager): Response { $post = new Post(); @@ -53,7 +53,7 @@ class PostController extends AbstractController } #[Route('/{id}/edit', name: 'app_post_edit', methods: ['GET', 'POST'])] - #[IsGranted('ROLE_USER', message: 'You must be logged in to access this page.')] + #[IsGranted('ROLE_USER')] public function edit(Request $request, Post $post, EntityManagerInterface $entityManager): Response { $form = $this->createForm(PostType::class, $post); @@ -72,10 +72,10 @@ class PostController extends AbstractController } #[Route('/{id}', name: 'app_post_delete', methods: ['POST'])] - #[IsGranted('ROLE_USER', message: 'You must be logged in to access this page.')] + #[IsGranted('ROLE_USER')] public function delete(Request $request, Post $post, EntityManagerInterface $entityManager): Response { - if ($this->isCsrfTokenValid('delete'.$post->getId(), $request->getPayload()->get('_token'))) { + if ($this->isCsrfTokenValid('delete'.$post->getId(), (string) $request->getPayload()->get('_token'))) { $entityManager->remove($post); $entityManager->flush(); } diff --git a/src/Controller/SpeciesController.php b/src/Controller/SpeciesController.php index 4e5d4cf..b63b960 100644 --- a/src/Controller/SpeciesController.php +++ b/src/Controller/SpeciesController.php @@ -24,7 +24,7 @@ class SpeciesController extends AbstractController } #[Route('/new', name: 'app_species_new', methods: ['GET', 'POST'])] - #[IsGranted('ROLE_USER', message: 'You must be logged in to access this page.')] + #[IsGranted('ROLE_USER')] public function new(Request $request, EntityManagerInterface $entityManager): Response { $species = new Species(); @@ -53,7 +53,7 @@ class SpeciesController extends AbstractController } #[Route('/{id}/edit', name: 'app_species_edit', methods: ['GET', 'POST'])] - #[IsGranted('ROLE_USER', message: 'You must be logged in to access this page.')] + #[IsGranted('ROLE_USER')] public function edit(Request $request, Species $species, EntityManagerInterface $entityManager): Response { $form = $this->createForm(SpeciesType::class, $species); @@ -72,10 +72,10 @@ class SpeciesController extends AbstractController } #[Route('/{id}', name: 'app_species_delete', methods: ['POST'])] - #[IsGranted('ROLE_USER', message: 'You must be logged in to access this page.')] + #[IsGranted('ROLE_USER')] public function delete(Request $request, Species $species, EntityManagerInterface $entityManager): Response { - if ($this->isCsrfTokenValid('delete'.$species->getId(), $request->getPayload()->get('_token'))) { + if ($this->isCsrfTokenValid('delete'.$species->getId(), (string) $request->getPayload()->get('_token'))) { $entityManager->remove($species); $entityManager->flush(); } diff --git a/src/DataFixtures/AppFixtures.php b/src/DataFixtures/AppFixtures.php index 12d5f40..3b45990 100644 --- a/src/DataFixtures/AppFixtures.php +++ b/src/DataFixtures/AppFixtures.php @@ -4,17 +4,29 @@ namespace App\DataFixtures; use App\Entity\Post; use App\Entity\Species; +use App\Entity\User; use DateTimeImmutable; use Doctrine\Bundle\FixturesBundle\Fixture; use Doctrine\Persistence\ObjectManager; +use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; class AppFixtures extends Fixture { + public function __construct( + private readonly UserPasswordHasherInterface $passwordHasher + ) + { + } + public function load(ObjectManager $manager): void { + $user = (new User())->setEmail('test@test.fr'); + $user->setPassword($this->passwordHasher->hashPassword($user, 'password')); + $manager->persist($user); + $faker = \Faker\Factory::create(); for ($i = 0; $i < 20; ++$i) { - $name = $faker->text(); + $name = $faker->name(); $species = (new Species()) ->setScientificName($name) ->setVernacularName($name) diff --git a/src/Entity/Post.php b/src/Entity/Post.php index 21a5976..58f13d2 100644 --- a/src/Entity/Post.php +++ b/src/Entity/Post.php @@ -8,6 +8,7 @@ use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; #[ORM\Entity(repositoryClass: PostRepository::class)] +#[ORM\HasLifecycleCallbacks] class Post { #[ORM\Id] @@ -128,4 +129,12 @@ class Post return $this; } + + #[ORM\PrePersist] + public function setPublicationDateValue(): void + { + if ($this->publicationDate === null) { + $this->publicationDate = new \DateTimeImmutable(); + } + } } diff --git a/src/Repository/UserRepository.php b/src/Repository/UserRepository.php index 4f2804e..e861224 100644 --- a/src/Repository/UserRepository.php +++ b/src/Repository/UserRepository.php @@ -33,6 +33,16 @@ class UserRepository extends ServiceEntityRepository implements PasswordUpgrader $this->getEntityManager()->flush(); } + public function findOneByEmail(string $email): ?User + { + return $this->createQueryBuilder('u') + ->andWhere('u.email = :email') + ->setParameter('email', $email) + ->getQuery() + ->getOneOrNullResult() + ; + } + // /** // * @return User[] Returns an array of User objects // */ diff --git a/tests/Controller/PostControllerTest.php b/tests/Controller/PostControllerTest.php index a94c010..ea52747 100644 --- a/tests/Controller/PostControllerTest.php +++ b/tests/Controller/PostControllerTest.php @@ -3,8 +3,9 @@ namespace App\Test\Controller; use App\Entity\Post; +use App\Entity\User; +use App\Repository\PostRepository; use Doctrine\ORM\EntityManagerInterface; -use Doctrine\ORM\EntityRepository; use Symfony\Bundle\FrameworkBundle\KernelBrowser; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; @@ -12,13 +13,15 @@ class PostControllerTest extends WebTestCase { private KernelBrowser $client; private EntityManagerInterface $manager; - private EntityRepository $repository; + private PostRepository $repository; private string $path = '/post/'; protected function setUp(): void { $this->client = static::createClient(); - $this->manager = static::getContainer()->get('doctrine')->getManager(); + /** @var EntityManagerInterface $manager */ + $manager = static::getContainer()->get(EntityManagerInterface::class); + $this->manager = $manager; $this->repository = $this->manager->getRepository(Post::class); foreach ($this->repository->findAll() as $object) { @@ -26,6 +29,12 @@ class PostControllerTest extends WebTestCase } $this->manager->flush(); + + $userRepository = $this->manager->getRepository(User::class); + /** @var User $user */ + $user = $userRepository->findOneByEmail('test@test.fr'); + $this->client->loginUser($user); + $this->client->request('GET', sprintf('%snew', $this->path)); } public function testIndex(): void @@ -41,35 +50,26 @@ class PostControllerTest extends WebTestCase public function testNew(): void { - $this->markTestIncomplete(); - $this->client->request('GET', sprintf('%snew', $this->path)); - self::assertResponseStatusCodeSame(200); $this->client->submitForm('Save', [ - 'post[foundDate]' => 'Testing', - 'post[latitude]' => 'Testing', - 'post[longitude]' => 'Testing', - 'post[altitude]' => 'Testing', + 'post[foundDate]' => '2024-01-01 00:00:00', + 'post[latitude]' => '45.0', + 'post[longitude]' => '45.0', + 'post[altitude]' => '500.0', 'post[commentary]' => 'Testing', - 'post[species]' => 'Testing', ]); self::assertResponseRedirects($this->path); - self::assertSame(1, $this->repository->count([])); + self::assertSame(1, $this->repository->count()); } public function testShow(): void { - $this->markTestIncomplete(); $fixture = new Post(); - $fixture->setFoundDate('My Title'); - $fixture->setLatitude('My Title'); - $fixture->setLongitude('My Title'); - $fixture->setAltitude('My Title'); - $fixture->setCommentary('My Title'); - $fixture->setSpecies('My Title'); + $fixture->setFoundDate(new \DateTimeImmutable('2024-01-01 00:00:00')); + $fixture->setCommentary('Cool stuff'); $this->manager->persist($fixture); $this->manager->flush(); @@ -77,21 +77,14 @@ class PostControllerTest extends WebTestCase $this->client->request('GET', sprintf('%s%s', $this->path, $fixture->getId())); self::assertResponseStatusCodeSame(200); - self::assertPageTitleContains('Post'); - - // Use assertions to check that the properties are properly displayed. + self::assertSelectorTextContains('h1', 'Post'); } public function testEdit(): void { - $this->markTestIncomplete(); $fixture = new Post(); - $fixture->setFoundDate('Value'); - $fixture->setLatitude('Value'); - $fixture->setLongitude('Value'); - $fixture->setAltitude('Value'); - $fixture->setCommentary('Value'); - $fixture->setSpecies('Value'); + $fixture->setFoundDate(new \DateTimeImmutable('2024-01-01 00:00:00')); + $fixture->setCommentary('Cool stuff'); $this->manager->persist($fixture); $this->manager->flush(); @@ -99,36 +92,29 @@ class PostControllerTest extends WebTestCase $this->client->request('GET', sprintf('%s%s/edit', $this->path, $fixture->getId())); $this->client->submitForm('Update', [ - 'post[foundDate]' => 'Something New', - 'post[latitude]' => 'Something New', - 'post[longitude]' => 'Something New', - 'post[altitude]' => 'Something New', + 'post[foundDate]' => '2024-03-25 00:00:00', + 'post[latitude]' => '90', + 'post[longitude]' => '90', + 'post[altitude]' => '200', 'post[commentary]' => 'Something New', - 'post[species]' => 'Something New', ]); self::assertResponseRedirects('/post/'); $fixture = $this->repository->findAll(); - self::assertSame('Something New', $fixture[0]->getFoundDate()); - self::assertSame('Something New', $fixture[0]->getLatitude()); - self::assertSame('Something New', $fixture[0]->getLongitude()); - self::assertSame('Something New', $fixture[0]->getAltitude()); + self::assertEquals(new \DateTimeImmutable('2024-03-25 00:00:00'), $fixture[0]->getFoundDate()); + self::assertSame(90., $fixture[0]->getLatitude()); + self::assertSame(90., $fixture[0]->getLongitude()); + self::assertSame(200., $fixture[0]->getAltitude()); self::assertSame('Something New', $fixture[0]->getCommentary()); - self::assertSame('Something New', $fixture[0]->getSpecies()); } public function testRemove(): void { - $this->markTestIncomplete(); $fixture = new Post(); - $fixture->setFoundDate('Value'); - $fixture->setLatitude('Value'); - $fixture->setLongitude('Value'); - $fixture->setAltitude('Value'); - $fixture->setCommentary('Value'); - $fixture->setSpecies('Value'); + $fixture->setFoundDate(new \DateTimeImmutable('2024-01-01 00:00:00')); + $fixture->setCommentary('Cool stuff'); $this->manager->persist($fixture); $this->manager->flush(); @@ -137,6 +123,6 @@ class PostControllerTest extends WebTestCase $this->client->submitForm('Delete'); self::assertResponseRedirects('/post/'); - self::assertSame(0, $this->repository->count([])); + self::assertSame(0, $this->repository->count()); } } diff --git a/tests/Controller/SpeciesControllerTest.php b/tests/Controller/SpeciesControllerTest.php index ef3289e..5ce80dd 100644 --- a/tests/Controller/SpeciesControllerTest.php +++ b/tests/Controller/SpeciesControllerTest.php @@ -3,8 +3,9 @@ namespace App\Test\Controller; use App\Entity\Species; +use App\Entity\User; +use App\Repository\SpeciesRepository; use Doctrine\ORM\EntityManagerInterface; -use Doctrine\ORM\EntityRepository; use Symfony\Bundle\FrameworkBundle\KernelBrowser; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; @@ -12,13 +13,15 @@ class SpeciesControllerTest extends WebTestCase { private KernelBrowser $client; private EntityManagerInterface $manager; - private EntityRepository $repository; + private SpeciesRepository $repository; private string $path = '/species/'; protected function setUp(): void { $this->client = static::createClient(); - $this->manager = static::getContainer()->get('doctrine')->getManager(); + /** @var EntityManagerInterface $manager */ + $manager = static::getContainer()->get(EntityManagerInterface::class); + $this->manager = $manager; $this->repository = $this->manager->getRepository(Species::class); foreach ($this->repository->findAll() as $object) { @@ -26,6 +29,12 @@ class SpeciesControllerTest extends WebTestCase } $this->manager->flush(); + + $userRepository = $this->manager->getRepository(User::class); + /** @var User $user */ + $user = $userRepository->findOneByEmail('test@test.fr'); + $this->client->loginUser($user); + $this->client->request('GET', sprintf('%snew', $this->path)); } public function testIndex(): void @@ -41,7 +50,6 @@ class SpeciesControllerTest extends WebTestCase public function testNew(): void { - $this->markTestIncomplete(); $this->client->request('GET', sprintf('%snew', $this->path)); self::assertResponseStatusCodeSame(200); @@ -59,10 +67,9 @@ class SpeciesControllerTest extends WebTestCase public function testShow(): void { - $this->markTestIncomplete(); $fixture = new Species(); - $fixture->setScientific_name('My Title'); - $fixture->setVernacular_name('My Title'); + $fixture->setScientificName('My Title'); + $fixture->setVernacularName('My Title'); $fixture->setRegion('My Title'); $this->manager->persist($fixture); @@ -78,10 +85,9 @@ class SpeciesControllerTest extends WebTestCase public function testEdit(): void { - $this->markTestIncomplete(); $fixture = new Species(); - $fixture->setScientific_name('Value'); - $fixture->setVernacular_name('Value'); + $fixture->setScientificName('Value'); + $fixture->setVernacularName('Value'); $fixture->setRegion('Value'); $this->manager->persist($fixture); @@ -99,17 +105,16 @@ class SpeciesControllerTest extends WebTestCase $fixture = $this->repository->findAll(); - self::assertSame('Something New', $fixture[0]->getScientific_name()); - self::assertSame('Something New', $fixture[0]->getVernacular_name()); + self::assertSame('Something New', $fixture[0]->getScientificName()); + self::assertSame('Something New', $fixture[0]->getVernacularName()); self::assertSame('Something New', $fixture[0]->getRegion()); } public function testRemove(): void { - $this->markTestIncomplete(); $fixture = new Species(); - $fixture->setScientific_name('Value'); - $fixture->setVernacular_name('Value'); + $fixture->setScientificName('Value'); + $fixture->setVernacularName('Value'); $fixture->setRegion('Value'); $this->manager->persist($fixture);