Skip to content

Commit

Permalink
[Security] Replace exception mocks with actual exception instances.
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
jakzal committed Apr 1, 2014
1 parent 8aa194c commit 9438f88
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ protected function getAuthenticationProvider($supports, $token = null, $exceptio
} elseif (null !== $exception) {
$provider->expects($this->once())
->method('authenticate')
->will($this->throwException($this->getMock($exception, null, array(), '', false)))
->will($this->throwException($this->getMock($exception, null, array(), '', true)))
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder;

use Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;

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

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

$provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface'), 'key', $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Security\Tests\Core\Authentication\Provider;

use Symfony\Component\Security\Core\Authentication\Provider\PreAuthenticatedAuthenticationProvider;
use Symfony\Component\Security\Core\Exception\LockedException;

class PreAuthenticatedAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -79,7 +80,7 @@ public function testAuthenticateWhenUserCheckerThrowsException()
$userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
$userChecker->expects($this->once())
->method('checkPostAuth')
->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\LockedException', null, array(), '', false)))
->will($this->throwException(new LockedException()))
;

$provider = $this->getProvider($user, $userChecker);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\Security\Tests\Core\Authentication\Provider;

use Symfony\Component\Security\Core\Authentication\Provider\RememberMeAuthenticationProvider;
use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
use Symfony\Component\Security\Core\Exception\AccountExpiredException;
use Symfony\Component\Security\Core\Role\Role;

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

$provider = $this->getProvider($userChecker);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

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

use Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider;
use Symfony\Component\Security\Core\Exception\AccountExpiredException;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\Exception\CredentialsExpiredException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Security\Core\Role\SwitchUserRole;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;

class UserAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -41,7 +43,7 @@ public function testAuthenticateWhenUsernameIsNotFound()
$provider = $this->getProvider(false, false);
$provider->expects($this->once())
->method('retrieveUser')
->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\UsernameNotFoundException', null, array(), '', false)))
->will($this->throwException(new UsernameNotFoundException()))
;

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

$provider->authenticate($this->getSupportedToken());
Expand Down Expand Up @@ -83,7 +85,7 @@ public function testAuthenticateWhenPreChecksFails()
$userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
$userChecker->expects($this->once())
->method('checkPreAuth')
->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\CredentialsExpiredException', null, array(), '', false)))
->will($this->throwException(new CredentialsExpiredException()))
;

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

$provider = $this->getProvider($userChecker);
Expand All @@ -128,7 +130,7 @@ public function testAuthenticateWhenPostCheckAuthenticationFails()
;
$provider->expects($this->once())
->method('checkAuthentication')
->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\BadCredentialsException', null, array(), '', false)))
->will($this->throwException(new BadCredentialsException()))
;

$provider->authenticate($this->getSupportedToken());
Expand Down

0 comments on commit 9438f88

Please sign in to comment.