Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

Commit 11c6ba0

Browse files
committed
[Security] Replace exception mocks with actual exception instances.
It is done for two reasons: * consistency - we use real exception objects in most of the code * latest phpunit does not like the way we were creating mocks for exceptions (it could be also fixed by letting phpunit to call the original constructor)
1 parent efead6d commit 11c6ba0

5 files changed

+17
-13
lines changed

Tests/Core/Authentication/AuthenticationProviderManagerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ protected function getAuthenticationProvider($supports, $token = null, $exceptio
129129
} elseif (null !== $exception) {
130130
$provider->expects($this->once())
131131
->method('authenticate')
132-
->will($this->throwException($this->getMock($exception, null, array(), '', false)))
132+
->will($this->throwException($this->getMock($exception, null, array(), '', true)))
133133
;
134134
}
135135

Tests/Core/Authentication/Provider/DaoAuthenticationProviderTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder;
1515

1616
use Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider;
17+
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
1718

1819
class DaoAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
1920
{
@@ -37,7 +38,7 @@ public function testRetrieveUserWhenUsernameIsNotFound()
3738
$userProvider = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserProviderInterface');
3839
$userProvider->expects($this->once())
3940
->method('loadUserByUsername')
40-
->will($this->throwException($this->getMock('Symfony\\Component\\Security\\Core\\Exception\\UsernameNotFoundException', null, array(), '', false)))
41+
->will($this->throwException(new UsernameNotFoundException()))
4142
;
4243

4344
$provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface'), 'key', $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface'));
@@ -55,7 +56,7 @@ public function testRetrieveUserWhenAnExceptionOccurs()
5556
$userProvider = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserProviderInterface');
5657
$userProvider->expects($this->once())
5758
->method('loadUserByUsername')
58-
->will($this->throwException($this->getMock('RuntimeException', null, array(), '', false)))
59+
->will($this->throwException(new \RuntimeException()))
5960
;
6061

6162
$provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface'), 'key', $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface'));

Tests/Core/Authentication/Provider/PreAuthenticatedAuthenticationProviderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Security\Tests\Core\Authentication\Provider;
1313

1414
use Symfony\Component\Security\Core\Authentication\Provider\PreAuthenticatedAuthenticationProvider;
15+
use Symfony\Component\Security\Core\Exception\LockedException;
1516

1617
class PreAuthenticatedAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
1718
{
@@ -79,7 +80,7 @@ public function testAuthenticateWhenUserCheckerThrowsException()
7980
$userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
8081
$userChecker->expects($this->once())
8182
->method('checkPostAuth')
82-
->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\LockedException', null, array(), '', false)))
83+
->will($this->throwException(new LockedException()))
8384
;
8485

8586
$provider = $this->getProvider($user, $userChecker);

Tests/Core/Authentication/Provider/RememberMeAuthenticationProviderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\Security\Tests\Core\Authentication\Provider;
1313

1414
use Symfony\Component\Security\Core\Authentication\Provider\RememberMeAuthenticationProvider;
15-
use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
15+
use Symfony\Component\Security\Core\Exception\AccountExpiredException;
1616
use Symfony\Component\Security\Core\Role\Role;
1717

1818
class RememberMeAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
@@ -52,7 +52,7 @@ public function testAuthenticateWhenPostChecksFails()
5252
$userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
5353
$userChecker->expects($this->once())
5454
->method('checkPostAuth')
55-
->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\AccountExpiredException', null, array(), '', false)))
55+
->will($this->throwException(new AccountExpiredException()))
5656
;
5757

5858
$provider = $this->getProvider($userChecker);

Tests/Core/Authentication/Provider/UserAuthenticationProviderTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111

1212
namespace Symfony\Component\Security\Tests\Core\Authentication\Provider;
1313

14-
use Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider;
14+
use Symfony\Component\Security\Core\Exception\AccountExpiredException;
15+
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
16+
use Symfony\Component\Security\Core\Exception\CredentialsExpiredException;
17+
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
1518
use Symfony\Component\Security\Core\Role\Role;
1619
use Symfony\Component\Security\Core\Role\SwitchUserRole;
17-
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
1820

1921
class UserAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
2022
{
@@ -41,7 +43,7 @@ public function testAuthenticateWhenUsernameIsNotFound()
4143
$provider = $this->getProvider(false, false);
4244
$provider->expects($this->once())
4345
->method('retrieveUser')
44-
->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\UsernameNotFoundException', null, array(), '', false)))
46+
->will($this->throwException(new UsernameNotFoundException()))
4547
;
4648

4749
$provider->authenticate($this->getSupportedToken());
@@ -55,7 +57,7 @@ public function testAuthenticateWhenUsernameIsNotFoundAndHideIsTrue()
5557
$provider = $this->getProvider(false, true);
5658
$provider->expects($this->once())
5759
->method('retrieveUser')
58-
->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\UsernameNotFoundException', null, array(), '', false)))
60+
->will($this->throwException(new UsernameNotFoundException()))
5961
;
6062

6163
$provider->authenticate($this->getSupportedToken());
@@ -83,7 +85,7 @@ public function testAuthenticateWhenPreChecksFails()
8385
$userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
8486
$userChecker->expects($this->once())
8587
->method('checkPreAuth')
86-
->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\CredentialsExpiredException', null, array(), '', false)))
88+
->will($this->throwException(new CredentialsExpiredException()))
8789
;
8890

8991
$provider = $this->getProvider($userChecker);
@@ -103,7 +105,7 @@ public function testAuthenticateWhenPostChecksFails()
103105
$userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
104106
$userChecker->expects($this->once())
105107
->method('checkPostAuth')
106-
->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\AccountExpiredException', null, array(), '', false)))
108+
->will($this->throwException(new AccountExpiredException()))
107109
;
108110

109111
$provider = $this->getProvider($userChecker);
@@ -128,7 +130,7 @@ public function testAuthenticateWhenPostCheckAuthenticationFails()
128130
;
129131
$provider->expects($this->once())
130132
->method('checkAuthentication')
131-
->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\BadCredentialsException', null, array(), '', false)))
133+
->will($this->throwException(new BadCredentialsException()))
132134
;
133135

134136
$provider->authenticate($this->getSupportedToken());

0 commit comments

Comments
 (0)