/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/AbstractRedisSessionHandlerTestCase.php

https://github.com/pulzarraider/symfony · PHP · 145 lines · 92 code · 29 blank · 24 comment · 1 complexity · 40233ebeebc810df61b925f7ab6efac4 MD5 · raw file

  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\RedisSessionHandler;
  13. /**
  14. * @requires extension redis
  15. * @group time-sensitive
  16. */
  17. abstract class AbstractRedisSessionHandlerTestCase extends TestCase
  18. {
  19. protected const PREFIX = 'prefix_';
  20. /**
  21. * @var RedisSessionHandler
  22. */
  23. protected $storage;
  24. /**
  25. * @var \Redis|\RedisArray|\RedisCluster|\Predis\Client
  26. */
  27. protected $redisClient;
  28. /**
  29. * @return \Redis|\RedisArray|\RedisCluster|\Predis\Client
  30. */
  31. abstract protected function createRedisClient(string $host);
  32. protected function setUp()
  33. {
  34. parent::setUp();
  35. if (!\extension_loaded('redis')) {
  36. self::markTestSkipped('Extension redis required.');
  37. }
  38. $host = getenv('REDIS_HOST') ?: 'localhost';
  39. $this->redisClient = $this->createRedisClient($host);
  40. $this->storage = new RedisSessionHandler(
  41. $this->redisClient,
  42. ['prefix' => self::PREFIX]
  43. );
  44. }
  45. protected function tearDown()
  46. {
  47. $this->redisClient = 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->assertTrue($this->storage->close());
  58. }
  59. public function testReadSession()
  60. {
  61. $this->redisClient->set(self::PREFIX.'id1', null);
  62. $this->redisClient->set(self::PREFIX.'id2', 'abc123');
  63. $this->assertEquals('', $this->storage->read('id1'));
  64. $this->assertEquals('abc123', $this->storage->read('id2'));
  65. }
  66. public function testWriteSession()
  67. {
  68. $this->assertTrue($this->storage->write('id', 'data'));
  69. $this->assertTrue((bool) $this->redisClient->exists(self::PREFIX.'id'));
  70. $this->assertEquals('data', $this->redisClient->get(self::PREFIX.'id'));
  71. }
  72. public function testUseSessionGcMaxLifetimeAsTimeToLive()
  73. {
  74. $this->storage->write('id', 'data');
  75. $ttl = $this->redisClient->ttl(self::PREFIX.'id');
  76. $this->assertLessThanOrEqual(ini_get('session.gc_maxlifetime'), $ttl);
  77. $this->assertGreaterThanOrEqual(0, $ttl);
  78. }
  79. public function testDestroySession()
  80. {
  81. $this->redisClient->set(self::PREFIX.'id', 'foo');
  82. $this->assertTrue((bool) $this->redisClient->exists(self::PREFIX.'id'));
  83. $this->assertTrue($this->storage->destroy('id'));
  84. $this->assertFalse((bool) $this->redisClient->exists(self::PREFIX.'id'));
  85. }
  86. public function testGcSession()
  87. {
  88. $this->assertTrue($this->storage->gc(123));
  89. }
  90. public function testUpdateTimestamp()
  91. {
  92. $lowTtl = 10;
  93. $this->redisClient->setex(self::PREFIX.'id', $lowTtl, 'foo');
  94. $this->storage->updateTimestamp('id', []);
  95. $this->assertGreaterThan($lowTtl, $this->redisClient->ttl(self::PREFIX.'id'));
  96. }
  97. /**
  98. * @dataProvider getOptionFixtures
  99. */
  100. public function testSupportedParam(array $options, bool $supported)
  101. {
  102. try {
  103. new RedisSessionHandler($this->redisClient, $options);
  104. $this->assertTrue($supported);
  105. } catch (\InvalidArgumentException $e) {
  106. $this->assertFalse($supported);
  107. }
  108. }
  109. public function getOptionFixtures(): array
  110. {
  111. return [
  112. [['prefix' => 'session'], true],
  113. [['prefix' => 'sfs', 'foo' => 'bar'], false],
  114. ];
  115. }
  116. }