From de98fdbb1345b84ee82797c77df66d62d76b3806 Mon Sep 17 00:00:00 2001 From: dorian Date: Thu, 13 Jun 2024 09:31:44 +0200 Subject: [PATCH] Adding phpunit settings and some tests --- composer.json | 2 +- composer.lock | 2 +- phpunit.xml.dist | 38 --------- src/Controller/PostController.php | 2 +- src/Entity/.gitignore | 0 src/Entity/Post.php | 8 ++ src/Entity/Profil.php | 2 +- tests/Controller/PostControllerTest.php | 92 ++++++++++++++++++++ tests/Entity/CommentaryTest.php | 44 ++++++++++ tests/Entity/PostTest.php | 103 +++++++++++++++++++++++ tests/Entity/ProfilTest.php | 107 ++++++++++++++++++++++++ 11 files changed, 358 insertions(+), 42 deletions(-) delete mode 100644 phpunit.xml.dist delete mode 100644 src/Entity/.gitignore create mode 100644 tests/Controller/PostControllerTest.php create mode 100644 tests/Entity/CommentaryTest.php create mode 100644 tests/Entity/PostTest.php create mode 100644 tests/Entity/ProfilTest.php diff --git a/composer.json b/composer.json index 15798fa..24f0b0a 100644 --- a/composer.json +++ b/composer.json @@ -98,7 +98,7 @@ } }, "require-dev": { - "phpunit/phpunit": "^9.5", + "phpunit/phpunit": "^9.6", "symfony/browser-kit": "7.0.7", "symfony/css-selector": "7.0.7", "symfony/debug-bundle": "7.0.7", diff --git a/composer.lock b/composer.lock index d8c75dc..3fd7697 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0428f7bdfb99fd81d09175f70a82db43", + "content-hash": "cbf57d864e9d8acd79ac187a999558f9", "packages": [ { "name": "composer/semver", diff --git a/phpunit.xml.dist b/phpunit.xml.dist deleted file mode 100644 index 6c4bfed..0000000 --- a/phpunit.xml.dist +++ /dev/null @@ -1,38 +0,0 @@ - - - - - - - - - - - - - - - - tests - - - - - - src - - - - - - - - - - diff --git a/src/Controller/PostController.php b/src/Controller/PostController.php index 345cda2..5a3c344 100644 --- a/src/Controller/PostController.php +++ b/src/Controller/PostController.php @@ -20,7 +20,7 @@ class PostController extends AbstractController $this->em = $em; } - # DEBUG: Ne doit pas être laissé en production. + # DEBUG : Ne doit pas être laissé en production. #[Route('/', name: 'all post', methods: ['GET'])] public function getAllPost(): Response { diff --git a/src/Entity/.gitignore b/src/Entity/.gitignore deleted file mode 100644 index e69de29..0000000 diff --git a/src/Entity/Post.php b/src/Entity/Post.php index d978eec..cf2524a 100644 --- a/src/Entity/Post.php +++ b/src/Entity/Post.php @@ -200,4 +200,12 @@ class Post return $this; } + + /** + * @param int|null $id + */ + public function setId(?int $id): void + { + $this->id = $id; + } } diff --git a/src/Entity/Profil.php b/src/Entity/Profil.php index 0909a5c..177913f 100644 --- a/src/Entity/Profil.php +++ b/src/Entity/Profil.php @@ -208,7 +208,7 @@ class Profil implements UserInterface, PasswordAuthenticatedUserInterface public function addFollower(self $follower): static { - if (!$this->followers->contains($follower) && $follower!=$this) { + if (!$this->followers->contains($follower) && $follower !== $this) { $this->followers->add($follower); } diff --git a/tests/Controller/PostControllerTest.php b/tests/Controller/PostControllerTest.php new file mode 100644 index 0000000..e9821ba --- /dev/null +++ b/tests/Controller/PostControllerTest.php @@ -0,0 +1,92 @@ +client = static::createClient(); + $this->em = static::getContainer()->get(EntityManagerInterface::class); + } + + public function testGetAllPost() + { + $crawler = $this->client->request('GET', '/'); + $this->assertResponseIsSuccessful(); + + $this->assertSelectorTextContains('h1', 'FukaFukashita'); + } + + + public function testGetPost() + { + $post = new Post(); + $post->setTitle('Test Post'); + $post->setText('This is a test post.'); + $post->setId(66666); + $post->setProfil($this->createUser()); + $post->setDream(true); + $this->em->persist($post); + $this->em->flush(); + + $crawler = $this->client->request('GET', '/post/' . $post->getId()); + + $this->assertResponseIsSuccessful(); + } + + + public function testAddPost() + { + $this->client->loginUser($this->createUser()); + + $crawler = $this->client->request('GET', '/post/new/'); + + $this->assertResponseIsSuccessful(); + + $form = $crawler->selectButton('Submit')->form([ + 'post[title]' => 'New Post', + 'post[text]' => 'Content of the new post', + ]); + + $this->client->submit($form); + + $post = $this->em->getRepository(Post::class)->findOneBy(['title' => 'New Post']); + $this->assertNotNull($post); + } + + public function testRemovePost() + { + $post = new Post(); + $post->setTitle('Post to be deleted'); + $post->setText('This post will be deleted.'); + $post->setProfil($this->createUser()); + $post->setDream(true); + $this->em->persist($post); + $this->em->flush(); + $postId = $post->getId(); + $this->client->request('DELETE', '/post/' . $postId); + $this->assertNull($this->em->getRepository(Post::class)->find($postId)); + } + + + private function createUser(): Profil + { + $user = new Profil(); + $user->setName('testuser'); + $user->setPassword(password_hash('password', PASSWORD_BCRYPT)); + $user->setId(666666); + $this->em->persist($user); + $this->em->flush(); + + return $user; + } +} diff --git a/tests/Entity/CommentaryTest.php b/tests/Entity/CommentaryTest.php new file mode 100644 index 0000000..dce438d --- /dev/null +++ b/tests/Entity/CommentaryTest.php @@ -0,0 +1,44 @@ +assertNull($commentary->getId()); + } + + public function testGetSetText() + { + $commentary = new Commentary(); + $text = 'This is a test commentary.'; + $commentary->setText($text); + + $this->assertSame($text, $commentary->getText()); + } + + public function testGetSetPost() + { + $commentary = new Commentary(); + $post = new Post(); + $commentary->setPost($post); + + $this->assertSame($post, $commentary->getPost()); + } + + public function testGetSetProfil() + { + $commentary = new Commentary(); + $profil = new Profil(); + $commentary->setProfil($profil); + + $this->assertSame($profil, $commentary->getProfil()); + } +} diff --git a/tests/Entity/PostTest.php b/tests/Entity/PostTest.php new file mode 100644 index 0000000..fe05fd1 --- /dev/null +++ b/tests/Entity/PostTest.php @@ -0,0 +1,103 @@ +assertNull($post->getId()); + } + + public function testGetSetTitle() + { + $post = new Post(); + $title = 'Test Title'; + $post->setTitle($title); + + $this->assertSame($title, $post->getTitle()); + } + + public function testGetSetText() + { + $post = new Post(); + $text = 'This is a test post.'; + $post->setText($text); + + $this->assertSame($text, $post->getText()); + } + + public function testGetSetIsDream() + { + $post = new Post(); + $post->setDream(true); + + $this->assertTrue($post->isDream()); + } + + public function testGetSetUpVote() + { + $post = new Post(); + $post->setUpVote(10); + + $this->assertSame(10, $post->getUpVote()); + } + + public function testGetSetDownVote() + { + $post = new Post(); + $post->setDownVote(5); + + $this->assertSame(5, $post->getDownVote()); + } + + public function testGetSetProfil() + { + $post = new Post(); + $profil = new Profil(); + $post->setProfil($profil); + + $this->assertSame($profil, $post->getProfil()); + } + + public function testAddRemoveCommentary() + { + $post = new Post(); + $commentary = new Commentary(); + + $post->addCommentary($commentary); + $this->assertTrue($post->getCommentaries()->contains($commentary)); + + $post->removeCommentary($commentary); + $this->assertFalse($post->getCommentaries()->contains($commentary)); + } + + public function testAddRemoveTag() + { + $post = new Post(); + $tag = new Tags(); + + $post->addTag($tag); + $this->assertTrue($post->getTags()->contains($tag)); + + $post->removeTag($tag); + $this->assertFalse($post->getTags()->contains($tag)); + } + + public function testGetSetCreatedAt() + { + $post = new Post(); + $createdAt = new DateTimeImmutable('now'); + $post->setCreatedAt($createdAt); + + $this->assertSame($createdAt, $post->getCreatedAt()); + } +} diff --git a/tests/Entity/ProfilTest.php b/tests/Entity/ProfilTest.php new file mode 100644 index 0000000..9721fba --- /dev/null +++ b/tests/Entity/ProfilTest.php @@ -0,0 +1,107 @@ +assertNull($profil->getId()); + } + + public function testGetSetName() + { + $profil = new Profil(); + $name = 'TestUser'; + $profil->setName($name); + + $this->assertSame($name, $profil->getName()); + } + + public function testGetSetDescription() + { + $profil = new Profil(); + $description = 'This is a test description.'; + $profil->setDescription($description); + + $this->assertSame($description, $profil->getDescription()); + } + + public function testGetSetPassword() + { + $profil = new Profil(); + $password = 'password123'; + $profil->setPassword($password); + + $this->assertSame($password, $profil->getPassword()); + } + + public function testAddRemovePost() + { + $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 testAddRemoveCommentary() + { + $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 testGetSetRoles() + { + $profil = new Profil(); + $roles = ['ROLE_ADMIN', 'ROLE_USER']; + $profil->setRoles($roles); + + $this->assertSame($roles, $profil->getRoles()); + } + + public function testAddRemoveFollower() + { + $profil1 = new Profil(); + $profil2 = new Profil(); + + $profil1->addFollower($profil2); + print_r($profil1->getFollowing()); + $this->assertTrue($profil1->getFollowers()->contains($profil2)); + $this->assertTrue($profil2->getFollowing()->contains($profil1)); + + $profil1->removeFollower($profil2); + $this->assertFalse($profil1->getFollowers()->contains($profil2)); + $this->assertFalse($profil2->getFollowing()->contains($profil1)); + } + + public function testAddRemoveFollowing() + { + $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)); + } +} +