MemcachedSessionHandlerTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. array('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->assertTrue($this->storage->close());
  54. }
  55. public function testReadSession()
  56. {
  57. $this->memcached
  58. ->expects($this->once())
  59. ->method('get')
  60. ->with(self::PREFIX.'id')
  61. ;
  62. $this->assertEquals('', $this->storage->read('id'));
  63. }
  64. public function testWriteSession()
  65. {
  66. $this->memcached
  67. ->expects($this->once())
  68. ->method('set')
  69. ->with(self::PREFIX.'id', 'data', $this->equalTo(time() + self::TTL, 2))
  70. ->will($this->returnValue(true))
  71. ;
  72. $this->assertTrue($this->storage->write('id', 'data'));
  73. }
  74. public function testDestroySession()
  75. {
  76. $this->memcached
  77. ->expects($this->once())
  78. ->method('delete')
  79. ->with(self::PREFIX.'id')
  80. ->will($this->returnValue(true))
  81. ;
  82. $this->assertTrue($this->storage->destroy('id'));
  83. }
  84. public function testGcSession()
  85. {
  86. $this->assertTrue($this->storage->gc(123));
  87. }
  88. /**
  89. * @dataProvider getOptionFixtures
  90. */
  91. public function testSupportedOptions($options, $supported)
  92. {
  93. try {
  94. new MemcachedSessionHandler($this->memcached, $options);
  95. $this->assertTrue($supported);
  96. } catch (\InvalidArgumentException $e) {
  97. $this->assertFalse($supported);
  98. }
  99. }
  100. public function getOptionFixtures()
  101. {
  102. return array(
  103. array(array('prefix' => 'session'), true),
  104. array(array('expiretime' => 100), true),
  105. array(array('prefix' => 'session', 'expiretime' => 200), true),
  106. array(array('expiretime' => 100, 'foo' => 'bar'), false),
  107. );
  108. }
  109. public function testGetConnection()
  110. {
  111. $method = new \ReflectionMethod($this->storage, 'getMemcached');
  112. $method->setAccessible(true);
  113. $this->assertInstanceOf('\Memcached', $method->invoke($this->storage));
  114. }
  115. }