Skip to content

Commit 9ec9766

Browse files
committed
Test logging process with users as entity
1 parent fd76519 commit 9ec9766

File tree

7 files changed

+136
-37
lines changed

7 files changed

+136
-37
lines changed

tests/Maker/FunctionalTest.php

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\Bundle\MakerBundle\Maker\MakeFixtures;
1717
use Symfony\Bundle\MakerBundle\Maker\MakeForm;
1818
use Symfony\Bundle\MakerBundle\Maker\MakeFunctionalTest;
19-
use Symfony\Bundle\MakerBundle\Maker\MakeLoginForm;
2019
use Symfony\Bundle\MakerBundle\Maker\MakeMigration;
2120
use Symfony\Bundle\MakerBundle\Maker\MakeSerializerEncoder;
2221
use Symfony\Bundle\MakerBundle\Maker\MakeSubscriber;
@@ -396,34 +395,40 @@ function (string $output, string $directory) {
396395
),
397396
];
398397

399-
yield 'auth_login_form_user_entity' => [
400-
MakerTestDetails::createTest(
401-
$this->getMakerInstance(MakeAuthenticator::class),
402-
[
403-
// authenticator type => login-form
404-
1,
405-
// class name
406-
'AppCustomAuthenticator',
407-
// controller name
408-
'SecurityController'
409-
]
410-
)
411-
->addExtraDependencies('doctrine')
412-
->addExtraDependencies('security')
413-
->addExtraDependencies('twig')
414-
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeAuthenticatorLoginFormUserEntity')
415-
->configureDatabase()
416-
->updateSchemaAfterCommand()
417-
->assert(
418-
function (string $output, string $directory) {
419-
$this->assertContains('Success', $output);
398+
$makerTestAuthenticatorLoginFormUserEntity = MakerTestDetails::createTest(
399+
$this->getMakerInstance(MakeAuthenticator::class),
400+
[
401+
// authenticator type => login-form
402+
1,
403+
// class name
404+
'AppCustomAuthenticator',
405+
// controller name
406+
'SecurityController',
407+
]
408+
)
409+
->addExtraDependencies('doctrine')
410+
->addExtraDependencies('security')
411+
->addExtraDependencies('twig')
412+
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeAuthenticatorLoginFormUserEntity')
413+
->configureDatabase()
414+
->updateSchemaAfterCommand()
415+
->assert(
416+
function (string $output, string $directory) {
417+
$this->assertContains('Success', $output);
418+
419+
$fs = new Filesystem();
420+
$this->assertTrue($fs->exists(sprintf('%s/src/Controller/SecurityController.php', $directory)));
421+
$this->assertTrue($fs->exists(sprintf('%s/templates/security/login.html.twig', $directory)));
422+
$this->assertTrue($fs->exists(sprintf('%s/src/Security/AppCustomAuthenticator.php', $directory)));
423+
}
424+
);
425+
426+
if (Kernel::VERSION_ID < 40100) {
427+
$makerTestAuthenticatorLoginFormUserEntity->addExtraDependencies('symfony/form');
428+
}
420429

421-
$fs = new Filesystem();
422-
$this->assertTrue($fs->exists(sprintf('%s/src/Controller/SecurityController.php', $directory)));
423-
$this->assertTrue($fs->exists(sprintf('%s/templates/security/login.html.twig', $directory)));
424-
$this->assertTrue($fs->exists(sprintf('%s/src/Security/AppCustomAuthenticator.php', $directory)));
425-
}
426-
),
430+
yield 'auth_login_form_user_entity' => [
431+
$makerTestAuthenticatorLoginFormUserEntity,
427432
];
428433

429434
yield 'auth_login_form_user_not_entity' => [
@@ -442,7 +447,8 @@ function (string $output, string $directory) {
442447
)
443448
->addExtraDependencies('security')
444449
->addExtraDependencies('twig')
445-
->addExtraDependencies('sensio/framework-extra-bundle annot')
450+
->addExtraDependencies('doctrine/annotations')
451+
->addExtraDependencies('symfony/form')
446452
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeAuthenticatorLoginFormUserNotEntity')
447453
->assert(
448454
function (string $output) {
@@ -466,6 +472,7 @@ function (string $output) {
466472
->addExtraDependencies('doctrine')
467473
->addExtraDependencies('security')
468474
->addExtraDependencies('twig')
475+
->addExtraDependencies('symfony/form')
469476
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeAuthenticatorLoginFormExistingController')
470477
->configureDatabase()
471478
->updateSchemaAfterCommand()

tests/fixtures/MakeAuthenticatorLoginFormExistingController/config/packages/security.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
security:
22
encoders:
3-
App\Security\User:
4-
algorithm: bcrypt
3+
App\Entity\User: plaintext
54

65
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
76
providers:
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Controller;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6+
use Symfony\Component\Routing\Annotation\Route;
7+
use Symfony\Component\HttpFoundation\Response;
8+
9+
class DefaultController extends AbstractController
10+
{
11+
/**
12+
* @Route("/home", name="app_homepage")
13+
*/
14+
public function homepage()
15+
{
16+
return new Response();
17+
}
18+
}

tests/fixtures/MakeAuthenticatorLoginFormExistingController/tests/SecurityControllerTest.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace App\Tests;
44

55
use App\Controller\SecurityController;
6+
use App\Entity\User;
7+
use Doctrine\ORM\EntityManagerInterface;
68
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
79

810
class SecurityControllerTest extends WebTestCase
@@ -12,17 +14,44 @@ public function testCommand()
1214
$this->assertTrue(method_exists(SecurityController::class, 'login'));
1315
$this->assertTrue(method_exists(SecurityController::class, 'logout'));
1416

15-
$client = self::createClient();
17+
$client = self::createClient();
1618
$crawler = $client->request('GET', '/login');
1719

1820
$this->assertEquals(200, $client->getResponse()->getStatusCode());
1921

22+
/** @var EntityManagerInterface $em */
23+
$em = self::$kernel->getContainer()
24+
->get('doctrine')
25+
->getManager();
26+
27+
$user = (new User())->setEmail('[email protected]')
28+
->setPassword('password');
29+
$em->persist($user);
30+
$em->flush();
31+
2032
$form = $crawler->filter('form')->form();
33+
$form->setValues(
34+
[
35+
'email' => '[email protected]',
36+
'password' => 'foo',
37+
]
38+
);
2139
$client->submit($form);
2240

2341
$this->assertEquals(302, $client->getResponse()->getStatusCode());
2442
$client->followRedirect();
2543
$this->assertEquals(200, $client->getResponse()->getStatusCode());
26-
$this->assertContains('Username could not be found.', $client->getResponse()->getContent());
44+
$this->assertContains('Invalid credentials.', $client->getResponse()->getContent());
45+
46+
$form->setValues(
47+
[
48+
'email' => '[email protected]',
49+
'password' => 'password',
50+
]
51+
);
52+
$client->submit($form);
53+
54+
$this->assertEquals(302, $client->getResponse()->getStatusCode());
55+
$this->assertContains('/home', $client->getResponse()->getContent());
2756
}
2857
}

tests/fixtures/MakeAuthenticatorLoginFormUserEntity/config/packages/security.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
security:
22
encoders:
3-
App\Entity\User:
4-
algorithm: bcrypt
3+
App\Entity\User: plaintext
54

65
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
76
providers:
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Controller;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6+
use Symfony\Component\Routing\Annotation\Route;
7+
use Symfony\Component\HttpFoundation\Response;
8+
9+
class DefaultController extends AbstractController
10+
{
11+
/**
12+
* @Route("/home", name="app_homepage")
13+
*/
14+
public function homepage()
15+
{
16+
return new Response();
17+
}
18+
}

tests/fixtures/MakeAuthenticatorLoginFormUserEntity/tests/SecurityControllerTest.php

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

33
namespace App\Tests;
44

5+
use App\Entity\User;
6+
use Doctrine\ORM\EntityManagerInterface;
57
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
68

79
class SecurityControllerTest extends WebTestCase
810
{
911
public function testCommand()
1012
{
11-
$client = self::createClient();
13+
$client = self::createClient();
1214
$crawler = $client->request('GET', '/login');
1315

1416
$this->assertEquals(200, $client->getResponse()->getStatusCode());
1517

18+
/** @var EntityManagerInterface $em */
19+
$em = self::$kernel->getContainer()
20+
->get('doctrine')
21+
->getManager();
22+
23+
$user = (new User())->setEmail('[email protected]')
24+
->setPassword('password');
25+
$em->persist($user);
26+
$em->flush();
27+
1628
$form = $crawler->filter('form')->form();
29+
$form->setValues(
30+
[
31+
'email' => '[email protected]',
32+
'password' => 'foo',
33+
]
34+
);
1735
$client->submit($form);
1836

1937
$this->assertEquals(302, $client->getResponse()->getStatusCode());
2038
$client->followRedirect();
2139
$this->assertEquals(200, $client->getResponse()->getStatusCode());
22-
$this->assertContains('Username could not be found.', $client->getResponse()->getContent());
40+
$this->assertContains('Invalid credentials.', $client->getResponse()->getContent());
41+
42+
$form->setValues(
43+
[
44+
'email' => '[email protected]',
45+
'password' => 'password',
46+
]
47+
);
48+
$client->submit($form);
49+
50+
$this->assertEquals(302, $client->getResponse()->getStatusCode());
51+
$this->assertContains('/home', $client->getResponse()->getContent());
2352
}
2453
}

0 commit comments

Comments
 (0)