StrictSessionHandlerTest.php 6.9 KB

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