No Description

MemcacheSessionHandlerTest.php 3.4KB

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