Files
archived-maker-bundle/tests/Maker/MakeAuthenticatorTest.php
2026-02-12 13:13:20 +01:00

396 lines
15 KiB
PHP

<?php
/*
* This file is part of the Symfony MakerBundle package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\MakerBundle\Tests\Maker;
use PHPUnit\Framework\Attributes\Group;
use Symfony\Bundle\MakerBundle\Maker\MakeAuthenticator;
use Symfony\Bundle\MakerBundle\Test\MakerTestCase;
use Symfony\Bundle\MakerBundle\Test\MakerTestRunner;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
/**
* @group legacy
*/
#[Group('legacy')]
class MakeAuthenticatorTest extends MakerTestCase
{
protected function getMakerClass(): string
{
return MakeAuthenticator::class;
}
public static function getTestDetails(): \Generator
{
yield 'auth_empty_one_firewall' => [self::buildMakerTest()
->run(static function (MakerTestRunner $runner) {
$output = $runner->runMaker([
// authenticator type => empty-auth
0,
// authenticator class name
'AppCustomAuthenticator',
]);
self::assertStringContainsString('Success', $output);
self::assertFileExists($runner->getPath('src/Security/AppCustomAuthenticator.php'));
$securityConfig = $runner->readYaml('config/packages/security.yaml');
self::assertEquals(
'App\\Security\\AppCustomAuthenticator',
$securityConfig['security']['firewalls']['main']['custom_authenticator']
);
}),
];
yield 'auth_empty_multiple_firewalls' => [self::buildMakerTest()
->run(static function (MakerTestRunner $runner) {
$runner->modifyYamlFile('config/packages/security.yaml', static function (array $config) {
$config['security']['firewalls']['second']['lazy'] = true;
return $config;
});
$output = $runner->runMaker([
// authenticator type => empty-auth
0,
// class name
'AppCustomAuthenticator',
// firewall name (1 will be the "second" firewall)
1,
]);
self::assertStringContainsString('Success', $output);
$securityConfig = $runner->readYaml('config/packages/security.yaml');
self::assertEquals(
'App\\Security\\AppCustomAuthenticator',
$securityConfig['security']['firewalls']['second']['custom_authenticator']
);
}),
];
yield 'auth_empty_existing_authenticator' => [self::buildMakerTest()
->run(static function (MakerTestRunner $runner) {
$runner->copy(
'make-auth/BlankAuthenticator.php',
'src/Security/BlankAuthenticator.php'
);
$runner->modifyYamlFile('config/packages/security.yaml', static function (array $config) {
$config['security']['firewalls']['main']['custom_authenticator'] = 'App\Security\BlankAuthenticator';
return $config;
});
$output = $runner->runMaker([
// authenticator type => empty-auth
0,
// class name
'AppCustomAuthenticator',
// firewall name
1,
]);
self::assertStringContainsString('Success', $output);
$securityConfig = $runner->readYaml('config/packages/security.yaml');
self::assertEquals(
'App\\Security\\AppCustomAuthenticator',
$securityConfig['security']['firewalls']['main']['custom_authenticator'][1]
);
}),
];
yield 'auth_empty_multiple_firewalls_existing_authenticator' => [self::buildMakerTest()
->run(static function (MakerTestRunner $runner) {
$runner->copy(
'make-auth/BlankAuthenticator.php',
'src/Security/BlankAuthenticator.php'
);
$runner->modifyYamlFile('config/packages/security.yaml', static function (array $config) {
$config['security']['firewalls']['second'] = ['lazy' => true, 'custom_authenticator' => 'App\Security\BlankAuthenticator'];
return $config;
});
$output = $runner->runMaker([
// authenticator type => empty-auth
0,
// class name
'AppCustomAuthenticator',
// firewall name
1,
// entry point
1,
]);
self::assertStringContainsString('Success', $output);
$securityConfig = $runner->readYaml('config/packages/security.yaml');
self::assertEquals(
'App\\Security\\AppCustomAuthenticator',
$securityConfig['security']['firewalls']['second']['custom_authenticator'][1]
);
}),
];
yield 'auth_login_form_user_entity_with_hasher' => [self::buildMakerTest()
->addExtraDependencies('doctrine', 'twig', 'symfony/form')
->run(static function (MakerTestRunner $runner) {
self::makeUser($runner, 'userEmail');
$output = $runner->runMaker([
// authenticator type => login-form
1,
// class name
'AppCustomAuthenticator',
// controller name
'SecurityController',
// field name
'userEmail',
'no',
// remember me support => no
'no',
]);
self::runLoginTest($runner, 'userEmail');
self::assertStringContainsString('Success', $output);
self::assertFileExists($runner->getPath('src/Controller/SecurityController.php'));
self::assertFileExists($runner->getPath('templates/security/login.html.twig'));
self::assertFileExists($runner->getPath('src/Security/AppCustomAuthenticator.php'));
}),
];
yield 'auth_login_form_no_entity_custom_username_field' => [self::buildMakerTest()
->addExtraDependencies('twig', 'symfony/form')
->run(static function (MakerTestRunner $runner) {
self::makeUser($runner, 'userEmail', false);
$runner->runMaker([
// authenticator type => login-form
1,
// class name
'AppCustomAuthenticator',
// controller name
'SecurityController',
// user class
'App\\Security\\User',
// username field => userEmail
0,
'no',
// remember me support => no
'no',
]);
$runner->runTests();
self::runLoginTest(
$runner,
'userEmail',
false,
'App\\Security\\User'
);
}),
];
yield 'auth_login_form_user_not_entity_with_hasher' => [self::buildMakerTest()
->addExtraDependencies('twig', 'symfony/form')
->run(static function (MakerTestRunner $runner) {
self::makeUser($runner, 'email', false);
$runner->runMaker([
// authenticator type => login-form
1,
// class name
'AppCustomAuthenticator',
// controller name
'SecurityController',
// user class
'App\Security\User',
'no',
// remember me support => no
'no',
]);
}),
];
yield 'auth_login_form_existing_controller' => [self::buildMakerTest()
->addExtraDependencies('doctrine', 'twig', 'symfony/form')
->run(static function (MakerTestRunner $runner) {
self::makeUser($runner, 'email');
$runner->copy(
'make-auth/SecurityController-empty.php',
'src/Controller/SecurityController.php'
);
$runner->runMaker([
// authenticator type => login-form
1,
// class name
'AppCustomAuthenticator',
// controller name
'SecurityController',
'no',
// remember me support => no
'no',
]);
self::runLoginTest($runner, 'email');
}),
];
yield 'auth_login_form_user_entity_with_logout' => [self::buildMakerTest()
->addExtraDependencies('doctrine', 'twig', 'symfony/form')
->run(static function (MakerTestRunner $runner) {
self::makeUser($runner, 'userEmail');
$output = $runner->runMaker([
// authenticator type => login-form
1,
// class name
'AppCustomAuthenticator',
// controller name
'SecurityController',
// logout support
'yes',
// remember me support => no
'no',
]);
self::runLoginTest($runner, 'userEmail', true, 'App\\Entity\\User', true);
self::assertStringContainsString('Success', $output);
self::assertFileExists($runner->getPath('src/Controller/SecurityController.php'));
self::assertFileExists($runner->getPath('templates/security/login.html.twig'));
self::assertFileExists($runner->getPath('src/Security/AppCustomAuthenticator.php'));
$securityConfig = $runner->readYaml('config/packages/security.yaml');
self::assertEquals(
'app_logout',
$securityConfig['security']['firewalls']['main']['logout']['path']
);
}),
];
yield 'auth_login_form_remember_me_via_checkbox' => [self::buildMakerTest()
->addExtraDependencies('doctrine', 'twig', 'symfony/form')
->run(static function (MakerTestRunner $runner) {
self::makeUser($runner, 'userEmail');
$output = $runner->runMaker([
// authenticator type => login-form
1,
// class name
'AppCustomAuthenticator',
// controller name
'SecurityController',
// logout support
'yes',
// remember me support => yes
'yes',
// remember me type => checkbox
0,
]);
self::runLoginTest($runner, 'userEmail');
self::assertStringContainsString('Success', $output);
$seucrityConfig = $runner->readYaml('config/packages/security.yaml');
$firewallMain = $seucrityConfig['security']['firewalls']['main'];
self::assertEquals('%kernel.secret%', $firewallMain['remember_me']['secret']);
self::assertEquals('604800', $firewallMain['remember_me']['lifetime']);
self::assertArrayNotHasKey('always_remember_me', $firewallMain['remember_me']);
}),
];
yield 'auth_login_form_always_remember_me' => [self::buildMakerTest()
->addExtraDependencies('doctrine', 'twig', 'symfony/form')
->run(static function (MakerTestRunner $runner) {
self::makeUser($runner, 'userEmail');
$output = $runner->runMaker([
// authenticator type => login-form
1,
// class name
'AppCustomAuthenticator',
// controller name
'SecurityController',
// logout support
'yes',
// remember me support => yes
'yes',
// remember me type => always
1,
]);
self::runLoginTest($runner, 'userEmail');
self::assertStringContainsString('Success', $output);
$seucrityConfig = $runner->readYaml('config/packages/security.yaml');
$firewallMain = $seucrityConfig['security']['firewalls']['main'];
self::assertEquals('%kernel.secret%', $firewallMain['remember_me']['secret']);
self::assertTrue($firewallMain['remember_me']['always_remember_me']);
}),
];
}
private static function runLoginTest(MakerTestRunner $runner, string $userIdentifier, bool $isEntity = true, string $userClass = 'App\\Entity\\User', bool $testLogin = false): void
{
$runner->renderTemplateFile(
'make-auth/LoginFlowTest.php.twig',
'tests/LoginFlowTest.php',
[
'userIdentifier' => $userIdentifier,
'isEntity' => $isEntity,
'userClass' => $userClass,
'testLogin' => $testLogin,
]
);
// plaintext password: needed for entities, simplifies overall
$runner->modifyYamlFile('config/packages/security.yaml', static function (array $config) {
if (isset($config['when@test']['security']['password_hashers'])) {
$config['when@test']['security']['password_hashers'] = [PasswordAuthenticatedUserInterface::class => 'plaintext'];
return $config;
}
return $config;
});
if ($isEntity) {
$runner->configureDatabase();
}
$runner->runTests();
}
private static function makeUser(MakerTestRunner $runner, string $userIdentifier, bool $isEntity = true): void
{
$runner->runConsole('make:user', [
'User', // class name
$isEntity ? 'y' : 'n', // entity
$userIdentifier, // identifier
'y', // password
]);
if (!$isEntity) {
$runner->copy(
'make-auth/UserProvider-no-entity.php',
'src/Security/UserProvider.php'
);
}
}
}