8000 Restore SessionListener class for backward compatibility with Silex by GromNaN · Pull Request #22171 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Restore SessionListener class for backward compatibility with Silex #22171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add test on *SessionListener* classes
  • Loading branch information
GromNaN committed Mar 27, 2017
commit 7b0ce40cd8b1bace1d2b2ca7efc074083863ea7b
10000
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?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\HttpKernel\Tests\EventListener;

use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpKernel\EventListener\ContainerAwareSessionListener;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

class ContainerAwareSessionListenerTest extends TestCase
{
/**
* @var ContainerInterface
*/
private $container;

/**
* @var SessionInterface
*/
private $session;

/**
* @var ContainerAwareSessionListener
*/
private $listener;

protected function setUp()
{
$this->container = $this->getMockBuilder(ContainerInterface::class)
->disableOriginalConstructor()
->getMock();
$this->session = $this->getMockBuilder(SessionInterface::class)
->disableOriginalConstructor()
->getMock();

$this->listener = new ContainerAwareSessionListener($this->container);
}

public function testShouldGetSessionService()
{
$this->containerHavingSession();

$this->assertSame($this->session, $this->getSession());
}

public function testShouldGetSessionNullWhenServiceIsNotDefined()
{
$this->containerNotHavingSession();

$this->assertNull($this->getSession());
}

private function getSession()
{
$method = (new \ReflectionClass($this->listener))
->getMethod('getSession');
$method->setAccessible(true);

return $method->invoke($this->listener);
}

private function containerHavingSession()
{
$this->container->expects($this->any())
->method('has')
->with($this->equalTo('session'))
->will($this->returnValue(true));

$this->container->expects($this->any())
->method('get')
->with($this->equalTo('session'))
->will($this->returnValue($this->session));
}

private function containerNotHavingSession()
{
$this->container->expects($this->any())
->method('has')
->with($this->equalTo('session'))
->will($this->returnValue(false));

$this->container->expects($this->never())
->method('get')
->with($this->equalTo('session'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?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\HttpKernel\Tests\EventListener;

use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpKernel\EventListener\ContainerAwareTestSessionListener;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

class ContainerAwareTestSessionListenerTest extends TestCase
{
/**
* @var ContainerInterface
*/
private $container;

/**
* @var SessionInterface
*/
private $session;

/**
* @var ContainerAwareTestSessionListener
*/
private $listener;

protected function setUp()
{
$this->container = $this->getMockBuilder(ContainerInterface::class)
->disableOriginalConstructor()
->getMock();
$this->session = $this->getMockBuilder(SessionInterface::class)
->disableOriginalConstructor()
->getMock();

$this->listener = new ContainerAwareTestSessionListener($this->container);
}

public function testShouldGetSessionService()
{
$this->containerHavingSession();

$this->assertSame($this->session, $this->getSession());
}

public function testShouldGetSessionNullWhenServiceIsNotDefined()
{
$this->containerNotHavingSession();

$this->assertNull($this->getSession());
}

private function getSession()
{
$method = (new \ReflectionClass($this->listener))
->getMethod('getSession');
$method->setAccessible(true);

return $method->invoke($this->listener);
}

private function containerHavingSession()
{
$this->container->expects($this->any())
->method('has')
->with($this->equalTo('session'))
->will($this->returnValue(true));

$this->container->expects($this->any())
->method('get')
->with($this->equalTo('session'))
->will($this->returnValue($this->session));
}

private function containerNotHavingSession()
{
$this->container->expects($this->any())
->method('has')
->with($this->equalTo('session'))
->will($this->returnValue(false));

$this->container->expects($this->never())
->method('get')
->with($this->equalTo('session'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?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\HttpKernel\Tests\EventListener;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\EventListener\SessionListener;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

/**
* SessionListenerTest.
*
* Tests SessionListener.
*/
class SessionListenerTest extends TestCase
{
/**
* @var Request
*/
private $request;

/**
* @var SessionListener
*/
private $listener;

/**
* @var SessionInterface
*/
private $session;

protected function setUp()
{
$this->request = $this->getMockBuilder(Request::class)
->disableOriginalConstructor()
->getMock();
$this->listener = $this->getMockForAbstractClass(SessionListener::class);
$this->session = $this->getSession();
}

public function testShouldSetSessionOnMasterRequest()
{
$this->sessionIsDefined();
$this->sessionMustBeSet();

$this->kernelRequest($this->request);
}

public function testShouldNotSetSessionOnSubRequest()
{
$this->sessionIsDefined();
$this->sessionMustNotBeSet();

$this->kernelRequest(new Request(), HttpKernelInterface::SUB_REQUEST);
}

public function testShouldNotSetNullSession()
{
$this->sessionIsNull();
$this->sessionMustNotBeSet();

$this->kernelRequest(new Request());
}

public function testShouldNotReplaceSession()
{
$this->sessionIsDefined();
$this->sessionMustNotBeSet();

$this->kernelRequest(new Request());
}

private function kernelRequest(Request $request, $type = HttpKernelInterface::MASTER_REQUEST)
{
$response = new Response();
$kernel = $this->getMockBuilder(HttpKernelInterface::class)->getMock();
$event = new GetResponseEvent($kernel, $request, $type);
$event->setResponse($response);

$this->listener->onKernelRequest($event);

$this->assertSame($response, $event->getResponse());
}

private function sessionIsDefined()
{
$this->listener->expects($this->any())
->method('getSession')
->will($this->returnValue($this->session));
}

private function sessionIsNull()
{
$this->listener->expects($this->any())
->method('getSession')
->will($this->returnValue(null));
}

private function sessionAlreadySet()
{
$this->request->expects($this->any())
->method('getSession')
->will($this->returnValue(clone $this->session));
}

private function sessionMustBeSet()
{
$this->request->expects($this->once())
->method('setSession')
->with($this->identicalTo($this->session));
}

private function sessionMustNotBeSet()
{
$this->request->expects($this->never())
->method('setSession');
}

private function getSession()
{
$mock = $this->getMockBuilder(SessionInterface::class)
->disableOriginalConstructor()
->getMock();

return $mock;
}
}
0