菜谱项目

MemcacheSessionHandlerTest.php 3.4KB

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