MemcachedSessionHandlerTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\MemcachedSessionHandler;
  13. /**
  14. * @requires extension memcached
  15. *
  16. * @group time-sensitive
  17. */
  18. class MemcachedSessionHandlerTest extends TestCase
  19. {
  20. private const PREFIX = 'prefix_';
  21. private const TTL = 1000;
  22. /**
  23. * @var MemcachedSessionHandler
  24. */
  25. protected $storage;
  26. protected $memcached;
  27. protected function setUp(): void
  28. {
  29. parent::setUp();
  30. if (version_compare(phpversion('memcached'), '2.2.0', '>=') && version_compare(phpversion('memcached'), '3.0.0b1', '<')) {
  31. $this->markTestSkipped('Tests can only be run with memcached extension 2.1.0 or lower, or 3.0.0b1 or higher');
  32. }
  33. $r = new \ReflectionClass(\Memcached::class);
  34. $methodsToMock = array_map(function ($m) { return $m->name; }, $r->getMethods(\ReflectionMethod::IS_PUBLIC));
  35. $methodsToMock = array_diff($methodsToMock, ['getDelayed', 'getDelayedByKey']);
  36. $this->memcached = $this->getMockBuilder(\Memcached::class)
  37. ->disableOriginalConstructor()
  38. ->onlyMethods($methodsToMock)
  39. ->getMock();
  40. $this->storage = new MemcachedSessionHandler(
  41. $this->memcached,
  42. ['prefix' => self::PREFIX, 'expiretime' => self::TTL]
  43. );
  44. }
  45. protected function tearDown(): void
  46. {
  47. $this->memcached = null;
  48. $this->storage = null;
  49. parent::tearDown();
  50. }
  51. public function testOpenSession()
  52. {
  53. $this->assertTrue($this->storage->open('', ''));
  54. }
  55. public function testCloseSession()
  56. {
  57. $this->memcached
  58. ->expects($this->once())
  59. ->method('quit')
  60. ->willReturn(true)
  61. ;
  62. $this->assertTrue($this->storage->close());
  63. }
  64. public function testReadSession()
  65. {
  66. $this->memcached
  67. ->expects($this->once())
  68. ->method('get')
  69. ->with(self::PREFIX.'id')
  70. ;
  71. $this->assertEquals('', $this->storage->read('id'));
  72. }
  73. public function testWriteSession()
  74. {
  75. $this->memcached
  76. ->expects($this->once())
  77. ->method('set')
  78. ->with(self::PREFIX.'id', 'data', $this->equalTo(self::TTL, 2))
  79. ->willReturn(true)
  80. ;
  81. $this->assertTrue($this->storage->write('id', 'data'));
  82. }
  83. public function testWriteSessionWithLargeTTL()
  84. {
  85. $this->memcached
  86. ->expects($this->once())
  87. ->method('set')
  88. ->with(self::PREFIX.'id', 'data', $this->equalTo(time() + self::TTL + 60 * 60 * 24 * 30, 2))
  89. ->willReturn(true)
  90. ;
  91. $storage = new MemcachedSessionHandler(
  92. $this->memcached,
  93. ['prefix' => self::PREFIX, 'expiretime' => self::TTL + 60 * 60 * 24 * 30]
  94. );
  95. $this->assertTrue($storage->write('id', 'data'));
  96. }
  97. public function testDestroySession()
  98. {
  99. $this->memcached
  100. ->expects($this->once())
  101. ->method('delete')
  102. ->with(self::PREFIX.'id')
  103. ->willReturn(true)
  104. ;
  105. $this->assertTrue($this->storage->destroy('id'));
  106. }
  107. public function testGcSession()
  108. {
  109. $this->assertIsInt($this->storage->gc(123));
  110. }
  111. /**
  112. * @dataProvider getOptionFixtures
  113. */
  114. public function testSupportedOptions($options, $supported)
  115. {
  116. try {
  117. new MemcachedSessionHandler($this->memcached, $options);
  118. $this->assertTrue($supported);
  119. } catch (\InvalidArgumentException $e) {
  120. $this->assertFalse($supported);
  121. }
  122. }
  123. public static function getOptionFixtures()
  124. {
  125. return [
  126. [['prefix' => 'session'], true],
  127. [['expiretime' => 100], true],
  128. [['prefix' => 'session', 'expiretime' => 200], true],
  129. [['expiretime' => 100, 'foo' => 'bar'], false],
  130. ];
  131. }
  132. public function testGetConnection()
  133. {
  134. $method = new \ReflectionMethod($this->storage, 'getMemcached');
  135. $method->setAccessible(true);
  136. $this->assertInstanceOf(\Memcached::class, $method->invoke($this->storage));
  137. }
  138. }