WIP tests unitaires

cleo
Cleo EIRAS 3 days ago
parent 3406dc14f4
commit f727fc819d

@ -6,6 +6,10 @@
parameters:
services:
App\Repository\RarityRepository:
public: true
App\Repository\EmojiRepository:
public: true
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.

@ -33,6 +33,13 @@ class EmojiController extends AbstractController
]);
}
#[Route('/count', name: 'count')]
public function count(EmojiRepository $emojiRepository): Response
{
$count = count($emojiRepository->findAll());
return new Response(['count' => $count]);
}
#[Route('/add/{code}', name: 'add')]
public function addEmojiDebug(string $code, EntityManagerInterface $entityManager) {
$emoji = new Emoji();

@ -0,0 +1,89 @@
<?php
namespace App\Tests\Controller;
use App\Entity\Emoji;
use App\Entity\Rarity;
use App\Repository\EmojiRepository;
use App\Repository\RarityRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;
class EmojiControllerTest extends WebTestCase
{
private $client;
private EntityManagerInterface $em;
protected function setUp(): void
{
$this->client = static::createClient();
$this->em = static::getContainer()->get(EntityManagerInterface::class);
// Démarre une transaction pour pouvoir annuler les modifications des tests
$this->em->getConnection()->beginTransaction();
}
protected function tearDown(): void
{
// Rollback la transaction pour annuler les changements des tests
$this->em->getConnection()->rollBack();
parent::tearDown();
}
public function testReproduceEmoji(): void
{
$emoji1 = (new Emoji())
->setCode('😀')
->setName('Parent1')
->setStrength(1.0)
->setToughness(1.0)
->setIntelligence(1.0)
->setSpeed(1.0)
->setFightsWon(5);
$emoji2 = (new Emoji())
->setCode('😎')
->setName('Parent2')
->setStrength(2.0)
->setToughness(2.0)
->setIntelligence(2.0)
->setSpeed(2.0)
->setFightsWon(3);
$rarity = (new Rarity())
->setName('Rare')
->setDropRate(1.0);
$this->em->persist($rarity);
$this->em->persist($emoji1);
$this->em->persist($emoji2);
$this->em->flush();
$id1 = $emoji1->getId();
$id2 = $emoji2->getId();
$this->client->request('GET', "/emoji/fusion/$id1/$id2");
$response = $this->client->getResponse();
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
$data = json_decode($response->getContent(), true);
$this->assertArrayHasKey('childId', $data);
$this->assertEquals('Child created', $data['message']);
}
public function testFusionEmojiNotFound(): void
{
$emojiRepo = $this->createMock(EmojiRepository::class);
$emojiRepo->method('find')->willReturn(null);
$this->client->request('GET', '/emoji/fusion/999/998');
$response = $this->client->getResponse();
$this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode());
$data = json_decode($response->getContent(), true);
$this->assertEquals('One or both emojis not found', $data['error']);
}
}
Loading…
Cancel
Save