133 lines
3.7 KiB
PHP
133 lines
3.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\SubjectFactory;
|
|
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);
|
|
|
|
SubjectFactory::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']);
|
|
}
|
|
|
|
public function testCreate(): void
|
|
{
|
|
$client = static::createClient();
|
|
|
|
$repo = static::getContainer()->get(SubjectRepository::class);
|
|
|
|
$user = UserFactory::createOne();
|
|
$client->loginUser($user);
|
|
|
|
$this->assertEmpty($repo->findAll());
|
|
|
|
$client->request(
|
|
'POST',
|
|
'/api/subjects',
|
|
server: ['CONTENT_TYPE' => 'application/json'],
|
|
content: json_encode(['name' => 'Riviera']),
|
|
);
|
|
|
|
$this->assertResponseIsSuccessful();
|
|
$this->assertResponseStatusCodeSame(201);
|
|
$this->assertCount(1, $repo->findAll());
|
|
|
|
$subject = static::getContainer()
|
|
->get(SubjectRepository::class)
|
|
->findOneBy(['name' => 'Riviera']);
|
|
|
|
$this->assertNotNull($subject);
|
|
$this->assertInstanceOf(Restaurant::class, $subject);
|
|
$this->assertNotNull($subject->getCreatedBy());
|
|
$this->assertSame($user->getId(), $subject->getCreatedBy()->getId());
|
|
}
|
|
|
|
public function testUpdate(): void
|
|
{
|
|
$client = static::createClient();
|
|
|
|
$user = UserFactory::createOne();
|
|
$client->loginUser($user);
|
|
|
|
$subject = RestaurantFactory::createOne([
|
|
'name' => 'Old name',
|
|
'createdBy' => $user,
|
|
]);
|
|
|
|
$name = 'New name';
|
|
|
|
$client->request(
|
|
'PATCH',
|
|
'/api/subjects/' . $subject->getId(),
|
|
server: ['CONTENT_TYPE' => 'application/json'],
|
|
content: json_encode([
|
|
'name' => $name,
|
|
]),
|
|
);
|
|
|
|
$this->assertResponseIsSuccessful();
|
|
$this->assertResponseStatusCodeSame(202);
|
|
|
|
$em = static::getContainer()->get(EntityManagerInterface::class);
|
|
$em->clear();
|
|
$updated = $em->getRepository(Subject::class)->find($subject->getId());
|
|
|
|
$this->assertSame($name, $updated->getName());
|
|
}
|
|
|
|
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)
|
|
);
|
|
}
|
|
}
|