Skip to content

Commit 02ff624

Browse files
committed
Functional tests
1 parent f96c51e commit 02ff624

File tree

15 files changed

+419
-15
lines changed

15 files changed

+419
-15
lines changed

src/Maker/MakeAuthenticator.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
106106
function ($answer) {
107107
Validator::notBlank($answer);
108108

109-
return Validator::validateClassDoesNotExist(
109+
return Validator::classDoesNotExist(
110110
$this->generator->createClassNameDetails(
111111
$answer,
112112
'Security\\'
@@ -149,7 +149,12 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
149149
self::AUTH_TYPE_FORM_LOGIN === $input->getArgument('authenticator-type') ?
150150
($this->doctrineHelper->isClassAMappedEntity($input->getArgument('user-class')) ? 'authenticator/LoginFormEntityAuthenticator.tpl.php' : 'authenticator/LoginFormNotEntityAuthenticator.tpl.php')
151151
: 'authenticator/Empty.tpl.php',
152-
[]
152+
$input->hasArgument('user-class') ?
153+
[
154+
'user_fully_qualified_class_name' => $input->getArgument('user-class'),
155+
'user_class_name' => substr(strrchr($input->getArgument('user-class'), '\\'), 1),
156+
]
157+
: []
153158
);
154159

155160
// update security.yaml with guard config

src/Resources/skeleton/authenticator/LoginFormEntityAuthenticator.tpl.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace <?= $namespace ?>;
44

5-
use App\Entity\User;
5+
use <?= $user_fully_qualified_class_name ?>;
66
use Doctrine\ORM\EntityManagerInterface;
77
use Symfony\Component\HttpFoundation\RedirectResponse;
88
use Symfony\Component\HttpFoundation\Request;
@@ -62,7 +62,7 @@ public function getUser($credentials, UserProviderInterface $userProvider)
6262
throw new InvalidCsrfTokenException();
6363
}
6464

65-
return $this->entityManager->getRepository(User::class)->findOneBy(['email' => $credentials['email']]);
65+
return $this->entityManager->getRepository(<?= $user_class_name ?>::class)->findOneBy(['email' => $credentials['email']]);
6666
}
6767

6868
public function checkCredentials($credentials, UserInterface $user)

src/Resources/skeleton/authenticator/LoginFormNotEntityAuthenticator.tpl.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace <?= $namespace ?>;
44

5-
use App\Entity\User;
65
use Symfony\Component\HttpFoundation\RedirectResponse;
76
use Symfony\Component\HttpFoundation\Request;
87
use Symfony\Component\Routing\RouterInterface;

src/Resources/skeleton/authenticator/SecurityController.tpl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\Controller;
44

5-
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
5+
use Symfony\Bundle\FrameworkBundle\Controller\<?= $parent_class_name; ?>;
66
use Symfony\Component\Routing\Annotation\Route;
77
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
88

src/Security/InteractiveSecurityHelper.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,8 @@ public static function guessUserClass(SymfonyStyle $io, array $securityData): st
8989
(It has to be handled by one of the firewall\'s providers)',
9090
class_exists('App\\Entity\\User') && isset(class_implements('App\\Entity\\User')[UserInterface::class]) ? 'App\\Entity\\User'
9191
: class_exists('App\\Security\\User') && isset(class_implements('App\\Security\\User')[UserInterface::class]) ? 'App\\Security\\User' : null,
92-
[Validator::class, 'classExists']
92+
[Validator::class, 'classIsUserInterface']
9393
);
94-
95-
if (!isset(class_implements($userClass)[UserInterface::class])) {
96-
throw new RuntimeCommandException(sprintf('The class "%s" doesn\'t implement "%s"', $userClass, UserInterface::class));
97-
}
9894
}
9995

10096
return $userClass;

src/Validator.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Doctrine\Common\Persistence\ManagerRegistry;
1515
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
16+
use Symfony\Component\Security\Core\User\UserInterface;
1617

1718
/**
1819
* @author Javier Eguiluz <[email protected]>
@@ -185,7 +186,7 @@ public static function entityExists(string $className = null, array $entities =
185186
return $className;
186187
}
187188

188-
public static function validateClassDoesNotExist($className)
189+
public static function classDoesNotExist($className)
189190
{
190191
self::notBlank($className);
191192

@@ -195,4 +196,15 @@ public static function validateClassDoesNotExist($className)
195196

196197
return $className;
197198
}
199+
200+
public static function classIsUserInterface($userClassName)
201+
{
202+
self::classExists($userClassName);
203+
204+
if (!isset(class_implements($userClassName)[UserInterface::class])) {
205+
throw new RuntimeCommandException(sprintf('The class "%s" doesn\'t implement "%s"', $userClassName, UserInterface::class));
206+
}
207+
208+
return $userClassName;
209+
}
198210
}

tests/Maker/FunctionalTest.php

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ function (string $output, string $directory) {
396396
),
397397
];
398398

399-
yield 'auth_login_form' => [
399+
yield 'auth_login_form_user_entity' => [
400400
MakerTestDetails::createTest(
401401
$this->getMakerInstance(MakeAuthenticator::class),
402402
[
@@ -410,7 +410,62 @@ function (string $output, string $directory) {
410410
)
411411
->addExtraDependencies('doctrine')
412412
->addExtraDependencies('security')
413-
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeAuthenticatorLoginForm'),
413+
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeAuthenticatorLoginFormUserEntity')
414+
->assert(
415+
function (string $output, string $directory) {
416+
$this->assertContains('Success', $output);
417+
418+
$fs = new Filesystem();
419+
$this->assertTrue($fs->exists(sprintf('%s/src/Controller/SecurityController.php', $directory)));
420+
$this->assertTrue($fs->exists(sprintf('%s/templates/security/login.html.twig', $directory)));
421+
$this->assertTrue($fs->exists(sprintf('%s/src/Security/AppCustomAuthenticator.php', $directory)));
422+
}
423+
),
424+
];
425+
426+
yield 'auth_login_form_user_not_entity' => [
427+
MakerTestDetails::createTest(
428+
$this->getMakerInstance(MakeAuthenticator::class),
429+
[
430+
// authenticator type => login-form
431+
1,
432+
// class name
433+
'AppCustomAuthenticator',
434+
// controller name
435+
'SecurityController',
436+
// user class
437+
'App\Security\User'
438+
]
439+
)
440+
->addExtraDependencies('security')
441+
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeAuthenticatorLoginFormUserNotEntity')
442+
->assert(
443+
function (string $output) {
444+
$this->assertContains('Success', $output);
445+
}
446+
),
447+
];
448+
449+
yield 'auth_login_form_existing_controller' => [
450+
MakerTestDetails::createTest(
451+
$this->getMakerInstance(MakeAuthenticator::class),
452+
[
453+
// authenticator type => login-form
454+
1,
455+
// class name
456+
'AppCustomAuthenticator',
457+
// controller name
458+
'SecurityController'
459+
]
460+
)
461+
->addExtraDependencies('doctrine')
462+
->addExtraDependencies('security')
463+
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeAuthenticatorLoginFormExistingController')
464+
->assert(
465+
function (string $output) {
466+
$this->assertContains('Success', $output);
467+
}
468+
),
414469
];
415470

416471
yield 'user_security_entity_with_password' => [MakerTestDetails::createTest(
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace App\Controller;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6+
7+
class SecurityController extends AbstractController
8+
{
9+
}

tests/fixtures/MakeAuthenticatorLoginForm/src/Entity/User.php renamed to tests/fixtures/MakeAuthenticatorLoginFormExistingController/src/Entity/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Symfony\Component\Security\Core\User\UserInterface;
77

88
/**
9-
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
9+
* @ORM\Entity()
1010
*/
1111
class User implements UserInterface
1212
{

0 commit comments

Comments
 (0)