Open Monograph Press  3.3.0
MemcacheSessionHandlerTest.php
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
13 
14 use PHPUnit\Framework\TestCase;
16 
21 class MemcacheSessionHandlerTest extends TestCase
22 {
23  const PREFIX = 'prefix_';
24  const TTL = 1000;
28  protected $storage;
29 
30  protected $memcache;
31 
32  protected function setUp()
33  {
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');
36  }
37 
38  parent::setUp();
39  $this->memcache = $this->getMockBuilder('Memcache')->getMock();
40  $this->storage = new MemcacheSessionHandler(
41  $this->memcache,
42  array('prefix' => self::PREFIX, 'expiretime' => self::TTL)
43  );
44  }
45 
46  protected function tearDown()
47  {
48  $this->memcache = null;
49  $this->storage = null;
50  parent::tearDown();
51  }
52 
53  public function testOpenSession()
54  {
55  $this->assertTrue($this->storage->open('', ''));
56  }
57 
58  public function testCloseSession()
59  {
60  $this->assertTrue($this->storage->close());
61  }
62 
63  public function testReadSession()
64  {
65  $this->memcache
66  ->expects($this->once())
67  ->method('get')
68  ->with(self::PREFIX.'id')
69  ;
70 
71  $this->assertEquals('', $this->storage->read('id'));
72  }
73 
74  public function testWriteSession()
75  {
76  $this->memcache
77  ->expects($this->once())
78  ->method('set')
79  ->with(self::PREFIX.'id', 'data', 0, $this->equalTo(time() + self::TTL, 2))
80  ->will($this->returnValue(true))
81  ;
82 
83  $this->assertTrue($this->storage->write('id', 'data'));
84  }
85 
86  public function testDestroySession()
87  {
88  $this->memcache
89  ->expects($this->once())
90  ->method('delete')
91  ->with(self::PREFIX.'id')
92  ->will($this->returnValue(true))
93  ;
94 
95  $this->assertTrue($this->storage->destroy('id'));
96  }
97 
98  public function testGcSession()
99  {
100  $this->assertTrue($this->storage->gc(123));
101  }
102 
106  public function testSupportedOptions($options, $supported)
107  {
108  try {
109  new MemcacheSessionHandler($this->memcache, $options);
110  $this->assertTrue($supported);
111  } catch (\InvalidArgumentException $e) {
112  $this->assertFalse($supported);
113  }
114  }
115 
116  public function getOptionFixtures()
117  {
118  return array(
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),
123  );
124  }
125 
126  public function testGetConnection()
127  {
128  $method = new \ReflectionMethod($this->storage, 'getMemcache');
129  $method->setAccessible(true);
130 
131  $this->assertInstanceOf('\Memcache', $method->invoke($this->storage));
132  }
133 }
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcacheSessionHandlerTest\getOptionFixtures
getOptionFixtures()
Definition: MemcacheSessionHandlerTest.php:119
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcacheSessionHandlerTest\TTL
const TTL
Definition: MemcacheSessionHandlerTest.php:24
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcacheSessionHandlerTest\PREFIX
const PREFIX
Definition: MemcacheSessionHandlerTest.php:23
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcacheSessionHandlerTest\tearDown
tearDown()
Definition: MemcacheSessionHandlerTest.php:49
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcacheSessionHandlerTest\testSupportedOptions
testSupportedOptions($options, $supported)
Definition: MemcacheSessionHandlerTest.php:109
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcacheSessionHandlerTest\testGetConnection
testGetConnection()
Definition: MemcacheSessionHandlerTest.php:129
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcacheSessionHandlerTest\testReadSession
testReadSession()
Definition: MemcacheSessionHandlerTest.php:66
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcacheSessionHandlerTest\$memcache
$memcache
Definition: MemcacheSessionHandlerTest.php:33
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcacheSessionHandlerTest\testDestroySession
testDestroySession()
Definition: MemcacheSessionHandlerTest.php:89
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcacheSessionHandlerTest\testCloseSession
testCloseSession()
Definition: MemcacheSessionHandlerTest.php:61
Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHandler
Definition: MemcacheSessionHandler.php:19
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcacheSessionHandlerTest\testWriteSession
testWriteSession()
Definition: MemcacheSessionHandlerTest.php:77
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler
Definition: MemcachedSessionHandlerTest.php:12
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcacheSessionHandlerTest
Definition: MemcacheSessionHandlerTest.php:21
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcacheSessionHandlerTest\testGcSession
testGcSession()
Definition: MemcacheSessionHandlerTest.php:101
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcacheSessionHandlerTest\setUp
setUp()
Definition: MemcacheSessionHandlerTest.php:35
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcacheSessionHandlerTest\testOpenSession
testOpenSession()
Definition: MemcacheSessionHandlerTest.php:56
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcacheSessionHandlerTest\$storage
$storage
Definition: MemcacheSessionHandlerTest.php:31