Skip to content

Commit 7fd5a35

Browse files
committed
Add tests into fixtures files
1 parent 02ff624 commit 7fd5a35

File tree

9 files changed

+118
-14
lines changed

9 files changed

+118
-14
lines changed

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

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

33
{% block title %}Login!{% endblock %}
44

5-
{% block stylesheets %}
6-
{{ parent() }}
7-
8-
<link rel="stylesheet" href="{{ asset('css/login.css') }}">
9-
{% endblock %}
10-
115
{% block body %}
126
<form class="form-signin" method="post">
137
{% if error %}

tests/Maker/FunctionalTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,10 @@ function (string $output, string $directory) {
410410
)
411411
->addExtraDependencies('doctrine')
412412
->addExtraDependencies('security')
413+
->addExtraDependencies('twig')
413414
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeAuthenticatorLoginFormUserEntity')
415+
->configureDatabase()
416+
->updateSchemaAfterCommand()
414417
->assert(
415418
function (string $output, string $directory) {
416419
$this->assertContains('Success', $output);
@@ -438,6 +441,8 @@ function (string $output, string $directory) {
438441
]
439442
)
440443
->addExtraDependencies('security')
444+
->addExtraDependencies('twig')
445+
->addExtraDependencies('sensio/framework-extra-bundle annot')
441446
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeAuthenticatorLoginFormUserNotEntity')
442447
->assert(
443448
function (string $output) {
@@ -460,7 +465,10 @@ function (string $output) {
460465
)
461466
->addExtraDependencies('doctrine')
462467
->addExtraDependencies('security')
468+
->addExtraDependencies('twig')
463469
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeAuthenticatorLoginFormExistingController')
470+
->configureDatabase()
471+
->updateSchemaAfterCommand()
464472
->assert(
465473
function (string $output) {
466474
$this->assertContains('Success', $output);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Tests;
4+
5+
use App\Controller\SecurityController;
6+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
7+
8+
class SecurityControllerTest extends WebTestCase
9+
{
10+
public function testCommand()
11+
{
12+
$this->assertTrue(method_exists(SecurityController::class, 'login'));
13+
$this->assertTrue(method_exists(SecurityController::class, 'logout'));
14+
15+
$client = self::createClient();
16+
$crawler = $client->request('GET', '/login');
17+
18+
$this->assertEquals(200, $client->getResponse()->getStatusCode());
19+
20+
$form = $crawler->filter('form')->form();
21+
$client->submit($form);
22+
23+
$this->assertEquals(200, $client->getResponse()->getStatusCode());
24+
$this->assertContains($client->getResponse()->getContent(), 'Invalid credentials');
25+
}
26+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
security:
22
encoders:
3-
App\Security\User:
3+
App\Entity\User:
44
algorithm: argon2i
55

66
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Tests;
4+
5+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6+
7+
class SecurityControllerTest extends WebTestCase
8+
{
9+
public function testCommand()
10+
{
11+
$client = self::createClient();
12+
$crawler = $client->request('GET', '/login');
13+
14+
$this->assertEquals(200, $client->getResponse()->getStatusCode());
15+
16+
$form = $crawler->filter('form')->form();
17+
$client->submit($form);
18+
19+
$this->assertEquals(200, $client->getResponse()->getStatusCode());
20+
$this->assertContains($client->getResponse()->getContent(), 'Invalid credentials');
21+
}
22+
}

tests/fixtures/MakeAuthenticatorLoginFormUserNotEntity/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: argon2i
3+
App\Security\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/MakeAuthenticatorLoginFormUserNotEntity/src/Security/UserProvider.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@ class UserProvider implements UserProviderInterface
2020
*/
2121
public function loadUserByUsername($username)
2222
{
23-
// Load a User object from your data source or throw UsernameNotFoundException.
24-
// The $username argument may not actually be a username:
25-
// it is whatever value is being returned by the getUsername()
26-
// method in your User class.
27-
throw new \Exception('TODO: fill in loadUserByUsername() inside '.__FILE__);
23+
return (new User())
24+
->setEmail($username)
25+
->setPassword($username);
2826
}
2927

3028
/**
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace App\Tests;
4+
5+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6+
7+
class SecurityControllerTest extends WebTestCase
8+
{
9+
public function testCommand()
10+
{
11+
$client = self::createClient();
12+
$crawler = $client->request('GET', '/login');
13+
14+
$this->assertEquals(200, $client->getResponse()->getStatusCode());
15+
16+
$form = $crawler->filter('form')->form();
17+
$form->setValues(
18+
[
19+
'email' => 'bar',
20+
'password' => 'foo',
21+
]
22+
);
23+
$client->submit($form);
24+
25+
$this->assertEquals(302, $client->getResponse()->getStatusCode());
26+
$client->followRedirect();
27+
$this->assertContains('Invalid credentials.', $client->getResponse()->getContent());
28+
$form->setValues(
29+
[
30+
'email' => '[email protected]',
31+
'password' => '[email protected]',
32+
]
33+
);
34+
$client->submit($form);
35+
36+
$this->assertEquals(302, $client->getResponse()->getStatusCode());
37+
$this->assertContains('/home', $client->getResponse()->getContent());
38+
}
39+
}

0 commit comments

Comments
 (0)