Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0fc60f4

Browse files
committed
Add test on *SessionListener* classes
1 parent e8f0972 commit 0fc60f4

File tree

3 files changed

+317
-0
lines changed

3 files changed

+317
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Tests\EventListener;
13+
14+
use Psr\Container\ContainerInterface;
15+
use Symfony\Component\HttpKernel\EventListener\ContainerAwareSessionListener;
16+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
17+
18+
class ContainerAwareSessionListenerTest extends \PHPUnit_Framework_TestCase
19+
{
20+
/**
21+
* @var ContainerInterface
22+
*/
23+
private $container;
24+
25+
/**
26+
* @var SessionInterface
27+
*/
28+
private $session;
29+
30+
/**
31+
* @var ContainerAwareSessionListener
32+
*/
33+
private $listener;
34+
35+
protected function setUp()
36+
{
37+
$this->container = $this->createMock(ContainerInterface::class);
38+
$this->session = $this->createMock(SessionInterface::class);
39+
40+
$this->listener = new ContainerAwareSessionListener($this->container);
41+
}
42+
43+
public function testShouldGetSessionService()
44+
{
45+
$this->containerHavingSession();
46+
47+
$this->assertSame($this->session, $this->getSession());
48+
}
49+
50+
public function testShouldGetSessionNullWhenServiceIsNotDefined()
51+
{
52+
$this->containerNotHavingSession();
53+
54+
$this->assertNull($this->getSession());
55+
}
56+
57+
private function getSession()
58+
{
59+
$method = (new \ReflectionClass($this->listener))
60+
->getMethod('getSession');
61+
$method->setAccessible(true);
62+
63+
return $method->invoke($this->listener);
64+
}
65+
66+
private function containerHavingSession()
67+
{
68+
$this->container->expects($this->any())
69+
->method('has')
70+
->with($this->equalTo('session'))
71+
->will($this->returnValue(true));
72+
73+
$this->container->expects($this->any())
74+
->method('get')
75+
->with($this->equalTo('session'))
76+
->will($this->returnValue($this->session));
77+
}
78+
79+
private function containerNotHavingSession()
80+
{
81+
$this->container->expects($this->any())
82+
->method('has')
83+
->with($this->equalTo('session'))
84+
->will($this->returnValue(false));
85+
86+
$this->container->expects($this->never())
87+
->method('get')
88+
->with($this->equalTo('session'));
89+
}
90+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Tests\EventListener;
13+
14+
use Psr\Container\ContainerInterface;
15+
use Symfony\Component\HttpKernel\EventListener\ContainerAwareTestSessionListener;
16+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
17+
18+
class ContainerAwareTestSessionListenerTest extends \PHPUnit_Framework_TestCase
19+
{
20+
/**
21+
* @var ContainerInterface
22+
*/
23+
private $container;
24+
25+
/**
26+
* @var SessionInterface
27+
*/
28+
private $session;
29+
30+
/**
31+
* @var ContainerAwareTestSessionListener
32+
*/
33+
private $listener;
34+
35+
protected function setUp()
36+
{
37+
$this->container = $this->createMock(ContainerInterface::class);
38+
$this->session = $this->createMock(SessionInterface::class);
39+
40+
$this->listener = new ContainerAwareTestSessionListener($this->container);
41+
}
42+
43+
public function testShouldGetSessionService()
44+
{
45+
$this->containerHavingSession();
46+
47+
$this->assertSame($this->session, $this->getSession());
48+
}
49+
50+
public function testShouldGetSessionNullWhenServiceIsNotDefined()
51+
{
52+
$this->containerNotHavingSession();
53+
54+
$this->assertNull($this->getSession());
55+
}
56+
57+
private function getSession()
58+
{
59+
$method = (new \ReflectionClass($this->listener))
60+
->getMethod('getSession');
61+
$method->setAccessible(true);
62+
63+
return $method->invoke($this->listener);
64+
}
65+
66+
private function containerHavingSession()
67+
{
68+
$this->container->expects($this->any())
69+
->method('has')
70+
->with($this->equalTo('session'))
71+
->will($this->returnValue(true));
72+
73+
$this->container->expects($this->any())
74+
->method('get')
75+
->with($this->equalTo('session'))
76+
->will($this->returnValue($this->session));
77+
}
78+
79+
private function containerNotHavingSession()
80+
{
81+
$this->container->expects($this->any())
82+
->method('has')
83+
->with($this->equalTo('session'))
84+
->will($this->returnValue(false));
85+
86+
$this->container->expects($this->never())
87+
->method('get')
88+
->with($this->equalTo('session'));
89+
}
90+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Tests\EventListener;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\HttpFoundation\Request;
17+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
18+
use Symfony\Component\HttpKernel\EventListener\SessionListener;
19+
use Symfony\Component\HttpKernel\HttpKernelInterface;
20+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
21+
22+
/**
23+
* SessionListenerTest.
24+
*
25+
* Tests SessionListener.
26+
*/
27+
class SessionListenerTest extends TestCase
28+
{
29+
/**
30+
* @var Request
31+
*/
32+
private $request;
33+
34+
/**
35+
* @var SessionListener
36+
*/
37+
private $listener;
38+
39+
/**
40+
* @var SessionInterface
41+
*/
42+
private $session;
43+
44+
protected function setUp()
45+
{
46+
$this->request = $this->createMock(Request::class);
47+
$this->listener = $this->getMockForAbstractClass(SessionListener::class);
48+
$this->session = $this->getSession();
49+
}
50+
51+
public function testShouldSetSessionOnMasterRequest()
52+
{
53+
$this->sessionIsDefined();
54+
$this->sessionMustBeSet();
55+
56+
$this->kernelRequest($this->request);
57+
}
58+
59+
public function testShouldNotSetSessionOnSubRequest()
60+
{
61+
$this->sessionIsDefined();
62+
$this->sessionMustNotBeSet();
63+
64+
$this->kernelRequest(new Request(), HttpKernelInterface::SUB_REQUEST);
65+
}
66+
67+
public function testShouldNotSetNullSession()
68+
{
69+
$this->sessionIsNull();
70+
$this->sessionMustNotBeSet();
71+
72+
$this->kernelRequest(new Request());
73+
}
74+
75+
public function testShouldNotReplaceSession()
76+
{
77+
$this->sessionIsDefined();
78+
$this->sessionMustNotBeSet();
79+
80+
$this->kernelRequest(new Request());
81+
}
82+
83+
private function kernelRequest(Request $request, $type = HttpKernelInterface::MASTER_REQUEST)
84+
{
85+
$response = new Response();
86+
$kernel = $this->getMockBuilder(HttpKernelInterface::class)->getMock();
87+
$event = new GetResponseEvent($kernel, $request, $type);
88+
$event->setResponse($response);
89+
90+
$this->listener->onKernelRequest($event);
91+
92+
$this->assertSame($response, $event->getResponse());
93+
}
94+
95+
private function sessionIsDefined()
96+
{
97+
$this->listener->expects($this->any())
98+
->method('getSession')
99+
->will($this->returnValue($this->session));
100+
}
101+
102+
private function sessionIsNull()
103+
{
104+
$this->listener->expects($this->any())
105+
->method('getSession')
106+
->will($this->returnValue(null));
107+
}
108+
109+
private function sessionAlreadySet()
110+
{
111+
$this->request->expects($this->any())
112+
->method('getSession')
113+
->will($this->returnValue(clone $this->session));
114+
}
115+
116+
private function sessionMustBeSet()
117+
{
118+
$this->request->expects($this->once())
119+
->method('setSession')
120+
->with($this->identicalTo($this->session));
121+
}
122+
123+
private function sessionMustNotBeSet()
124+
{
125+
$this->request->expects($this->never())
126+
->method('setSession');
127+
}
128+
129+
private function getSession()
130+
{
131+
$mock = $this->getMockBuilder(SessionInterface::class)
132+
->disableOriginalConstructor()
133+
->getMock();
134+
135+
return $mock;
136+
}
137+
}

0 commit comments

Comments
 (0)