4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcachedSessionHandler;
18 * @requires extension memcached
19 * @group time-sensitive
21 class MemcachedSessionHandlerTest extends TestCase
23 const PREFIX = 'prefix_';
27 * @var MemcachedSessionHandler
33 protected function setUp()
35 if (defined('HHVM_VERSION')) {
36 $this->markTestSkipped('PHPUnit_MockObject cannot mock the Memcached class on HHVM. See https://github.com/sebastianbergmann/phpunit-mock-objects/pull/289');
41 if (version_compare(phpversion('memcached'), '2.2.0', '>=') && version_compare(phpversion('memcached'), '3.0.0b1', '<')) {
42 $this->markTestSkipped('Tests can only be run with memcached extension 2.1.0 or lower, or 3.0.0b1 or higher');
45 $this->memcached = $this->getMockBuilder('Memcached')->getMock();
46 $this->storage = new MemcachedSessionHandler(
48 array('prefix' => self::PREFIX, 'expiretime' => self::TTL)
52 protected function tearDown()
54 $this->memcached = null;
55 $this->storage = null;
59 public function testOpenSession()
61 $this->assertTrue($this->storage->open('', ''));
64 public function testCloseSession()
66 $this->assertTrue($this->storage->close());
69 public function testReadSession()
72 ->expects($this->once())
74 ->with(self::PREFIX.'id')
77 $this->assertEquals('', $this->storage->read('id'));
80 public function testWriteSession()
83 ->expects($this->once())
85 ->with(self::PREFIX.'id', 'data', $this->equalTo(time() + self::TTL, 2))
86 ->will($this->returnValue(true))
89 $this->assertTrue($this->storage->write('id', 'data'));
92 public function testDestroySession()
95 ->expects($this->once())
97 ->with(self::PREFIX.'id')
98 ->will($this->returnValue(true))
101 $this->assertTrue($this->storage->destroy('id'));
104 public function testGcSession()
106 $this->assertTrue($this->storage->gc(123));
110 * @dataProvider getOptionFixtures
112 public function testSupportedOptions($options, $supported)
115 new MemcachedSessionHandler($this->memcached, $options);
116 $this->assertTrue($supported);
117 } catch (\InvalidArgumentException $e) {
118 $this->assertFalse($supported);
122 public function getOptionFixtures()
125 array(array('prefix' => 'session'), true),
126 array(array('expiretime' => 100), true),
127 array(array('prefix' => 'session', 'expiretime' => 200), true),
128 array(array('expiretime' => 100, 'foo' => 'bar'), false),
132 public function testGetConnection()
134 $method = new \ReflectionMethod($this->storage, 'getMemcached');
135 $method->setAccessible(true);
137 $this->assertInstanceOf('\Memcached', $method->invoke($this->storage));