2026-06-03 23:49:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
2026-06-04 21:46:16 +02:00
|
|
|
namespace App\Tests\Unit\Controller\Api;
|
2026-06-03 23:49:30 +02:00
|
|
|
|
2026-06-05 00:00:51 +02:00
|
|
|
use App\Entity\Rating;
|
2026-06-03 23:49:30 +02:00
|
|
|
use App\Entity\Restaurant;
|
2026-06-05 00:00:51 +02:00
|
|
|
use App\Factory\RatingFactory;
|
|
|
|
|
use App\Factory\RestaurantFactory;
|
2026-06-03 23:49:30 +02:00
|
|
|
use App\Factory\UserFactory;
|
2026-06-05 00:00:51 +02:00
|
|
|
use App\Repository\RatingRepository;
|
2026-06-03 23:49:30 +02:00
|
|
|
use App\Repository\SubjectRepository;
|
2026-06-05 00:20:33 +02:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2026-06-03 23:49:30 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
|
|
|
|
|
2026-06-05 00:00:51 +02:00
|
|
|
class RatingControllerTest extends WebTestCase
|
2026-06-03 23:49:30 +02:00
|
|
|
{
|
|
|
|
|
public function testList(): void
|
|
|
|
|
{
|
|
|
|
|
$client = static::createClient();
|
|
|
|
|
|
|
|
|
|
$user = UserFactory::createOne();
|
|
|
|
|
$client->loginUser($user);
|
|
|
|
|
|
2026-06-05 00:00:51 +02:00
|
|
|
$subject = RestaurantFactory::createOne([
|
2026-06-03 23:49:30 +02:00
|
|
|
'createdBy' => $user,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-06-05 00:00:51 +02:00
|
|
|
$ratings = RatingFactory::createMany(5, [
|
|
|
|
|
'subject' => $subject,
|
|
|
|
|
]);
|
2026-06-03 23:49:30 +02:00
|
|
|
|
|
|
|
|
$client->request(
|
|
|
|
|
'GET',
|
2026-06-05 00:00:51 +02:00
|
|
|
'/api/ratings',
|
2026-06-03 23:49:30 +02:00
|
|
|
server: ['CONTENT_TYPE' => 'application/json'],
|
|
|
|
|
content: json_encode(['name' => 'Riviera']),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$data = json_decode($client->getResponse()->getContent(), true);
|
2026-06-05 00:00:51 +02:00
|
|
|
|
2026-06-03 23:49:30 +02:00
|
|
|
$this->assertResponseIsSuccessful();
|
2026-06-05 00:00:51 +02:00
|
|
|
$this->assertCount(5, $data['payload']['ratings']);
|
2026-06-03 23:49:30 +02:00
|
|
|
}
|
|
|
|
|
|
2026-06-05 00:00:51 +02:00
|
|
|
public function testCreate(): void
|
2026-06-03 23:49:30 +02:00
|
|
|
{
|
|
|
|
|
$client = static::createClient();
|
|
|
|
|
|
2026-06-05 00:00:51 +02:00
|
|
|
$repo = static::getContainer()->get(RatingRepository::class);
|
2026-06-03 23:49:30 +02:00
|
|
|
|
|
|
|
|
$user = UserFactory::createOne();
|
|
|
|
|
$client->loginUser($user);
|
|
|
|
|
|
2026-06-05 00:00:51 +02:00
|
|
|
$subject = RestaurantFactory::createOne([
|
|
|
|
|
'createdBy' => $user,
|
|
|
|
|
]);
|
2026-06-03 23:49:30 +02:00
|
|
|
|
|
|
|
|
$client->request(
|
|
|
|
|
'POST',
|
2026-06-05 00:00:51 +02:00
|
|
|
'/api/ratings',
|
2026-06-03 23:49:30 +02:00
|
|
|
server: ['CONTENT_TYPE' => 'application/json'],
|
2026-06-05 00:00:51 +02:00
|
|
|
content: json_encode([
|
|
|
|
|
'subject_id' => $subject->getId(),
|
|
|
|
|
'score' => 8,
|
|
|
|
|
]),
|
2026-06-03 23:49:30 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertResponseIsSuccessful();
|
|
|
|
|
$this->assertResponseStatusCodeSame(201);
|
|
|
|
|
$this->assertCount(1, $repo->findAll());
|
|
|
|
|
|
2026-06-05 00:00:51 +02:00
|
|
|
$ratings = static::getContainer()
|
|
|
|
|
->get(RatingRepository::class)
|
|
|
|
|
->findAllBySubject($subject);
|
2026-06-03 23:49:30 +02:00
|
|
|
|
2026-06-05 00:00:51 +02:00
|
|
|
$this->assertNotNull($ratings);
|
|
|
|
|
$this->assertCount(1, $ratings);
|
|
|
|
|
$this->assertInstanceOf(Rating::class, $ratings[0]);
|
2026-06-03 23:49:30 +02:00
|
|
|
}
|
2026-06-05 00:20:33 +02:00
|
|
|
|
|
|
|
|
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(201);
|
|
|
|
|
|
|
|
|
|
$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());
|
|
|
|
|
}
|
2026-06-03 23:49:30 +02:00
|
|
|
}
|