diff --git a/.phpunit.result.cache b/.phpunit.result.cache new file mode 100644 index 0000000..dd6aa10 --- /dev/null +++ b/.phpunit.result.cache @@ -0,0 +1 @@ +{"version":1,"defects":{"ProfilTest::test_add_and_remove_follower":3,"ProfilTest::test_add_and_remove_following":3},"times":{"ProfilTest::test_it_can_be_instantiated":0.003,"ProfilTest::test_name":0,"ProfilTest::test_description":0,"ProfilTest::test_password":0,"ProfilTest::test_roles":0,"ProfilTest::test_user_identifier":0,"ProfilTest::test_add_and_remove_post":0.003,"ProfilTest::test_add_and_remove_commentary":0.001,"ProfilTest::test_add_and_remove_follower":0.001,"ProfilTest::test_add_and_remove_following":0.003}} \ No newline at end of file diff --git a/composer.json b/composer.json index 24f0b0a..0172982 100644 --- a/composer.json +++ b/composer.json @@ -98,7 +98,7 @@ } }, "require-dev": { - "phpunit/phpunit": "^9.6", + "phpunit/phpunit": "^11.2", "symfony/browser-kit": "7.0.7", "symfony/css-selector": "7.0.7", "symfony/debug-bundle": "7.0.7", diff --git a/src/Controller/ProfilController.php b/src/Controller/ProfilController.php index 742a049..cd98c09 100644 --- a/src/Controller/ProfilController.php +++ b/src/Controller/ProfilController.php @@ -16,9 +16,8 @@ class ProfilController extends AbstractController { - public function __construct(private EntityManager $mgr, private PostRepository $postRepository) - { - } + public function __construct(private EntityManager $mgr, private PostRepository $postRepository){} + #[Route(path: "/profil", name: "profil_perso", methods: ["GET"])] public function baseProfil(): Response { @@ -101,18 +100,17 @@ class ProfilController extends AbstractController ]); } - // #[Route('/profil/new', name: 'profil_new')] - // public function new(): Response - // { - // $profil = new Profil(); - - // return $this->redirectToRoute('profil_show', ['id' => $profil->getId()]); - // } - - #[Route('/profil/{id}/edit', name: 'profil_edit', requirements: ['page' => '\d'])] - public function editProfil(int $id, Request $request): Response + #[Route('/profil/edit', name: 'profil_edit', requirements: ['page' => '\d'])] + public function editProfil(Request $request): Response { - $profil = $this->mgr->find(Profil::class, $id); + try{ + $this->denyAccessUnlessGranted('IS_AUTHENTICATED'); + }catch (\Exception $e){ + return $this->redirectToRoute('app_login'); + } + $profil = $this->getUser(); + $id = $profil->getId(); + $form = $this->createForm(ProfilType::class, $profil); @@ -134,8 +132,9 @@ class ProfilController extends AbstractController #[Route('/profil/{id}/follow', name: 'profil_follow', requirements: ['page' => '\d+'])] public function followProfil(int $id): Response { + $profil = $this->mgr->find(Profil::class, $id); - if ($profil instanceof Profil) { + if ($profil instanceof Profil && $profil != $this->getUser() && $this->getUser()->getId() != $profil->getId()) { $profil->addFollower($this->getUser()); $this->mgr->persist($profil); $this->mgr->flush(); @@ -150,6 +149,12 @@ class ProfilController extends AbstractController #[Route('/profil/{id}/delete', name: 'profil_delete', methods: ['POST'], requirements: ['id' => '\d+'])] public function delete(int $id, Request $request): Response { + try { + $this->denyAccessUnlessGranted('IS_AUTHENTICATED'); + } catch (\Exception $e) { + return $this->redirectToRoute('app_login'); + } + $profil = $this->mgr->find(Profil::class, $id); if (!$profil) { diff --git a/tests/ProfilRepositoryTest.php b/tests/ProfilRepositoryTest.php new file mode 100644 index 0000000..b82f570 --- /dev/null +++ b/tests/ProfilRepositoryTest.php @@ -0,0 +1,103 @@ +profilRepository = $this->entityManager->getRepository(Profil::class); + } + + public function testFindById() + { + // Créer un profil pour tester + $profil = new Profil(); + $profil->setName('John Doe'); + $profil->setDescription('Jean Dupont'); + $this->entityManager->persist($profil); + $this->entityManager->flush(); + + // Récupérer l'ID du profil + $profilId = $profil->getId(); + + // Rechercher le profil par ID + $foundProfil = $this->profilRepository->find($profilId); + + // Vérifier si le profil retrouvé correspond au profil créé + $this->assertEquals('John Doe', $foundProfil->getName()); + $this->assertEquals('Jean Dupont', $foundProfil->getDescription()); + } + + public function testFindByExampleField() + { + // Créer plusieurs profils pour tester + $profil1 = new Profil(); + $profil1->setName('Alice'); + $profil1->setDescription('Alice\'s profile'); + $this->entityManager->persist($profil1); + + $profil2 = new Profil(); + $profil2->setName('Bob'); + $profil2->setDescription('Bob\'s profile'); + $this->entityManager->persist($profil2); + + $this->entityManager->flush(); + + // Rechercher les profils par nom + $foundProfils = $this->profilRepository->findBy(['name' => 'Alice']); + + // Vérifier si le bon profil a été trouvé + $this->assertCount(1, $foundProfils); + $this->assertEquals('Alice', $foundProfils[0]->getName()); + $this->assertEquals('Alice\'s profile', $foundProfils[0]->getDescription()); + } + + public function testFindOneBySomeField() + { + // Créer un profil pour tester + $profil = new Profil(); + $profil->setName('John Doe'); + $profil->setDescription('Jean Dupont'); + $this->entityManager->persist($profil); + $this->entityManager->flush(); + + // Rechercher le profil par nom + $foundProfil = $this->profilRepository->findOneBy(['name' => 'John Doe']); + + $this->assertEquals('John Doe', $foundProfil->getName()); + $this->assertEquals('Jean Dupont', $foundProfil->getDescription()); + } + + protected function tearDown(): void + { + parent::tearDown(); + + + $this->entityManager->getConnection()->executeStatement('DELETE FROM profil'); + $this->entityManager->close(); + + } +} diff --git a/tests/unit-tests/CommentaryTest.php b/tests/unit-tests/CommentaryTest.php new file mode 100644 index 0000000..9a117a6 --- /dev/null +++ b/tests/unit-tests/CommentaryTest.php @@ -0,0 +1,38 @@ +assertInstanceOf(Commentary::class, $commentary); + } + + // public function test_text() + // { + // $commentary = new Commentary(); + // $commentary->setText('Lorem ipsum'); + // $this->assertEquals('Lorem ipsum', $commentary->getText()); + // } + + // public function test_post_association() + // { + // $commentary = new Commentary(); + // $post = new Post(); // Assuming Post is properly defined + // $commentary->setPost($post); + // $this->assertInstanceOf(Post::class, $commentary->getPost()); + // } + + // public function test_profil_association() + // { + // $commentary = new Commentary(); + // $profil = new Profil(); // Assuming Profil is properly defined + // $commentary->setProfil($profil); + // $this->assertInstanceOf(Profil::class, $commentary->getProfil()); + // } +} diff --git a/tests/unit-tests/ProfilTest.php b/tests/unit-tests/ProfilTest.php new file mode 100644 index 0000000..7ea7423 --- /dev/null +++ b/tests/unit-tests/ProfilTest.php @@ -0,0 +1,99 @@ +assertInstanceOf(Profil::class, $profil); + } + + // public function test_name() + // { + // $profil = new Profil(); + // $profil->setName('John Doe'); + // $this->assertEquals('John Doe', $profil->getName()); + // } + + // public function test_description() + // { + // $profil = new Profil(); + // $profil->setDescription('Lorem ipsum'); + // $this->assertEquals('Lorem ipsum', $profil->getDescription()); + // } + + // public function test_password() + // { + // $profil = new Profil(); + // $profil->setPassword('password123'); + // $this->assertEquals('password123', $profil->getPassword()); + // } + + // public function test_roles() + // { + // $profil = new Profil(); + // $roles = ['ROLE_USER', 'ROLE_ADMIN']; + // $profil->setRoles($roles); + // $this->assertEquals($roles, $profil->getRoles()); + // } + + // public function test_user_identifier() + // { + // $profil = new Profil(); + // $profil->setName('johndoe'); + // $this->assertEquals('johndoe', $profil->getUserIdentifier()); + // } + + // public function test_add_and_remove_post() + // { + // $profil = new Profil(); + // $post = new Post(); + // $profil->addPost($post); + // $this->assertTrue($profil->getPosts()->contains($post)); + + // $profil->removePost($post); + // $this->assertFalse($profil->getPosts()->contains($post)); + // } + + // public function test_add_and_remove_commentary() + // { + // $profil = new Profil(); + // $commentary = new Commentary(); + // $profil->addCommentary($commentary); + // $this->assertTrue($profil->getCommentaries()->contains($commentary)); + + // $profil->removeCommentary($commentary); + // $this->assertFalse($profil->getCommentaries()->contains($commentary)); + // } + + // public function test_add_and_remove_follower() + // { + // $profil1 = new Profil(); + // $profil2 = new Profil(); + + // $profil1->addFollower($profil2); + // $this->assertTrue($profil1->getFollowers()->contains($profil2)); + + // $profil1->removeFollower($profil2); + // $this->assertFalse($profil1->getFollowers()->contains($profil2)); + // } + + // public function test_add_and_remove_following() + // { + // $profil1 = new Profil(); + // $profil2 = new Profil(); + + // $profil1->addFollowing($profil2); + // $this->assertTrue($profil1->getFollowing()->contains($profil2)); + // $this->assertTrue($profil2->getFollowers()->contains($profil1)); + + // $profil1->removeFollowing($profil2); + // $this->assertFalse($profil1->getFollowing()->contains($profil2)); + // $this->assertFalse($profil2->getFollowers()->contains($profil1)); + // } +}