4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Session\Storage\Handler\AbstractSessionHandler;
16 use Symfony\Component\HttpFoundation\Session\Storage\Handler\StrictSessionHandler;
18 class StrictSessionHandlerTest extends TestCase
20 public function testOpen()
22 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
23 $handler->expects($this->once())->method('open')
24 ->with('path', 'name')->willReturn(true);
25 $proxy = new StrictSessionHandler($handler);
27 $this->assertInstanceOf('SessionUpdateTimestampHandlerInterface', $proxy);
28 $this->assertInstanceOf(AbstractSessionHandler::class, $proxy);
29 $this->assertTrue($proxy->open('path', 'name'));
32 public function testCloseSession()
34 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
35 $handler->expects($this->once())->method('close')
37 $proxy = new StrictSessionHandler($handler);
39 $this->assertTrue($proxy->close());
42 public function testValidateIdOK()
44 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
45 $handler->expects($this->once())->method('read')
46 ->with('id')->willReturn('data');
47 $proxy = new StrictSessionHandler($handler);
49 $this->assertTrue($proxy->validateId('id'));
52 public function testValidateIdKO()
54 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
55 $handler->expects($this->once())->method('read')
56 ->with('id')->willReturn('');
57 $proxy = new StrictSessionHandler($handler);
59 $this->assertFalse($proxy->validateId('id'));
62 public function testRead()
64 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
65 $handler->expects($this->once())->method('read')
66 ->with('id')->willReturn('data');
67 $proxy = new StrictSessionHandler($handler);
69 $this->assertSame('data', $proxy->read('id'));
72 public function testReadWithValidateIdOK()
74 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
75 $handler->expects($this->once())->method('read')
76 ->with('id')->willReturn('data');
77 $proxy = new StrictSessionHandler($handler);
79 $this->assertTrue($proxy->validateId('id'));
80 $this->assertSame('data', $proxy->read('id'));
83 public function testReadWithValidateIdMismatch()
85 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
86 $handler->expects($this->exactly(2))->method('read')
87 ->withConsecutive(array('id1'), array('id2'))
88 ->will($this->onConsecutiveCalls('data1', 'data2'));
89 $proxy = new StrictSessionHandler($handler);
91 $this->assertTrue($proxy->validateId('id1'));
92 $this->assertSame('data2', $proxy->read('id2'));
95 public function testUpdateTimestamp()
97 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
98 $handler->expects($this->once())->method('write')
99 ->with('id', 'data')->willReturn(true);
100 $proxy = new StrictSessionHandler($handler);
102 $this->assertTrue($proxy->updateTimestamp('id', 'data'));
105 public function testWrite()
107 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
108 $handler->expects($this->once())->method('write')
109 ->with('id', 'data')->willReturn(true);
110 $proxy = new StrictSessionHandler($handler);
112 $this->assertTrue($proxy->write('id', 'data'));
115 public function testWriteEmptyNewSession()
117 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
118 $handler->expects($this->once())->method('read')
119 ->with('id')->willReturn('');
120 $handler->expects($this->never())->method('write');
121 $handler->expects($this->once())->method('destroy')->willReturn(true);
122 $proxy = new StrictSessionHandler($handler);
124 $this->assertFalse($proxy->validateId('id'));
125 $this->assertSame('', $proxy->read('id'));
126 $this->assertTrue($proxy->write('id', ''));
129 public function testWriteEmptyExistingSession()
131 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
132 $handler->expects($this->once())->method('read')
133 ->with('id')->willReturn('data');
134 $handler->expects($this->never())->method('write');
135 $handler->expects($this->once())->method('destroy')->willReturn(true);
136 $proxy = new StrictSessionHandler($handler);
138 $this->assertSame('data', $proxy->read('id'));
139 $this->assertTrue($proxy->write('id', ''));
142 public function testDestroy()
144 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
145 $handler->expects($this->once())->method('destroy')
146 ->with('id')->willReturn(true);
147 $proxy = new StrictSessionHandler($handler);
149 $this->assertTrue($proxy->destroy('id'));
152 public function testDestroyNewSession()
154 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
155 $handler->expects($this->once())->method('read')
156 ->with('id')->willReturn('');
157 $handler->expects($this->once())->method('destroy')->willReturn(true);
158 $proxy = new StrictSessionHandler($handler);
160 $this->assertSame('', $proxy->read('id'));
161 $this->assertTrue($proxy->destroy('id'));
164 public function testDestroyNonEmptyNewSession()
166 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
167 $handler->expects($this->once())->method('read')
168 ->with('id')->willReturn('');
169 $handler->expects($this->once())->method('write')
170 ->with('id', 'data')->willReturn(true);
171 $handler->expects($this->once())->method('destroy')
172 ->with('id')->willReturn(true);
173 $proxy = new StrictSessionHandler($handler);
175 $this->assertSame('', $proxy->read('id'));
176 $this->assertTrue($proxy->write('id', 'data'));
177 $this->assertTrue($proxy->destroy('id'));
180 public function testGc()
182 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
183 $handler->expects($this->once())->method('gc')
184 ->with(123)->willReturn(true);
185 $proxy = new StrictSessionHandler($handler);
187 $this->assertTrue($proxy->gc(123));