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

80 lines
2.1 KiB
PHP
Raw Normal View History

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;
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
}
}