10000 [Security] added userChecker to SimpleAuthenticationProvider · symfony/symfony@d068bd3 · GitHub
[go: up one dir, main page]

Skip to content

Commit d068bd3

Browse files
author
Boris Vujicic
committed
[Security] added userChecker to SimpleAuthenticationProvider
test userChecker
1 parent 3cc426e commit d068bd3

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

src/Symfony/Component/Security/Core/Authentication/Provider/SimpleAuthenticationProvider.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ public function authenticate(TokenInterface $token)
4444
throw new AuthenticationException('Simple authenticator failed to return an authenticated token.');
4545
}
4646

47-
$this->userChecker->checkPreAuth($authToken->getUser());
48-
$this->userChecker->checkPostAuth($authToken->getUser());
47+
$user = $authToken->getUser();
48+
$this->userChecker->checkPreAuth($user);
49+
$this->userChecker->checkPostAuth($user);
4950

5051
return $authToken;
5152
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Core\Tests\Authentication\Provider;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Security\Core\Exception\DisabledException;
16+
use Symfony\Component\Security\Core\Authentication\Provider\SimpleAuthenticationProvider;
17+
18+
class SimpleAuthenticationProviderTest extends TestCase
19+
{
20+
21+
/**
22+
* @expectedException \Symfony\Component\Security\Core\Exception\DisabledException
23+
*/
24+
public function testAuthenticateWhenPreChecksFails()
25+
{
26+
$userChecker = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserCheckerInterface')->getMock();
27+
$userChecker->expects($this->once())
28+
->method('checkPreAuth')
29+
->will($this->throwException(new DisabledException()));
30+
31+
$provider = $this->getProvider($userChecker);
32+
33+
$token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
34+
$provider->authenticate($token);
35+
}
36+
37+
public function testAuthenticate()
38+
{
39+
$user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
40+
$user->expects($this->exactly(2))
41+
->method('getRoles')
42+
->will($this->returnValue(array('ROLE_FOO')));
43+
$provider = $this->getProvider();
44+
45+
$token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
46+
$authToken = $provider->authenticate($token);
47+
48+
$this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\TokenInterface', $authToken);
49+
$this->assertSame($user, $authToken->getUser());
50+
$this->assertEquals(array(new Role('ROLE_FOO')), $authToken->getRoles());
51+
$this->assertEquals('', $authToken->getCredentials());
52+
}
53+
54+
protected function getProvider($userChecker = null, $key = 'test')
55+
{
56+
if (null === $userChecker) {
57+
$userChecker = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserCheckerInterface')->getMock();
58+
}
59+
60+
return new SimpleAuthenticationProvider($userChecker, $key, 'foo');
61+
}
62+
}

0 commit comments

Comments
 (0)
0