Файл: symfony-2.7/src/Symfony/Component/Security/Http/Tests/Firewall/RememberMeListenerTest.php
Строк: 406
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SymfonyComponentSecurityHttpTestsFirewall;
use SymfonyComponentSecurityCoreExceptionAuthenticationException;
use SymfonyComponentSecurityHttpFirewallRememberMeListener;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentSecurityHttpSecurityEvents;
class RememberMeListenerTest extends PHPUnit_Framework_TestCase
{
public function testOnCoreSecurityDoesNotTryToPopulateNonEmptyTokenStorage()
{
list($listener, $tokenStorage, , , ,) = $this->getListener();
$tokenStorage
->expects($this->once())
->method('getToken')
->will($this->returnValue($this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface')))
;
$tokenStorage
->expects($this->never())
->method('setToken')
;
$this->assertNull($listener->handle($this->getGetResponseEvent()));
}
public function testOnCoreSecurityDoesNothingWhenNoCookieIsSet()
{
list($listener, $tokenStorage, $service, ,) = $this->getListener();
$tokenStorage
->expects($this->once())
->method('getToken')
->will($this->returnValue(null))
;
$service
->expects($this->once())
->method('autoLogin')
->will($this->returnValue(null))
;
$event = $this->getGetResponseEvent();
$event
->expects($this->once())
->method('getRequest')
->will($this->returnValue(new Request()))
;
$this->assertNull($listener->handle($event));
}
public function testOnCoreSecurityIgnoresAuthenticationExceptionThrownByAuthenticationManagerImplementation()
{
list($listener, $tokenStorage, $service, $manager,) = $this->getListener();
$tokenStorage
->expects($this->once())
->method('getToken')
->will($this->returnValue(null))
;
$service
->expects($this->once())
->method('autoLogin')
->will($this->returnValue($this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface')))
;
$service
->expects($this->once())
->method('loginFail')
;
$exception = new AuthenticationException('Authentication failed.');
$manager
->expects($this->once())
->method('authenticate')
->will($this->throwException($exception))
;
$event = $this->getGetResponseEvent();
$event
->expects($this->once())
->method('getRequest')
->will($this->returnValue(new Request()))
;
$listener->handle($event);
}
/**
* @expectedException SymfonyComponentSecurityCoreExceptionAuthenticationException
* @expectedExceptionMessage Authentication failed.
*/
public function testOnCoreSecurityIgnoresAuthenticationOptionallyRethrowsExceptionThrownAuthenticationManagerImplementation()
{
list($listener, $tokenStorage, $service, $manager,) = $this->getListener(false, false);
$tokenStorage
->expects($this->once())
->method('getToken')
->will($this->returnValue(null))
;
$service
->expects($this->once())
->method('autoLogin')
->will($this->returnValue($this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface')))
;
$service
->expects($this->once())
->method('loginFail')
;
$exception = new AuthenticationException('Authentication failed.');
$manager
->expects($this->once())
->method('authenticate')
->will($this->throwException($exception))
;
$event = $this->getGetResponseEvent();
$event
->expects($this->once())
->method('getRequest')
->will($this->returnValue(new Request()))
;
$listener->handle($event);
}
public function testOnCoreSecurity()
{
list($listener, $tokenStorage, $service, $manager,) = $this->getListener();
$tokenStorage
->expects($this->once())
->method('getToken')
->will($this->returnValue(null))
;
$token = $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface');
$service
->expects($this->once())
->method('autoLogin')
->will($this->returnValue($token))
;
$tokenStorage
->expects($this->once())
->method('setToken')
->with($this->equalTo($token))
;
$manager
->expects($this->once())
->method('authenticate')
->will($this->returnValue($token))
;
$event = $this->getGetResponseEvent();
$event
->expects($this->once())
->method('getRequest')
->will($this->returnValue(new Request()))
;
$listener->handle($event);
}
public function testOnCoreSecurityInteractiveLoginEventIsDispatchedIfDispatcherIsPresent()
{
list($listener, $tokenStorage, $service, $manager, , $dispatcher) = $this->getListener(true);
$tokenStorage
->expects($this->once())
->method('getToken')
->will($this->returnValue(null))
;
$token = $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface');
$service
->expects($this->once())
->method('autoLogin')
->will($this->returnValue($token))
;
$tokenStorage
->expects($this->once())
->method('setToken')
->with($this->equalTo($token))
;
$manager
->expects($this->once())
->method('authenticate')
->will($this->returnValue($token))
;
$event = $this->getGetResponseEvent();
$request = new Request();
$event
->expects($this->once())
->method('getRequest')
->will($this->returnValue($request))
;
$dispatcher
->expects($this->once())
->method('dispatch')
->with(
SecurityEvents::INTERACTIVE_LOGIN,
$this->isInstanceOf('SymfonyComponentSecurityHttpEventInteractiveLoginEvent')
)
;
$listener->handle($event);
}
protected function getGetResponseEvent()
{
return $this->getMock('SymfonyComponentHttpKernelEventGetResponseEvent', array(), array(), '', false);
}
protected function getFilterResponseEvent()
{
return $this->getMock('SymfonyComponentHttpKernelEventFilterResponseEvent', array(), array(), '', false);
}
protected function getListener($withDispatcher = false, $catchExceptions = true)
{
$listener = new RememberMeListener(
$tokenStorage = $this->getTokenStorage(),
$service = $this->getService(),
$manager = $this->getManager(),
$logger = $this->getLogger(),
$dispatcher = ($withDispatcher ? $this->getDispatcher() : null),
$catchExceptions
);
return array($listener, $tokenStorage, $service, $manager, $logger, $dispatcher);
}
protected function getLogger()
{
return $this->getMock('PsrLogLoggerInterface');
}
protected function getManager()
{
return $this->getMock('SymfonyComponentSecurityCoreAuthenticationAuthenticationManagerInterface');
}
protected function getService()
{
return $this->getMock('SymfonyComponentSecurityHttpRememberMeRememberMeServicesInterface');
}
protected function getTokenStorage()
{
return $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface');
}
protected function getDispatcher()
{
return $this->getMock('SymfonyComponentEventDispatcherEventDispatcherInterface');
}
}