1<?php 2 3/* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler; 13 14use PHPUnit\Framework\TestCase; 15use Symfony\Component\HttpFoundation\Session\Storage\Handler\AbstractSessionHandler; 16use Symfony\Component\HttpFoundation\Session\Storage\Handler\StrictSessionHandler; 17 18class StrictSessionHandlerTest extends TestCase 19{ 20 public function testOpen() 21 { 22 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock(); 23 $handler->expects($this->once())->method('open') 24 ->with('path', 'name')->willReturn(true); 25 $proxy = new StrictSessionHandler($handler); 26 27 $this->assertInstanceOf('SessionUpdateTimestampHandlerInterface', $proxy); 28 $this->assertInstanceOf(AbstractSessionHandler::class, $proxy); 29 $this->assertTrue($proxy->open('path', 'name')); 30 } 31 32 public function testCloseSession() 33 { 34 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock(); 35 $handler->expects($this->once())->method('close') 36 ->willReturn(true); 37 $proxy = new StrictSessionHandler($handler); 38 39 $this->assertTrue($proxy->close()); 40 } 41 42 public function testValidateIdOK() 43 { 44 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock(); 45 $handler->expects($this->once())->method('read') 46 ->with('id')->willReturn('data'); 47 $proxy = new StrictSessionHandler($handler); 48 49 $this->assertTrue($proxy->validateId('id')); 50 } 51 52 public function testValidateIdKO() 53 { 54 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock(); 55 $handler->expects($this->once())->method('read') 56 ->with('id')->willReturn(''); 57 $proxy = new StrictSessionHandler($handler); 58 59 $this->assertFalse($proxy->validateId('id')); 60 } 61 62 public function testRead() 63 { 64 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock(); 65 $handler->expects($this->once())->method('read') 66 ->with('id')->willReturn('data'); 67 $proxy = new StrictSessionHandler($handler); 68 69 $this->assertSame('data', $proxy->read('id')); 70 } 71 72 public function testReadWithValidateIdOK() 73 { 74 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock(); 75 $handler->expects($this->once())->method('read') 76 ->with('id')->willReturn('data'); 77 $proxy = new StrictSessionHandler($handler); 78 79 $this->assertTrue($proxy->validateId('id')); 80 $this->assertSame('data', $proxy->read('id')); 81 } 82 83 public function testReadWithValidateIdMismatch() 84 { 85 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock(); 86 $handler->expects($this->exactly(2))->method('read') 87 ->withConsecutive(['id1'], ['id2']) 88 ->will($this->onConsecutiveCalls('data1', 'data2')); 89 $proxy = new StrictSessionHandler($handler); 90 91 $this->assertTrue($proxy->validateId('id1')); 92 $this->assertSame('data2', $proxy->read('id2')); 93 } 94 95 public function testUpdateTimestamp() 96 { 97 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock(); 98 $handler->expects($this->once())->method('write') 99 ->with('id', 'data')->willReturn(true); 100 $proxy = new StrictSessionHandler($handler); 101 102 $this->assertTrue($proxy->updateTimestamp('id', 'data')); 103 } 104 105 public function testWrite() 106 { 107 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock(); 108 $handler->expects($this->once())->method('write') 109 ->with('id', 'data')->willReturn(true); 110 $proxy = new StrictSessionHandler($handler); 111 112 $this->assertTrue($proxy->write('id', 'data')); 113 } 114 115 public function testWriteEmptyNewSession() 116 { 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); 123 124 $this->assertFalse($proxy->validateId('id')); 125 $this->assertSame('', $proxy->read('id')); 126 $this->assertTrue($proxy->write('id', '')); 127 } 128 129 public function testWriteEmptyExistingSession() 130 { 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); 137 138 $this->assertSame('data', $proxy->read('id')); 139 $this->assertTrue($proxy->write('id', '')); 140 } 141 142 public function testDestroy() 143 { 144 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock(); 145 $handler->expects($this->once())->method('destroy') 146 ->with('id')->willReturn(true); 147 $proxy = new StrictSessionHandler($handler); 148 149 $this->assertTrue($proxy->destroy('id')); 150 } 151 152 public function testDestroyNewSession() 153 { 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); 159 160 $this->assertSame('', $proxy->read('id')); 161 $this->assertTrue($proxy->destroy('id')); 162 } 163 164 public function testDestroyNonEmptyNewSession() 165 { 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); 174 175 $this->assertSame('', $proxy->read('id')); 176 $this->assertTrue($proxy->write('id', 'data')); 177 $this->assertTrue($proxy->destroy('id')); 178 } 179 180 public function testGc() 181 { 182 $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock(); 183 $handler->expects($this->once())->method('gc') 184 ->with(123)->willReturn(true); 185 $proxy = new StrictSessionHandler($handler); 186 187 $this->assertTrue($proxy->gc(123)); 188 } 189} 190