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

158 lines
4.7 KiB
PHP

<?php
namespace App\Tests\Application\Controller\Api;
use App\Entity\Restaurant;
use App\Entity\Subject;
use App\Factory\RestaurantFactory;
use App\Factory\UserFactory;
use App\Repository\SubjectRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class SubjectControllerTest extends WebTestCase
{
public function testList(): void
{
$client = static::createClient();
$user = UserFactory::createOne();
$client->loginUser($user);
RestaurantFactory::createMany(5, [
'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']);
$subject = $data['payload']['subjects'][0];
$this->assertNotNull($subject['latitude']);
$this->assertNotNull($subject['longitude']);
}
public function testCreate(): void
{
$client = static::createClient();
$repo = static::getContainer()->get(SubjectRepository::class);
$user = UserFactory::createOne();
$client->loginUser($user);
$this->assertEmpty($repo->findAll());
$faker = \Zenstruck\Foundry\faker();
$name = $faker->company();
$latitude = $faker->latitude();
$longitude = $faker->longitude();
$client->request(
'POST',
'/api/subjects',
server: ['CONTENT_TYPE' => 'application/json'],
content: json_encode([
'name' => $name,
'latitude' => $latitude,
'longitude' => $longitude,
]),
);
$this->assertResponseIsSuccessful();
$this->assertResponseStatusCodeSame(201);
$this->assertCount(1, $repo->findAll());
$subject = static::getContainer()
->get(SubjectRepository::class)
->findOneBy(['name' => $name]);
$this->assertNotNull($subject);
$this->assertInstanceOf(Restaurant::class, $subject);
$this->assertSame($user->getId(), $subject->getCreatedBy()->getId());
$this->assertEqualsWithDelta((float) $latitude, (float) $subject->getLatitude(), 0.0000001);
$this->assertEqualsWithDelta((float) $longitude, (float) $subject->getLongitude(), 0.0000001);
}
public function testUpdate(): void
{
$client = static::createClient();
$user = UserFactory::createOne();
$client->loginUser($user);
$nameOld = 'Old name';
$nameNew = 'New name';
$latitudeOld = '15.005';
$latitudeNew = '51.66';
$longitudeOld = '15.040';
$longitudeNew = '51.54';
$subject = RestaurantFactory::createOne([
'name' => $nameOld,
'createdBy' => $user,
'latitude' => $latitudeOld,
'longitude' => $longitudeOld,
]);
$client->request(
'PATCH',
'/api/subjects/' . $subject->getId(),
server: ['CONTENT_TYPE' => 'application/json'],
content: json_encode([
'name' => $nameNew,
'latitude' => $latitudeNew,
'longitude' => $longitudeNew,
]),
);
$this->assertResponseIsSuccessful();
$this->assertResponseStatusCodeSame(202);
$em = static::getContainer()->get(EntityManagerInterface::class);
$em->clear();
$updated = $em->getRepository(Subject::class)->find($subject->getId());
$this->assertSame($nameNew, $updated->getName());
$this->assertEqualsWithDelta((float) $latitudeNew, (float) $subject->getLatitude(), 0.0000001);
$this->assertEqualsWithDelta((float) $longitudeNew, (float) $subject->getLongitude(), 0.0000001);
}
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)
);
}
}