StrictSessionHandlerTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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->getMockBuilder('SessionHandlerInterface')->getMock();
  19. $handler->expects($this->once())->method('open')
  20. ->with('path', 'name')->willReturn(true);
  21. $proxy = new StrictSessionHandler($handler);
  22. $this->assertInstanceOf('SessionUpdateTimestampHandlerInterface', $proxy);
  23. $this->assertInstanceOf(AbstractSessionHandler::class, $proxy);
  24. $this->assertTrue($proxy->open('path', 'name'));
  25. }
  26. public function testCloseSession()
  27. {
  28. $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
  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->getMockBuilder('SessionHandlerInterface')->getMock();
  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->getMockBuilder('SessionHandlerInterface')->getMock();
  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->getMockBuilder('SessionHandlerInterface')->getMock();
  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->getMockBuilder('SessionHandlerInterface')->getMock();
  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->getMockBuilder('SessionHandlerInterface')->getMock();
  70. $handler->expects($this->exactly(2))->method('read')
  71. ->withConsecutive(['id1'], ['id2'])
  72. ->will($this->onConsecutiveCalls('data1', 'data2'));
  73. $proxy = new StrictSessionHandler($handler);
  74. $this->assertTrue($proxy->validateId('id1'));
  75. $this->assertSame('data2', $proxy->read('id2'));
  76. }
  77. public function testUpdateTimestamp()
  78. {
  79. $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
  80. $handler->expects($this->once())->method('write')
  81. ->with('id', 'data')->willReturn(true);
  82. $proxy = new StrictSessionHandler($handler);
  83. $this->assertTrue($proxy->updateTimestamp('id', 'data'));
  84. }
  85. public function testWrite()
  86. {
  87. $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
  88. $handler->expects($this->once())->method('write')
  89. ->with('id', 'data')->willReturn(true);
  90. $proxy = new StrictSessionHandler($handler);
  91. $this->assertTrue($proxy->write('id', 'data'));
  92. }
  93. public function testWriteEmptyNewSession()
  94. {
  95. $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
  96. $handler->expects($this->once())->method('read')
  97. ->with('id')->willReturn('');
  98. $handler->expects($this->never())->method('write');
  99. $handler->expects($this->once())->method('destroy')->willReturn(true);
  100. $proxy = new StrictSessionHandler($handler);
  101. $this->assertFalse($proxy->validateId('id'));
  102. $this->assertSame('', $proxy->read('id'));
  103. $this->assertTrue($proxy->write('id', ''));
  104. }
  105. public function testWriteEmptyExistingSession()
  106. {
  107. $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
  108. $handler->expects($this->once())->method('read')
  109. ->with('id')->willReturn('data');
  110. $handler->expects($this->never())->method('write');
  111. $handler->expects($this->once())->method('destroy')->willReturn(true);
  112. $proxy = new StrictSessionHandler($handler);
  113. $this->assertSame('data', $proxy->read('id'));
  114. $this->assertTrue($proxy->write('id', ''));
  115. }
  116. public function testDestroy()
  117. {
  118. $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
  119. $handler->expects($this->once())->method('destroy')
  120. ->with('id')->willReturn(true);
  121. $proxy = new StrictSessionHandler($handler);
  122. $this->assertTrue($proxy->destroy('id'));
  123. }
  124. public function testDestroyNewSession()
  125. {
  126. $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
  127. $handler->expects($this->once())->method('read')
  128. ->with('id')->willReturn('');
  129. $handler->expects($this->once())->method('destroy')->willReturn(true);
  130. $proxy = new StrictSessionHandler($handler);
  131. $this->assertSame('', $proxy->read('id'));
  132. $this->assertTrue($proxy->destroy('id'));
  133. }
  134. public function testDestroyNonEmptyNewSession()
  135. {
  136. $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
  137. $handler->expects($this->once())->method('read')
  138. ->with('id')->willReturn('');
  139. $handler->expects($this->once())->method('write')
  140. ->with('id', 'data')->willReturn(true);
  141. $handler->expects($this->once())->method('destroy')
  142. ->with('id')->willReturn(true);
  143. $proxy = new StrictSessionHandler($handler);
  144. $this->assertSame('', $proxy->read('id'));
  145. $this->assertTrue($proxy->write('id', 'data'));
  146. $this->assertTrue($proxy->destroy('id'));
  147. }
  148. public function testGc()
  149. {
  150. $handler = $this->getMockBuilder('SessionHandlerInterface')->getMock();
  151. $handler->expects($this->once())->method('gc')
  152. ->with(123)->willReturn(true);
  153. $proxy = new StrictSessionHandler($handler);
  154. $this->assertTrue($proxy->gc(123));
  155. }
  156. }