use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
-use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
-use Symfony\Component\HttpKernel\Event\GetResponseEvent;
-use Symfony\Component\HttpKernel\HttpKernelInterface;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
+use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\EventListener\SessionListener;
use Symfony\Component\HttpKernel\EventListener\TestSessionListener;
-use Symfony\Component\HttpFoundation\Session\SessionInterface;
+use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* SessionListenerTest.
$this->assertNotEmpty($response->headers->getCookies());
}
+ /**
+ * @dataProvider anotherCookieProvider
+ */
+ public function testSessionWithNewSessionIdAndNewCookieDoesNotSendAnotherCookie($existing, array $expected)
+ {
+ $this->sessionHasBeenStarted();
+ $this->sessionIsEmpty();
+ $this->fixSessionId('456');
+
+ $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
+ $request = Request::create('/', 'GET', array(), array('MOCKSESSID' => '123'));
+ $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
+ $this->listener->onKernelRequest($event);
+
+ $response = new Response('', 200, array('Set-Cookie' => $existing));
+
+ $response = $this->filterResponse(new Request(), HttpKernelInterface::MASTER_REQUEST, $response);
+
+ $this->assertSame($expected, $response->headers->get('Set-Cookie', null, false));
+ }
+
+ public function anotherCookieProvider()
+ {
+ return array(
+ 'same' => array('MOCKSESSID=789; path=/', array('MOCKSESSID=789; path=/')),
+ 'different domain' => array('MOCKSESSID=789; path=/; domain=example.com', array('MOCKSESSID=789; path=/; domain=example.com', 'MOCKSESSID=456; path=/')),
+ 'different path' => array('MOCKSESSID=789; path=/foo', array('MOCKSESSID=789; path=/foo', 'MOCKSESSID=456; path=/')),
+ );
+ }
+
public function testUnstartedSessionIsNotSave()
{
$this->sessionHasNotBeenStarted();
$this->assertFalse(is_subclass_of(TestSessionListener::class, ServiceSubscriberInterface::class, 'Implementing ServiceSubscriberInterface would create a dep on the DI component, which eg Silex cannot afford'));
}
- private function filterResponse(Request $request, $type = HttpKernelInterface::MASTER_REQUEST)
+ private function filterResponse(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, Response $response = null)
{
$request->setSession($this->session);
- $response = new Response();
+ $response = $response ?: new Response();
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
$event = new FilterResponseEvent($kernel, $request, $type, $response);