MemcacheSessionHandlerTest.php 3.4 KB

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