summaryrefslogtreecommitdiffstats
path: root/Tests/Http/Firewall
diff options
context:
space:
mode:
authorJakub Zalas <jakub@zalas.pl>2013-05-27 21:42:52 +0100
committerJakub Zalas <jakub@zalas.pl>2013-06-02 18:27:22 +0100
commit1202a61dbc24e3f713ad1483975eabc0ce6dd2e8 (patch)
tree28737282c541789249f59c498b8598e0d4be84ab /Tests/Http/Firewall
parentb92bb91cb0cb1042f26f5db266c8a4594311301b (diff)
downloadsymfony-security-1202a61dbc24e3f713ad1483975eabc0ce6dd2e8.zip
symfony-security-1202a61dbc24e3f713ad1483975eabc0ce6dd2e8.tar.gz
symfony-security-1202a61dbc24e3f713ad1483975eabc0ce6dd2e8.tar.bz2
[Security] Added tests for the ContextListener.
Diffstat (limited to 'Tests/Http/Firewall')
-rw-r--r--Tests/Http/Firewall/ContextListenerTest.php82
1 files changed, 75 insertions, 7 deletions
diff --git a/Tests/Http/Firewall/ContextListenerTest.php b/Tests/Http/Firewall/ContextListenerTest.php
index ffe6195..336c333 100644
--- a/Tests/Http/Firewall/ContextListenerTest.php
+++ b/Tests/Http/Firewall/ContextListenerTest.php
@@ -17,6 +17,7 @@ use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
+use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\Security\Http\Firewall\ContextListener;
@@ -48,6 +49,32 @@ class ContextListenerTest extends \PHPUnit_Framework_TestCase
unset($this->securityContext);
}
+ /**
+ * @expectedException \InvalidArgumentException
+ * @expectedExceptionMessage $contextKey must not be empty
+ */
+ public function testItRequiresContextKey()
+ {
+ new ContextListener(
+ $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface'),
+ array(),
+ ''
+ );
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ * @expectedExceptionMessage User provider "stdClass" must implement "Symfony\Component\Security\Core\User\UserProviderInterface
+ */
+ public function testUserProvidersNeedToImplementAnInterface()
+ {
+ new ContextListener(
+ $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface'),
+ array(new \stdClass()),
+ 'key123'
+ );
+ }
+
public function testOnKernelResponseWillAddSession()
{
$session = $this->runSessionOnKernelResponse(
@@ -131,9 +158,7 @@ class ContextListenerTest extends \PHPUnit_Framework_TestCase
->disableOriginalConstructor()
->getMock();
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
- $session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')
- ->disableOriginalConstructor()
- ->getMock();
+ $session = $this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface');
$event->expects($this->any())
->method('getRequest')
@@ -147,7 +172,7 @@ class ContextListenerTest extends \PHPUnit_Framework_TestCase
$session->expects($this->any())
->method('get')
->with('_security_key123')
- ->will($this->returnValue(serialize($token)));
+ ->will($this->returnValue($token));
$context->expects($this->once())
->method('setToken')
->with(null);
@@ -159,11 +184,53 @@ class ContextListenerTest extends \PHPUnit_Framework_TestCase
public function provideInvalidToken()
{
return array(
- array(new \__PHP_Incomplete_Class()),
- array(null),
+ array(serialize(new \__PHP_Incomplete_Class())),
+ array(serialize(null)),
+ array(null)
);
}
+ public function testHandleAddsKernelResponseListener()
+ {
+ $context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
+ $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
+ $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $listener = new ContextListener($context, array(), 'key123', null, $dispatcher);
+
+ $event->expects($this->any())
+ ->method('getRequestType')
+ ->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST));
+ $event->expects($this->any())
+ ->method('getRequest')
+ ->will($this->returnValue($this->getMock('Symfony\Component\HttpFoundation\Request')));
+
+ $dispatcher->expects($this->once())
+ ->method('addListener')
+ ->with(KernelEvents::RESPONSE, array($listener, 'onKernelResponse'));
+
+ $listener->handle($event);
+ }
+
+ public function testHandleRemovesTokenIfNoPreviousSessionWasFound()
+ {
+ $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
+ $request->expects($this->any())->method('hasPreviousSession')->will($this->returnValue(false));
+
+ $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $event->expects($this->any())->method('getRequest')->will($this->returnValue($request));
+
+ $context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
+ $context->expects($this->once())->method('setToken')->with(null);
+
+ $listener = new ContextListener($context, array(), 'key123');
+ $listener->handle($event);
+ }
+
protected function runSessionOnKernelResponse($newToken, $original = null)
{
$session = new Session(new MockArraySessionStorage());
@@ -189,4 +256,5 @@ class ContextListenerTest extends \PHPUnit_Framework_TestCase
$listener->onKernelResponse($event);
return $session;
- }}
+ }
+}