No Description

MemcachedSessionHandlerTest.php 3.7KB

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