71 lines
2 KiB
PHP
71 lines
2 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\Unit\Controller\Api;
|
|
|
|
use App\Entity\Restaurant;
|
|
use App\Factory\SubjectFactory;
|
|
use App\Factory\UserFactory;
|
|
use App\Repository\SubjectRepository;
|
|
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,
|
|
]);
|
|
|
|
static::getContainer()
|
|
->get(SubjectRepository::class)
|
|
->findOneBy(['name' => 'Riviera']);
|
|
|
|
$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 testAddingNewSubjectViaApi(): 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());
|
|
}
|
|
}
|