MemcachedSessionHandlerTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. * @group time-sensitive
  16. */
  17. class MemcachedSessionHandlerTest extends TestCase
  18. {
  19. const PREFIX = 'prefix_';
  20. const TTL = 1000;
  21. /**
  22. * @var MemcachedSessionHandler
  23. */
  24. protected $storage;
  25. protected $memcached;
  26. protected function setUp()
  27. {
  28. if (\defined('HHVM_VERSION')) {
  29. $this->markTestSkipped('PHPUnit_MockObject cannot mock the Memcached class on HHVM. See https://github.com/sebastianbergmann/phpunit-mock-objects/pull/289');
  30. }
  31. parent::setUp();
  32. if (version_compare(phpversion('memcached'), '2.2.0', '>=') && version_compare(phpversion('memcached'), '3.0.0b1', '<')) {
  33. $this->markTestSkipped('Tests can only be run with memcached extension 2.1.0 or lower, or 3.0.0b1 or higher');
  34. }
  35. $this->memcached = $this->getMockBuilder('Memcached')->getMock();
  36. $this->storage = new MemcachedSessionHandler(
  37. $this->memcached,
  38. ['prefix' => self::PREFIX, 'expiretime' => self::TTL]
  39. );
  40. }
  41. protected function tearDown()
  42. {
  43. $this->memcached = null;
  44. $this->storage = null;
  45. parent::tearDown();
  46. }
  47. public function testOpenSession()
  48. {
  49. $this->assertTrue($this->storage->open('', ''));
  50. }
  51. public function testCloseSession()
  52. {
  53. $this->memcached
  54. ->expects($this->once())
  55. ->method('quit')
  56. ->will($this->returnValue(true))
  57. ;
  58. $this->assertTrue($this->storage->close());
  59. }
  60. public function testReadSession()
  61. {
  62. $this->memcached
  63. ->expects($this->once())
  64. ->method('get')
  65. ->with(self::PREFIX.'id')
  66. ;
  67. $this->assertEquals('', $this->storage->read('id'));
  68. }
  69. public function testWriteSession()
  70. {
  71. $this->memcached
  72. ->expects($this->once())
  73. ->method('set')
  74. ->with(self::PREFIX.'id', 'data', $this->equalTo(time() + self::TTL, 2))
  75. ->will($this->returnValue(true))
  76. ;
  77. $this->assertTrue($this->storage->write('id', 'data'));
  78. }
  79. public function testDestroySession()
  80. {
  81. $this->memcached
  82. ->expects($this->once())
  83. ->method('delete')
  84. ->with(self::PREFIX.'id')
  85. ->will($this->returnValue(true))
  86. ;
  87. $this->assertTrue($this->storage->destroy('id'));
  88. }
  89. public function testGcSession()
  90. {
  91. $this->assertTrue($this->storage->gc(123));
  92. }
  93. /**
  94. * @dataProvider getOptionFixtures
  95. */
  96. public function testSupportedOptions($options, $supported)
  97. {
  98. try {
  99. new MemcachedSessionHandler($this->memcached, $options);
  100. $this->assertTrue($supported);
  101. } catch (\InvalidArgumentException $e) {
  102. $this->assertFalse($supported);
  103. }
  104. }
  105. public function getOptionFixtures()
  106. {
  107. return [
  108. [['prefix' => 'session'], true],
  109. [['expiretime' => 100], true],
  110. [['prefix' => 'session', 'expiretime' => 200], true],
  111. [['expiretime' => 100, 'foo' => 'bar'], false],
  112. ];
  113. }
  114. public function testGetConnection()
  115. {
  116. $method = new \ReflectionMethod($this->storage, 'getMemcached');
  117. $method->setAccessible(true);
  118. $this->assertInstanceOf('\Memcached', $method->invoke($this->storage));
  119. }
  120. }