2026-06-03 23:49:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
2026-06-05 14:09:34 +02:00
|
|
|
namespace App\Tests\Application\Controller\Api;
|
2026-06-03 23:49:30 +02:00
|
|
|
|
|
|
|
|
use App\Entity\Restaurant;
|
2026-06-05 13:57:21 +02:00
|
|
|
use App\Entity\Subject;
|
|
|
|
|
use App\Factory\RestaurantFactory;
|
2026-06-03 23:49:30 +02:00
|
|
|
use App\Factory\UserFactory;
|
|
|
|
|
use App\Repository\SubjectRepository;
|
2026-06-05 13:57:21 +02:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2026-06-03 23:49:30 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
|
|
|
|
|
|
|
|
|
class SubjectControllerTest extends WebTestCase
|
|
|
|
|
{
|
|
|
|
|
public function testList(): void
|
|
|
|
|
{
|
|
|
|
|
$client = static::createClient();
|
|
|
|
|
|
|
|
|
|
$user = UserFactory::createOne();
|
|
|
|
|
$client->loginUser($user);
|
|
|
|
|
|
2026-06-05 15:24:19 +02:00
|
|
|
RestaurantFactory::createMany(5, [
|
2026-06-03 23:49:30 +02:00
|
|
|
'createdBy' => $user,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$client->request(
|
|
|
|
|
'GET',
|
|
|
|
|
'/api/subjects',
|
|
|
|
|
server: ['CONTENT_TYPE' => 'application/json'],
|
|
|
|
|
content: json_encode(['name' => 'Riviera']),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$data = json_decode($client->getResponse()->getContent(), true);
|
|
|
|
|
$this->assertResponseIsSuccessful();
|
|
|
|
|
$this->assertCount(5, $data['payload']['subjects']);
|
2026-06-05 19:46:57 +02:00
|
|
|
|
|
|
|
|
$subject = $data['payload']['subjects'][0];
|
|
|
|
|
$this->assertNotNull($subject['latitude']);
|
|
|
|
|
$this->assertNotNull($subject['longitude']);
|
2026-06-03 23:49:30 +02:00
|
|
|
}
|
|
|
|
|
|
2026-06-05 13:57:21 +02:00
|
|
|
public function testCreate(): void
|
2026-06-03 23:49:30 +02:00
|
|
|
{
|
|
|
|
|
$client = static::createClient();
|
|
|
|
|
|
|
|
|
|
$repo = static::getContainer()->get(SubjectRepository::class);
|
|
|
|
|
|
|
|
|
|
$user = UserFactory::createOne();
|
|
|
|
|
$client->loginUser($user);
|
|
|
|
|
|
|
|
|
|
$this->assertEmpty($repo->findAll());
|
|
|
|
|
|
2026-06-05 15:24:19 +02:00
|
|
|
$faker = \Zenstruck\Foundry\faker();
|
|
|
|
|
$name = $faker->company();
|
|
|
|
|
$latitude = $faker->latitude();
|
|
|
|
|
$longitude = $faker->longitude();
|
|
|
|
|
|
2026-06-03 23:49:30 +02:00
|
|
|
$client->request(
|
|
|
|
|
'POST',
|
|
|
|
|
'/api/subjects',
|
|
|
|
|
server: ['CONTENT_TYPE' => 'application/json'],
|
2026-06-05 15:24:19 +02:00
|
|
|
content: json_encode([
|
|
|
|
|
'name' => $name,
|
|
|
|
|
'latitude' => $latitude,
|
|
|
|
|
'longitude' => $longitude,
|
|
|
|
|
]),
|
2026-06-03 23:49:30 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertResponseIsSuccessful();
|
|
|
|
|
$this->assertResponseStatusCodeSame(201);
|
|
|
|
|
$this->assertCount(1, $repo->findAll());
|
|
|
|
|
|
|
|
|
|
$subject = static::getContainer()
|
|
|
|
|
->get(SubjectRepository::class)
|
2026-06-05 15:24:19 +02:00
|
|
|
->findOneBy(['name' => $name]);
|
2026-06-03 23:49:30 +02:00
|
|
|
|
|
|
|
|
$this->assertNotNull($subject);
|
|
|
|
|
$this->assertInstanceOf(Restaurant::class, $subject);
|
|
|
|
|
$this->assertSame($user->getId(), $subject->getCreatedBy()->getId());
|
2026-06-05 15:24:19 +02:00
|
|
|
$this->assertEqualsWithDelta((float) $latitude, (float) $subject->getLatitude(), 0.0000001);
|
|
|
|
|
$this->assertEqualsWithDelta((float) $longitude, (float) $subject->getLongitude(), 0.0000001);
|
2026-06-03 23:49:30 +02:00
|
|
|
}
|
2026-06-05 13:57:21 +02:00
|
|
|
|
|
|
|
|
public function testUpdate(): void
|
|
|
|
|
{
|
|
|
|
|
$client = static::createClient();
|
|
|
|
|
|
|
|
|
|
$user = UserFactory::createOne();
|
|
|
|
|
$client->loginUser($user);
|
|
|
|
|
|
2026-06-05 15:24:19 +02:00
|
|
|
$nameOld = 'Old name';
|
|
|
|
|
$nameNew = 'New name';
|
|
|
|
|
$latitudeOld = '15.005';
|
|
|
|
|
$latitudeNew = '51.66';
|
|
|
|
|
$longitudeOld = '15.040';
|
|
|
|
|
$longitudeNew = '51.54';
|
|
|
|
|
|
2026-06-05 13:57:21 +02:00
|
|
|
$subject = RestaurantFactory::createOne([
|
2026-06-05 15:24:19 +02:00
|
|
|
'name' => $nameOld,
|
2026-06-05 13:57:21 +02:00
|
|
|
'createdBy' => $user,
|
2026-06-05 15:24:19 +02:00
|
|
|
'latitude' => $latitudeOld,
|
|
|
|
|
'longitude' => $longitudeOld,
|
2026-06-05 13:57:21 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$client->request(
|
|
|
|
|
'PATCH',
|
|
|
|
|
'/api/subjects/' . $subject->getId(),
|
|
|
|
|
server: ['CONTENT_TYPE' => 'application/json'],
|
|
|
|
|
content: json_encode([
|
2026-06-05 15:24:19 +02:00
|
|
|
'name' => $nameNew,
|
|
|
|
|
'latitude' => $latitudeNew,
|
|
|
|
|
'longitude' => $longitudeNew,
|
2026-06-05 13:57:21 +02:00
|
|
|
]),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertResponseIsSuccessful();
|
|
|
|
|
$this->assertResponseStatusCodeSame(202);
|
|
|
|
|
|
|
|
|
|
$em = static::getContainer()->get(EntityManagerInterface::class);
|
|
|
|
|
$em->clear();
|
|
|
|
|
$updated = $em->getRepository(Subject::class)->find($subject->getId());
|
|
|
|
|
|
2026-06-05 15:24:19 +02:00
|
|
|
$this->assertSame($nameNew, $updated->getName());
|
|
|
|
|
$this->assertEqualsWithDelta((float) $latitudeNew, (float) $subject->getLatitude(), 0.0000001);
|
|
|
|
|
$this->assertEqualsWithDelta((float) $longitudeNew, (float) $subject->getLongitude(), 0.0000001);
|
2026-06-05 13:57:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testDelete(): void
|
|
|
|
|
{
|
|
|
|
|
$client = static::createClient();
|
|
|
|
|
|
|
|
|
|
$user = UserFactory::createOne();
|
|
|
|
|
$client->loginUser($user);
|
|
|
|
|
|
|
|
|
|
$subject = RestaurantFactory::createOne([
|
|
|
|
|
'name' => 'My Restaurant',
|
|
|
|
|
'createdBy' => $user,
|
|
|
|
|
]);
|
|
|
|
|
$id = $subject->getId();
|
|
|
|
|
|
|
|
|
|
$client->request(
|
|
|
|
|
'DELETE',
|
|
|
|
|
'/api/subjects/' . $id,
|
|
|
|
|
server: ['CONTENT_TYPE' => 'application/json'],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertResponseIsSuccessful();
|
|
|
|
|
$this->assertResponseStatusCodeSame(204);
|
|
|
|
|
|
|
|
|
|
$em = static::getContainer()->get(EntityManagerInterface::class);
|
|
|
|
|
$em->clear();
|
|
|
|
|
|
|
|
|
|
$this->assertNull(
|
|
|
|
|
$em->getRepository(Subject::class)->find($id)
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-06-03 23:49:30 +02:00
|
|
|
}
|