Adding phpunit settings and some tests

pull/27/head
Dorian HODIN 10 months ago
parent dd7dc054d2
commit de98fdbb13

@ -98,7 +98,7 @@
} }
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^9.5", "phpunit/phpunit": "^9.6",
"symfony/browser-kit": "7.0.7", "symfony/browser-kit": "7.0.7",
"symfony/css-selector": "7.0.7", "symfony/css-selector": "7.0.7",
"symfony/debug-bundle": "7.0.7", "symfony/debug-bundle": "7.0.7",

2
composer.lock generated

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "0428f7bdfb99fd81d09175f70a82db43", "content-hash": "cbf57d864e9d8acd79ac187a999558f9",
"packages": [ "packages": [
{ {
"name": "composer/semver", "name": "composer/semver",

@ -1,38 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="tests/bootstrap.php"
convertDeprecationsToExceptions="false"
>
<php>
<ini name="display_errors" value="1" />
<ini name="error_reporting" value="-1" />
<server name="APP_ENV" value="test" force="true" />
<server name="SHELL_VERBOSITY" value="-1" />
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
<server name="SYMFONY_PHPUNIT_VERSION" value="9.5" />
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>
<extensions>
</extensions>
</phpunit>

@ -200,4 +200,12 @@ class Post
return $this; return $this;
} }
/**
* @param int|null $id
*/
public function setId(?int $id): void
{
$this->id = $id;
}
} }

@ -208,7 +208,7 @@ class Profil implements UserInterface, PasswordAuthenticatedUserInterface
public function addFollower(self $follower): static public function addFollower(self $follower): static
{ {
if (!$this->followers->contains($follower) && $follower!=$this) { if (!$this->followers->contains($follower) && $follower !== $this) {
$this->followers->add($follower); $this->followers->add($follower);
} }

@ -0,0 +1,92 @@
<?php
namespace App\Tests\Controller;
use App\Entity\Profil;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use App\Entity\Post;
class PostControllerTest extends WebTestCase
{
private KernelBrowser $client;
private mixed $em;
protected function setUp(): void
{
$this->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;
}
}

@ -0,0 +1,44 @@
<?php
namespace App\Tests\Entity;
use App\Entity\Commentary;
use PHPUnit\Framework\TestCase;
use App\Entity\Post;
use App\Entity\Profil;
class CommentaryTest extends TestCase
{
public function testGetId()
{
$commentary = new Commentary();
$this->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());
}
}

@ -0,0 +1,103 @@
<?php
namespace App\Tests\Entity;
use App\Entity\Post;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use App\Entity\Profil;
use App\Entity\Commentary;
use App\Entity\Tags;
class PostTest extends TestCase
{
public function testGetId()
{
$post = new Post();
$this->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());
}
}

@ -0,0 +1,107 @@
<?php
namespace App\Tests\Entity;
use App\Entity\Profil;
use PHPUnit\Framework\TestCase;
use App\Entity\Post;
use App\Entity\Commentary;
class ProfilTest extends TestCase
{
public function testGetId()
{
$profil = new Profil();
$this->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));
}
}
Loading…
Cancel
Save