123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- namespace Symfony\Tests\Component\Security\Authentication\Provider;
- use Symfony\Component\Security\Authentication\Provider\RememberMeAuthenticationProvider;
- use Symfony\Component\Security\Authentication\Token\RememberMeToken;
- use Symfony\Component\Security\Role\Role;
- class RememberMeAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
- {
- public function testSupports()
- {
- $provider = $this->getProvider();
- $this->assertTrue($provider->supports($this->getSupportedToken()));
- $this->assertFalse($provider->supports($this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface')));
- }
- public function testAuthenticateWhenTokenIsNotSupported()
- {
- $provider = $this->getProvider();
- $token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface');
- $this->assertNull($provider->authenticate($token));
- }
- /**
- * @expectedException Symfony\Component\Security\Exception\BadCredentialsException
- */
- public function testAuthenticateWhenKeysDoNotMatch()
- {
- $provider = $this->getProvider(null, 'key1');
- $token = $this->getSupportedToken(null, 'key2');
- $provider->authenticate($token);
- }
- /**
- * @expectedException Symfony\Component\Security\Exception\CredentialsExpiredException
- */
- public function testAuthenticateWhenPreChecksFails()
- {
- $userChecker = $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface');
- $userChecker->expects($this->once())
- ->method('checkPreAuth')
- ->will($this->throwException($this->getMock('Symfony\Component\Security\Exception\CredentialsExpiredException', null, array(), '', false)))
- ;
- $provider = $this->getProvider($userChecker);
- $provider->authenticate($this->getSupportedToken());
- }
- /**
- * @expectedException Symfony\Component\Security\Exception\AccountExpiredException
- */
- public function testAuthenticateWhenPostChecksFails()
- {
- $userChecker = $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface');
- $userChecker->expects($this->once())
- ->method('checkPostAuth')
- ->will($this->throwException($this->getMock('Symfony\Component\Security\Exception\AccountExpiredException', null, array(), '', false)))
- ;
- $provider = $this->getProvider($userChecker);
- $provider->authenticate($this->getSupportedToken());
- }
- public function testAuthenticate()
- {
- $user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
- $user->expects($this->once())
- ->method('getRoles')
- ->will($this->returnValue(array('ROLE_FOO')))
- ;
- $provider = $this->getProvider();
- $token = $this->getSupportedToken($user);
- $token
- ->expects($this->once())
- ->method('getCredentials')
- ->will($this->returnValue('foo'))
- ;
- $authToken = $provider->authenticate($token);
- $this->assertInstanceOf('Symfony\Component\Security\Authentication\Token\RememberMeToken', $authToken);
- $this->assertSame($user, $authToken->getUser());
- $this->assertEquals(array(new Role('ROLE_FOO')), $authToken->getRoles());
- $this->assertEquals('foo', $authToken->getCredentials());
- }
- protected function getSupportedToken($user = null, $key = 'test')
- {
- if (null === $user) {
- $user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
- $user
- ->expects($this->any())
- ->method('getRoles')
- ->will($this->returnValue(array()))
- ;
- }
- $token = $this->getMock('Symfony\Component\Security\Authentication\Token\RememberMeToken', array('getCredentials', 'getProviderKey'), array($user, 'foo', $key));
- $token
- ->expects($this->once())
- ->method('getProviderKey')
- ->will($this->returnValue('foo'))
- ;
- return $token;
- }
- protected function getProvider($userChecker = null, $key = 'test')
- {
- if (null === $userChecker) {
- $userChecker = $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface');
- }
- return new RememberMeAuthenticationProvider($userChecker, $key, 'foo');
- }
- }
|