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\MemcacheSessionHandler;
18 * @requires extension memcache
19 * @group time-sensitive
21 class MemcacheSessionHandlerTest extends TestCase
23 const PREFIX = 'prefix_';
26 * @var MemcacheSessionHandler
32 protected function setUp()
34 if (defined('HHVM_VERSION')) {
35 $this->markTestSkipped('PHPUnit_MockObject cannot mock the Memcache class on HHVM. See https://github.com/sebastianbergmann/phpunit-mock-objects/pull/289');
39 $this->memcache = $this->getMockBuilder('Memcache')->getMock();
40 $this->storage = new MemcacheSessionHandler(
42 array('prefix' => self::PREFIX, 'expiretime' => self::TTL)
46 protected function tearDown()
48 $this->memcache = null;
49 $this->storage = null;
53 public function testOpenSession()
55 $this->assertTrue($this->storage->open('', ''));
58 public function testCloseSession()
60 $this->assertTrue($this->storage->close());
63 public function testReadSession()
66 ->expects($this->once())
68 ->with(self::PREFIX.'id')
71 $this->assertEquals('', $this->storage->read('id'));
74 public function testWriteSession()
77 ->expects($this->once())
79 ->with(self::PREFIX.'id', 'data', 0, $this->equalTo(time() + self::TTL, 2))
80 ->will($this->returnValue(true))
83 $this->assertTrue($this->storage->write('id', 'data'));
86 public function testDestroySession()
89 ->expects($this->once())
91 ->with(self::PREFIX.'id')
92 ->will($this->returnValue(true))
95 $this->assertTrue($this->storage->destroy('id'));
98 public function testGcSession()
100 $this->assertTrue($this->storage->gc(123));
104 * @dataProvider getOptionFixtures
106 public function testSupportedOptions($options, $supported)
109 new MemcacheSessionHandler($this->memcache, $options);
110 $this->assertTrue($supported);
111 } catch (\InvalidArgumentException $e) {
112 $this->assertFalse($supported);
116 public function getOptionFixtures()
119 array(array('prefix' => 'session'), true),
120 array(array('expiretime' => 100), true),
121 array(array('prefix' => 'session', 'expiretime' => 200), true),
122 array(array('expiretime' => 100, 'foo' => 'bar'), false),
126 public function testGetConnection()
128 $method = new \ReflectionMethod($this->storage, 'getMemcache');
129 $method->setAccessible(true);
131 $this->assertInstanceOf('\Memcache', $method->invoke($this->storage));