|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony framework. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * This source file is subject to the MIT license that is bundled |
| 9 | + * with this source code in the file LICENSE. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Tests\Component\Security\Http\Firewall; |
| 13 | + |
| 14 | +use Symfony\Component\HttpFoundation\Request; |
| 15 | +use Symfony\Component\HttpFoundation\Response; |
| 16 | +use Symfony\Component\Security\Http\Firewall\LogoutListener; |
| 17 | + |
| 18 | +class LogoutListenerTest extends \PHPUnit_Framework_TestCase |
| 19 | +{ |
| 20 | + public function testHandleUnmatchedPath() |
| 21 | + { |
| 22 | + list($listener, $context, $httpUtils, $options) = $this->getListener(); |
| 23 | + |
| 24 | + list($event, $request) = $this->getGetResponseEvent(); |
| 25 | + |
| 26 | + $event->expects($this->never()) |
| 27 | + ->method('setResponse'); |
| 28 | + |
| 29 | + $httpUtils->expects($this->once()) |
| 30 | + ->method('checkRequestPath') |
| 31 | + ->with($request, $options['logout_path']) |
| 32 | + ->will($this->returnValue(false)); |
| 33 | + |
| 34 | + $listener->handle($event); |
| 35 | + } |
| 36 | + |
| 37 | + public function testHandleMatchedPathWithSuccessHandler() |
| 38 | + { |
| 39 | + $successHandler = $this->getSuccessHandler(); |
| 40 | + |
| 41 | + list($listener, $context, $httpUtils, $options) = $this->getListener($successHandler); |
| 42 | + |
| 43 | + list($event, $request) = $this->getGetResponseEvent(); |
| 44 | + |
| 45 | + $httpUtils->expects($this->once()) |
| 46 | + ->method('checkRequestPath') |
| 47 | + ->with($request, $options['logout_path']) |
| 48 | + ->will($this->returnValue(true)); |
| 49 | + |
| 50 | + $successHandler->expects($this->once()) |
| 51 | + ->method('onLogoutSuccess') |
| 52 | + ->with($request) |
| 53 | + ->will($this->returnValue($response = new Response())); |
| 54 | + |
| 55 | + $context->expects($this->once()) |
| 56 | + ->method('getToken') |
| 57 | + ->will($this->returnValue($token = $this->getToken())); |
| 58 | + |
| 59 | + $handler = $this->getHandler(); |
| 60 | + $handler->expects($this->once()) |
| 61 | + ->method('logout') |
| 62 | + ->with($request, $response, $token); |
| 63 | + |
| 64 | + $context->expects($this->once()) |
| 65 | + ->method('setToken') |
| 66 | + ->with(null); |
| 67 | + |
| 68 | + $event->expects($this->once()) |
| 69 | + ->method('setResponse') |
| 70 | + ->with($response); |
| 71 | + |
| 72 | + $listener->addHandler($handler); |
| 73 | + |
| 74 | + $listener->handle($event); |
| 75 | + } |
| 76 | + |
| 77 | + public function testHandleMatchedPathWithoutSuccessHandler() |
| 78 | + { |
| 79 | + list($listener, $context, $httpUtils, $options) = $this->getListener(); |
| 80 | + |
| 81 | + list($event, $request) = $this->getGetResponseEvent(); |
| 82 | + |
| 83 | + $httpUtils->expects($this->once()) |
| 84 | + ->method('checkRequestPath') |
| 85 | + ->with($request, $options['logout_path']) |
| 86 | + ->will($this->returnValue(true)); |
| 87 | + |
| 88 | + $httpUtils->expects($this->once()) |
| 89 | + ->method('createRedirectResponse') |
| 90 | + ->with($request, $options['target_url']) |
| 91 | + ->will($this->returnValue($response = new Response())); |
| 92 | + |
| 93 | + $context->expects($this->once()) |
| 94 | + ->method('getToken') |
| 95 | + ->will($this->returnValue($token = $this->getToken())); |
| 96 | + |
| 97 | + $handler = $this->getHandler(); |
| 98 | + $handler->expects($this->once()) |
| 99 | + ->method('logout') |
| 100 | + ->with($request, $response, $token); |
| 101 | + |
| 102 | + $context->expects($this->once()) |
| 103 | + ->method('setToken') |
| 104 | + ->with(null); |
| 105 | + |
| 106 | + $event->expects($this->once()) |
| 107 | + ->method('setResponse') |
| 108 | + ->with($response); |
| 109 | + |
| 110 | + $listener->addHandler($handler); |
| 111 | + |
| 112 | + $listener->handle($event); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @expectedException RuntimeException |
| 117 | + */ |
| 118 | + public function testSuccessHandlerReturnsNonResponse() |
| 119 | + { |
| 120 | + $successHandler = $this->getSuccessHandler(); |
| 121 | + |
| 122 | + list($listener, $context, $httpUtils, $options) = $this->getListener($successHandler); |
| 123 | + |
| 124 | + list($event, $request) = $this->getGetResponseEvent(); |
| 125 | + |
| 126 | + $httpUtils->expects($this->once()) |
| 127 | + ->method('checkRequestPath') |
| 128 | + ->with($request, $options['logout_path']) |
| 129 | + ->will($this->returnValue(true)); |
| 130 | + |
| 131 | + $successHandler->expects($this->once()) |
| 132 | + ->method
38BA
span>('onLogoutSuccess') |
| 133 | + ->with($request) |
| 134 | + ->will($this->returnValue(null)); |
| 135 | + |
| 136 | + $listener->handle($event); |
| 137 | + } |
| 138 | + |
| 139 | + private function getContext() |
| 140 | + { |
| 141 | + return $this->getMockBuilder('Symfony\Component\Security\Core\SecurityContext') |
| 142 | + ->disableOriginalConstructor() |
| 143 | + ->getMock(); |
| 144 | + } |
| 145 | + |
| 146 | + private function getGetResponseEvent() |
| 147 | + { |
| 148 | + $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent') |
| 149 | + ->disableOriginalConstructor() |
| 150 | + ->getMock(); |
| 151 | + |
| 152 | + $event->expects($this->any()) |
| 153 | + ->method('getRequest') |
| 154 | + ->will($this->returnValue($request = new Request())); |
| 155 | + |
| 156 | + return array($event, $request); |
| 157 | + } |
| 158 | + |
| 159 | + private function getHandler() |
| 160 | + { |
| 161 | + return $this->getMock('Symfony\Component\Security\Http\Logout\LogoutHandlerInterface'); |
| 162 | + } |
| 163 | + |
| 164 | + private function getHttpUtils() |
| 165 | + { |
| 166 | + return $this->getMockBuilder('Symfony\Component\Security\Http\HttpUtils') |
| 167 | + ->disableOriginalConstructor() |
| 168 | + ->getMock(); |
| 169 | + } |
| 170 | + |
| 171 | + private function getListener($successHandler = null) |
| 172 | + { |
| 173 | + $listener = new LogoutListener( |
| 174 | + $context = $this->getContext(), |
| 175 | + $httpUtils = $this->getHttpUtils(), |
| 176 | + $options = array( |
| 177 | + 'logout_path' => '/logout', |
| 178 | + 'target_url' => '/', |
| 179 | + ), |
| 180 | + $successHandler |
| 181 | + ); |
| 182 | + |
| 183 | + return array($listener, $context, $httpUtils, $options); |
| 184 | + } |
| 185 | + |
| 186 | + private function getSuccessHandler() |
| 187 | + { |
| 188 | + return $this->getMock('Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface'); |
| 189 | + } |
| 190 | + |
| 191 | + private function getToken() |
| 192 | + { |
| 193 | + return $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); |
| 194 | + } |
| 195 | +} |
0 commit comments