Select Git revision
BasePageTest.php

Sebastien authored
BasePageTest.php 1.73 KiB
<?php
namespace App\Tests\acceptance;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
abstract class BasePageTest extends WebTestCase
{
protected KernelBrowser $client;
protected ?User $user;
protected function givenUser(string $userName): BasePageTest
{
$object_manager = $this->client->getContainer()
->get('doctrine')
->getManager();
$user_repository = $object_manager->getRepository(User::class);
$user = $user_repository
->findOneBy(['email' => $userName ]);
if (! $user instanceof User) {
throw new \Exception("User does not exist.");
}
$this->user = $user;
return $this;
}
protected function onPage(string $uri): BasePageTest
{
$this->client->request('GET', $uri);
$this->assertResponseIsSuccessful();
return $this;
}
protected function clickLink(string $linkText, ?string $errorMessageText): BasePageTest
{
$this->client->clickLink($linkText);
$this->assertResponseIsSuccessful($errorMessageText ?? '');
return $this;
}
/**
* @throws \Exception the user is not initialized
*/
protected function logged(): BasePageTest
{
if (null === $this->user) {
throw new \Exception("User is not initialized.");
}
$this->client->loginUser($this->user);
return $this;
}
protected function setUp(): void
{
self::ensureKernelShutdown();
$this->client = static::createClient();
}
protected function tearDown(): void
{
parent::tearDown();
self::ensureKernelShutdown();
}
}