123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- <?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 Symfony\Component\Security\Http\Tests;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface;
- use Symfony\Component\Security\Core\Exception\AuthenticationException;
- use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
- use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
- use Symfony\Component\Security\Http\Authentication\SimpleAuthenticationHandler;
- class SimpleAuthenticationHandlerTest extends TestCase
- {
- private $successHandler;
- private $failureHandler;
- private $request;
- private $token;
- private $authenticationException;
- private $response;
- protected function setUp()
- {
- $this->successHandler = $this->getMockBuilder('Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface')->getMock();
- $this->failureHandler = $this->getMockBuilder('Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface')->getMock();
- $this->request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
- $this->token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
- // No methods are invoked on the exception; we just assert on its class
- $this->authenticationException = new AuthenticationException();
- $this->response = new Response();
- }
- public function testOnAuthenticationSuccessFallsBackToDefaultHandlerIfSimpleIsNotASuccessHandler()
- {
- $authenticator = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock();
- $this->successHandler->expects($this->once())
- ->method('onAuthenticationSuccess')
- ->with($this->request, $this->token)
- ->will($this->returnValue($this->response));
- $handler = new SimpleAuthenticationHandler($authenticator, $this->successHandler, $this->failureHandler);
- $result = $handler->onAuthenticationSuccess($this->request, $this->token);
- $this->assertSame($this->response, $result);
- }
- public function testOnAuthenticationSuccessCallsSimpleAuthenticator()
- {
- $this->successHandler->expects($this->never())
- ->method('onAuthenticationSuccess');
- $authenticator = $this->getMockForAbstractClass('Symfony\Component\Security\Http\Tests\TestSuccessHandlerInterface');
- $authenticator->expects($this->once())
- ->method('onAuthenticationSuccess')
- ->with($this->request, $this->token)
- ->will($this->returnValue($this->response));
- $handler = new SimpleAuthenticationHandler($authenticator, $this->successHandler, $this->failureHandler);
- $result = $handler->onAuthenticationSuccess($this->request, $this->token);
- $this->assertSame($this->response, $result);
- }
- /**
- * @expectedException \UnexpectedValueException
- * @expectedExceptionMessage onAuthenticationSuccess method must return null to use the default success handler, or a Response object
- */
- public function testOnAuthenticationSuccessThrowsAnExceptionIfNonResponseIsReturned()
- {
- $this->successHandler->expects($this->never())
- ->method('onAuthenticationSuccess');
- $authenticator = $this->getMockForAbstractClass('Symfony\Component\Security\Http\Tests\TestSuccessHandlerInterface');
- $authenticator->expects($this->once())
- ->method('onAuthenticationSuccess')
- ->with($this->request, $this->token)
- ->will($this->returnValue(new \stdClass()));
- $handler = new SimpleAuthenticationHandler($authenticator, $this->successHandler, $this->failureHandler);
- $handler->onAuthenticationSuccess($this->request, $this->token);
- }
- public function testOnAuthenticationSuccessFallsBackToDefaultHandlerIfNullIsReturned()
- {
- $this->successHandler->expects($this->once())
- ->method('onAuthenticationSuccess')
- ->with($this->request, $this->token)
- ->will($this->returnValue($this->response));
- $authenticator = $this->getMockForAbstractClass('Symfony\Component\Security\Http\Tests\TestSuccessHandlerInterface');
- $authenticator->expects($this->once())
- ->method('onAuthenticationSuccess')
- ->with($this->request, $this->token)
- ->will($this->returnValue(null));
- $handler = new SimpleAuthenticationHandler($authenticator, $this->successHandler, $this->failureHandler);
- $result = $handler->onAuthenticationSuccess($this->request, $this->token);
- $this->assertSame($this->response, $result);
- }
- public function testOnAuthenticationFailureFallsBackToDefaultHandlerIfSimpleIsNotAFailureHandler()
- {
- $authenticator = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock();
- $this->failureHandler->expects($this->once())
- ->method('onAuthenticationFailure')
- ->with($this->request, $this->authenticationException)
- ->will($this->returnValue($this->response));
- $handler = new SimpleAuthenticationHandler($authenticator, $this->successHandler, $this->failureHandler);
- $result = $handler->onAuthenticationFailure($this->request, $this->authenticationException);
- $this->assertSame($this->response, $result);
- }
- public function testOnAuthenticationFailureCallsSimpleAuthenticator()
- {
- $this->failureHandler->expects($this->never())
- ->method('onAuthenticationFailure');
- $authenticator = $this->getMockForAbstractClass('Symfony\Component\Security\Http\Tests\TestFailureHandlerInterface');
- $authenticator->expects($this->once())
- ->method('onAuthenticationFailure')
- ->with($this->request, $this->authenticationException)
- ->will($this->returnValue($this->response));
- $handler = new SimpleAuthenticationHandler($authenticator, $this->successHandler, $this->failureHandler);
- $result = $handler->onAuthenticationFailure($this->request, $this->authenticationException);
- $this->assertSame($this->response, $result);
- }
- /**
- * @expectedException \UnexpectedValueException
- * @expectedExceptionMessage onAuthenticationFailure method must return null to use the default failure handler, or a Response object
- */
- public function testOnAuthenticationFailureThrowsAnExceptionIfNonResponseIsReturned()
- {
- $this->failureHandler->expects($this->never())
- ->method('onAuthenticationFailure');
- $authenticator = $this->getMockForAbstractClass('Symfony\Component\Security\Http\Tests\TestFailureHandlerInterface');
- $authenticator->expects($this->once())
- ->method('onAuthenticationFailure')
- ->with($this->request, $this->authenticationException)
- ->will($this->returnValue(new \stdClass()));
- $handler = new SimpleAuthenticationHandler($authenticator, $this->successHandler, $this->failureHandler);
- $handler->onAuthenticationFailure($this->request, $this->authenticationException);
- }
- public function testOnAuthenticationFailureFallsBackToDefaultHandlerIfNullIsReturned()
- {
- $this->failureHandler->expects($this->once())
- ->method('onAuthenticationFailure')
- ->with($this->request, $this->authenticationException)
- ->will($this->returnValue($this->response));
- $authenticator = $this->getMockForAbstractClass('Symfony\Component\Security\Http\Tests\TestFailureHandlerInterface');
- $authenticator->expects($this->once())
- ->method('onAuthenticationFailure')
- ->with($this->request, $this->authenticationException)
- ->will($this->returnValue(null));
- $handler = new SimpleAuthenticationHandler($authenticator, $this->successHandler, $this->failureHandler);
- $result = $handler->onAuthenticationFailure($this->request, $this->authenticationException);
- $this->assertSame($this->response, $result);
- }
- }
- interface TestSuccessHandlerInterface extends AuthenticationSuccessHandlerInterface, SimpleAuthenticatorInterface
- {
- }
- interface TestFailureHandlerInterface extends AuthenticationFailureHandlerInterface, SimpleAuthenticatorInterface
- {
- }
|