rater/tests/Application/Controller/Api/RatingControllerTest.php

153 lines
4.1 KiB
PHP

<?php
namespace App\Tests\Unit\Controller\Api;
use App\Entity\Rating;
use App\Entity\Restaurant;
use App\Factory\RatingFactory;
use App\Factory\RestaurantFactory;
use App\Factory\UserFactory;
use App\Repository\RatingRepository;
use App\Repository\SubjectRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class RatingControllerTest extends WebTestCase
{
public function testList(): void
{
$client = static::createClient();
$user = UserFactory::createOne();
$client->loginUser($user);
$subject = RestaurantFactory::createOne([
'createdBy' => $user,
]);
$ratings = RatingFactory::createMany(5, [
'subject' => $subject,
]);
$client->request(
'GET',
'/api/ratings',
server: ['CONTENT_TYPE' => 'application/json'],
content: json_encode(['name' => 'Riviera']),
);
$data = json_decode($client->getResponse()->getContent(), true);
$this->assertResponseIsSuccessful();
$this->assertCount(5, $data['payload']['ratings']);
}
public function testCreate(): void
{
$client = static::createClient();
$repo = static::getContainer()->get(RatingRepository::class);
$user = UserFactory::createOne();
$client->loginUser($user);
$subject = RestaurantFactory::createOne([
'createdBy' => $user,
]);
$client->request(
'POST',
'/api/ratings',
server: ['CONTENT_TYPE' => 'application/json'],
content: json_encode([
'subject_id' => $subject->getId(),
'score' => 8,
]),
);
$this->assertResponseIsSuccessful();
$this->assertResponseStatusCodeSame(201);
$this->assertCount(1, $repo->findAll());
$ratings = static::getContainer()
->get(RatingRepository::class)
->findAllBySubject($subject);
$this->assertNotNull($ratings);
$this->assertCount(1, $ratings);
$this->assertInstanceOf(Rating::class, $ratings[0]);
}
public function testUpdate(): void
{
$client = static::createClient();
$user = UserFactory::createOne();
$client->loginUser($user);
$subject = RestaurantFactory::createOne([
'createdBy' => $user,
]);
$rating = RatingFactory::createOne([
'subject' => $subject,
'score' => 0,
'note' => 'first note',
]);
$client->request(
'PATCH',
'/api/ratings/' . $rating->getId(),
server: ['CONTENT_TYPE' => 'application/json'],
content: json_encode([
'score' => 8,
'note' => 'Other note',
]),
);
$this->assertResponseIsSuccessful();
$this->assertResponseStatusCodeSame(202);
$em = static::getContainer()->get(EntityManagerInterface::class);
$em->clear();
$updated = $em->getRepository(Rating::class)->find($rating->getId());
$this->assertSame(8, $updated->getScore());
$this->assertSame('Other note', $updated->getNote());
}
public function testDelete(): void
{
$client = static::createClient();
$user = UserFactory::createOne();
$client->loginUser($user);
$subject = RestaurantFactory::createOne([
'createdBy' => $user,
]);
$rating = RatingFactory::createOne([
'subject' => $subject,
'score' => 0,
'note' => 'first note',
]);
$id = $rating->getId();
$client->request(
'DELETE',
'/api/ratings/' . $id,
server: ['CONTENT_TYPE' => 'application/json'],
);
$this->assertResponseIsSuccessful();
$this->assertResponseStatusCodeSame(204);
$em = static::getContainer()->get(EntityManagerInterface::class);
$em->clear();
$this->assertNull(
$em->getRepository(Rating::class)->find($id)
);
}
}