Skip to content

Commit fd76519

Browse files
committed
unit tests
1 parent 7fd5a35 commit fd76519

File tree

15 files changed

+176
-6
lines changed

15 files changed

+176
-6
lines changed

src/Security/SecurityControllerBuilder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,6 @@ public function addLogoutMethod(ClassSourceManipulator $manipulator)
6363
CODE
6464
);
6565
$manipulator->addMethodBuilder($loginMethodBuilder);
66+
$manipulator->addUseStatementIfNecessary('Symfony\\Component\\Routing\\Annotation\\Route');
6667
}
6768
}

tests/Security/InteractiveSecurityHelperTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,37 @@ public function getEntryPointTests()
120120
'main',
121121
];
122122
}
123+
124+
/**
125+
* @dataProvider getUserClassTests
126+
*/
127+
public function testGuessUserClass(array $securityData, string $expectedUserClass, bool $userClassAutomaticallyGuessed)
128+
{
129+
/** @var SymfonyStyle|\PHPUnit_Framework_MockObject_MockObject $io */
130+
$io = $this->createMock(SymfonyStyle::class);
131+
$io->expects($this->exactly(true === $userClassAutomaticallyGuessed ? 0 : 1))
132+
->method('ask')
133+
->willReturn($expectedUserClass);
134+
135+
$helper = new InteractiveSecurityHelper();
136+
$this->assertEquals(
137+
$expectedUserClass,
138+
$helper->guessUserClass($io, $securityData)
139+
);
140+
}
141+
142+
public function getUserClassTests()
143+
{
144+
yield 'user_from_provider' => [
145+
['security' => ['providers' => ['app_provider' => ['entity' => ['class' => 'App\\Entity\\User']]]]],
146+
'App\\Entity\\User',
147+
true,
148+
];
149+
150+
yield 'user_asked_user' => [
151+
['security' => ['providers' => []]],
152+
'App\\Entity\\User',
153+
false,
154+
];
155+
}
123156
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\MakerBundle\Tests\Security;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Bundle\MakerBundle\Security\SecurityControllerBuilder;
7+
use Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator;
8+
9+
class SecurityControllerBuilderTest extends TestCase
10+
{
11+
public function testAddLoginMethod()
12+
{
13+
$source = file_get_contents(__DIR__.'/fixtures/source/SecurityController.php');
14+
$expectedSource = file_get_contents(__DIR__.'/fixtures/expected/SecurityController_login.php');
15+
16+
$manipulator = new ClassSourceManipulator($source);
17+
18+
$securityControllerBuilder = new SecurityControllerBuilder();
19+
$securityControllerBuilder->addLoginMethod($manipulator);
20+
21+
$this->assertSame($expectedSource, $manipulator->getSourceCode());
22+
}
23+
public function testAddLogoutMethod()
24+
{
25+
$source = file_get_contents(__DIR__.'/fixtures/source/SecurityController.php');
26+
$expectedSource = file_get_contents(__DIR__.'/fixtures/expected/SecurityController_logout.php');
27+
28+
$manipulator = new ClassSourceManipulator($source);
29+
30+
$securityControllerBuilder = new SecurityControllerBuilder();
31+
$securityControllerBuilder->addLogoutMethod($manipulator);
32+
33+
$this->assertSame($expectedSource, $manipulator->getSourceCode());
34+
}
35+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App\Controller;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6+
use Symfony\Component\HttpFoundation\Response;
7+
use Symfony\Component\Routing\Annotation\Route;
8+
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
9+
10+
class SecurityController extends AbstractController
11+
{
12+
/**
13+
* @Route("/login", name="app_login")
14+
*/
15+
public function login(AuthenticationUtils $authenticationUtils): Response
16+
{
17+
// get the login error if there is one
18+
$error = $authenticationUtils->getLastAuthenticationError();
19+
// last username entered by the user
20+
$lastUsername = $authenticationUtils->getLastUsername();
21+
22+
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
23+
}
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Controller;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6+
use Symfony\Component\Routing\Annotation\Route;
7+
8+
class SecurityController extends AbstractController
9+
{
10+
/**
11+
* @Route("/logout", name="app_logout")
12+
*/
13+
public function logout()
14+
{
15+
throw new \Exception('will be intercepted before getting here');
16+
}
17+
}
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/Util/ClassSourceManipulatorTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,4 +542,27 @@ public function testAddInterface()
542542

543543
$this->assertSame($expectedSource, $manipulator->getSourceCode());
544544
}
545+
546+
public function testAddMethodWithBody()
547+
{
548+
$source = file_get_contents(__DIR__.'/fixtures/source/EmptyController.php');
549+
$expectedSource = file_get_contents(__DIR__.'/fixtures/add_method/Controller_with_action.php');
550+
551+
$manipulator = new ClassSourceManipulator($source);
552+
553+
$methodBuilder = $manipulator->createMethodBuilder('action', 'JsonResponse', false, ['@Route("/action", name="app_action")']);
554+
$methodBuilder->addParam(
555+
(new \PhpParser\Builder\Param('param'))->setTypeHint('string')
556+
);
557+
$manipulator->addMethodBody($methodBuilder, <<<'CODE'
558+
<?php
559+
return new JsonResponse(['param' => $param]);
560+
CODE
561+
);
562+
$manipulator->addMethodBuilder($methodBuilder);
563+
$manipulator->addUseStatementIfNecessary('Symfony\\Component\\HttpFoundation\\JsonResponse');
564+
$manipulator->addUseStatementIfNecessary('Symfony\\Component\\Routing\\Annotation\\Route');
565+
566+
$this->assertSame($expectedSource, $manipulator->getSourceCode());
567+
}
545568
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Controller;
4+
5+
use Symfony\Component\HttpFoundation\JsonResponse;
6+
use Symfony\Component\Routing\Annotation\Route;
7+
8+
class EmptyController
9+
{
10+
/**
11+
* @Route("/action", name="app_action")
12+
*/
13+
public function action(string $param): JsonResponse
14+
{
15+
return new JsonResponse(['param' => $param]);
16+
}
17+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace App\Controller;
4+
5+
class EmptyController
6+
{
7+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
security:
22
encoders:
33
App\Security\User:
4-
algorithm: argon2i
4+
algorithm: bcrypt
55

66
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
77
providers:

0 commit comments

Comments
 (0)