SessionHandlerProxyTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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\Proxy;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Session\Storage\Handler\StrictSessionHandler;
  13. use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
  14. use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
  15. /**
  16. * Tests for SessionHandlerProxy class.
  17. *
  18. * @author Drak <drak@zikula.org>
  19. *
  20. * @runTestsInSeparateProcesses
  21. *
  22. * @preserveGlobalState disabled
  23. */
  24. class SessionHandlerProxyTest extends TestCase
  25. {
  26. /**
  27. * @var \PHPUnit\Framework\MockObject\Matcher
  28. */
  29. private $mock;
  30. /**
  31. * @var SessionHandlerProxy
  32. */
  33. private $proxy;
  34. protected function setUp(): void
  35. {
  36. $this->mock = $this->createMock(\SessionHandlerInterface::class);
  37. $this->proxy = new SessionHandlerProxy($this->mock);
  38. }
  39. protected function tearDown(): void
  40. {
  41. $this->mock = null;
  42. $this->proxy = null;
  43. }
  44. public function testOpenTrue()
  45. {
  46. $this->mock->expects($this->once())
  47. ->method('open')
  48. ->willReturn(true);
  49. $this->assertFalse($this->proxy->isActive());
  50. $this->proxy->open('name', 'id');
  51. $this->assertFalse($this->proxy->isActive());
  52. }
  53. public function testOpenFalse()
  54. {
  55. $this->mock->expects($this->once())
  56. ->method('open')
  57. ->willReturn(false);
  58. $this->assertFalse($this->proxy->isActive());
  59. $this->proxy->open('name', 'id');
  60. $this->assertFalse($this->proxy->isActive());
  61. }
  62. public function testClose()
  63. {
  64. $this->mock->expects($this->once())
  65. ->method('close')
  66. ->willReturn(true);
  67. $this->assertFalse($this->proxy->isActive());
  68. $this->proxy->close();
  69. $this->assertFalse($this->proxy->isActive());
  70. }
  71. public function testCloseFalse()
  72. {
  73. $this->mock->expects($this->once())
  74. ->method('close')
  75. ->willReturn(false);
  76. $this->assertFalse($this->proxy->isActive());
  77. $this->proxy->close();
  78. $this->assertFalse($this->proxy->isActive());
  79. }
  80. public function testRead()
  81. {
  82. $this->mock->expects($this->once())
  83. ->method('read')
  84. ->willReturn('foo')
  85. ;
  86. $this->proxy->read('id');
  87. }
  88. public function testWrite()
  89. {
  90. $this->mock->expects($this->once())
  91. ->method('write')
  92. ->willReturn(true)
  93. ;
  94. $this->assertTrue($this->proxy->write('id', 'data'));
  95. }
  96. public function testDestroy()
  97. {
  98. $this->mock->expects($this->once())
  99. ->method('destroy')
  100. ->willReturn(true)
  101. ;
  102. $this->assertTrue($this->proxy->destroy('id'));
  103. }
  104. public function testGc()
  105. {
  106. $this->mock->expects($this->once())
  107. ->method('gc')
  108. ->willReturn(1)
  109. ;
  110. $this->proxy->gc(86400);
  111. }
  112. public function testValidateId()
  113. {
  114. $mock = $this->createMock(TestSessionHandler::class);
  115. $mock->expects($this->once())
  116. ->method('validateId');
  117. $proxy = new SessionHandlerProxy($mock);
  118. $proxy->validateId('id');
  119. $this->assertTrue($this->proxy->validateId('id'));
  120. }
  121. public function testUpdateTimestamp()
  122. {
  123. $mock = $this->createMock(TestSessionHandler::class);
  124. $mock->expects($this->once())
  125. ->method('updateTimestamp')
  126. ->willReturn(false);
  127. $proxy = new SessionHandlerProxy($mock);
  128. $proxy->updateTimestamp('id', 'data');
  129. $this->mock->expects($this->once())
  130. ->method('write')
  131. ->willReturn(true)
  132. ;
  133. $this->proxy->updateTimestamp('id', 'data');
  134. }
  135. /**
  136. * @dataProvider provideNativeSessionStorageHandler
  137. */
  138. public function testNativeSessionStorageSaveHandlerName($handler)
  139. {
  140. $this->assertSame('files', (new NativeSessionStorage([], $handler))->getSaveHandler()->getSaveHandlerName());
  141. }
  142. public static function provideNativeSessionStorageHandler()
  143. {
  144. return [
  145. [new \SessionHandler()],
  146. [new StrictSessionHandler(new \SessionHandler())],
  147. [new SessionHandlerProxy(new StrictSessionHandler(new \SessionHandler()))],
  148. ];
  149. }
  150. }
  151. abstract class TestSessionHandler implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface
  152. {
  153. }