63 lines
1.9 KiB
PHP
63 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\Controller\Admin;
|
|
|
|
use App\Entity\User;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Doctrine\ORM\Tools\SchemaTool;
|
|
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
|
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
|
|
|
final class AuthControllerTest extends WebTestCase
|
|
{
|
|
private KernelBrowser $client;
|
|
private EntityManagerInterface $entityManager;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
self::ensureKernelShutdown();
|
|
$this->client = self::createClient();
|
|
|
|
$this->entityManager = static::getContainer()->get(EntityManagerInterface::class);
|
|
$metadata = $this->entityManager->getMetadataFactory()->getAllMetadata();
|
|
$schemaTool = new SchemaTool($this->entityManager);
|
|
$schemaTool->dropSchema($metadata);
|
|
$schemaTool->createSchema($metadata);
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
parent::tearDown();
|
|
|
|
$this->entityManager->close();
|
|
}
|
|
|
|
public function testAdminRedirectsToLogin(): void
|
|
{
|
|
$this->client->request('GET', '/admin');
|
|
|
|
self::assertResponseRedirects('/admin/login');
|
|
}
|
|
|
|
public function testAdminCanLogin(): void
|
|
{
|
|
$container = static::getContainer();
|
|
|
|
$user = (new User())->setUsername('admin');
|
|
$hash = $container->get(UserPasswordHasherInterface::class)->hashPassword($user, 'secret-password');
|
|
$user->setPasswordHash($hash);
|
|
|
|
$this->entityManager->persist($user);
|
|
$this->entityManager->flush();
|
|
|
|
$crawler = $this->client->request('GET', '/admin/login');
|
|
$form = $crawler->selectButton('Sign in')->form([
|
|
'login_form[username]' => 'admin',
|
|
'login_form[password]' => 'secret-password',
|
|
]);
|
|
$this->client->submit($form);
|
|
|
|
self::assertResponseRedirects('/admin');
|
|
}
|
|
}
|