Open Monograph Press  3.3.0
MemcachedSessionHandlerTest.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 MemcachedSessionHandlerTest extends TestCase
22 {
23  const PREFIX = 'prefix_';
24  const TTL = 1000;
25 
29  protected $storage;
30 
31  protected $memcached;
32 
33  protected function setUp()
34  {
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');
37  }
38 
39  parent::setUp();
40 
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');
43  }
44 
45  $this->memcached = $this->getMockBuilder('Memcached')->getMock();
46  $this->storage = new MemcachedSessionHandler(
47  $this->memcached,
48  array('prefix' => self::PREFIX, 'expiretime' => self::TTL)
49  );
50  }
51 
52  protected function tearDown()
53  {
54  $this->memcached = null;
55  $this->storage = null;
56  parent::tearDown();
57  }
58 
59  public function testOpenSession()
60  {
61  $this->assertTrue($this->storage->open('', ''));
62  }
63 
64  public function testCloseSession()
65  {
66  $this->assertTrue($this->storage->close());
67  }
68 
69  public function testReadSession()
70  {
71  $this->memcached
72  ->expects($this->once())
73  ->method('get')
74  ->with(self::PREFIX.'id')
75  ;
76 
77  $this->assertEquals('', $this->storage->read('id'));
78  }
79 
80  public function testWriteSession()
81  {
82  $this->memcached
83  ->expects($this->once())
84  ->method('set')
85  ->with(self::PREFIX.'id', 'data', $this->equalTo(time() + self::TTL, 2))
86  ->will($this->returnValue(true))
87  ;
88 
89  $this->assertTrue($this->storage->write('id', 'data'));
90  }
91 
92  public function testDestroySession()
93  {
94  $this->memcached
95  ->expects($this->once())
96  ->method('delete')
97  ->with(self::PREFIX.'id')
98  ->will($this->returnValue(true))
99  ;
100 
101  $this->assertTrue($this->storage->destroy('id'));
102  }
103 
104  public function testGcSession()
105  {
106  $this->assertTrue($this->storage->gc(123));
107  }
108 
112  public function testSupportedOptions($options, $supported)
113  {
114  try {
115  new MemcachedSessionHandler($this->memcached, $options);
116  $this->assertTrue($supported);
117  } catch (\InvalidArgumentException $e) {
118  $this->assertFalse($supported);
119  }
120  }
121 
122  public function getOptionFixtures()
123  {
124  return array(
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),
129  );
130  }
131 
132  public function testGetConnection()
133  {
134  $method = new \ReflectionMethod($this->storage, 'getMemcached');
135  $method->setAccessible(true);
136 
137  $this->assertInstanceOf('\Memcached', $method->invoke($this->storage));
138  }
139 }
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcachedSessionHandlerTest\setUp
setUp()
Definition: MemcachedSessionHandlerTest.php:36
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcachedSessionHandlerTest\testGcSession
testGcSession()
Definition: MemcachedSessionHandlerTest.php:107
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcachedSessionHandlerTest\$memcached
$memcached
Definition: MemcachedSessionHandlerTest.php:34
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcachedSessionHandlerTest\getOptionFixtures
getOptionFixtures()
Definition: MemcachedSessionHandlerTest.php:125
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcachedSessionHandlerTest\$storage
$storage
Definition: MemcachedSessionHandlerTest.php:32
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcachedSessionHandlerTest\tearDown
tearDown()
Definition: MemcachedSessionHandlerTest.php:55
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcachedSessionHandlerTest\testSupportedOptions
testSupportedOptions($options, $supported)
Definition: MemcachedSessionHandlerTest.php:115
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcachedSessionHandlerTest\testGetConnection
testGetConnection()
Definition: MemcachedSessionHandlerTest.php:135
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcachedSessionHandlerTest\testCloseSession
testCloseSession()
Definition: MemcachedSessionHandlerTest.php:67
Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcachedSessionHandler
Definition: lib/vendor/symfony/http-foundation/Session/Storage/Handler/MemcachedSessionHandler.php:24
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcachedSessionHandlerTest\testOpenSession
testOpenSession()
Definition: MemcachedSessionHandlerTest.php:62
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcachedSessionHandlerTest\testDestroySession
testDestroySession()
Definition: MemcachedSessionHandlerTest.php:95
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcachedSessionHandlerTest\testReadSession
testReadSession()
Definition: MemcachedSessionHandlerTest.php:72
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcachedSessionHandlerTest
Definition: MemcachedSessionHandlerTest.php:21
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler
Definition: MemcachedSessionHandlerTest.php:12
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcachedSessionHandlerTest\TTL
const TTL
Definition: MemcachedSessionHandlerTest.php:24
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcachedSessionHandlerTest\PREFIX
const PREFIX
Definition: MemcachedSessionHandlerTest.php:23
Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler\MemcachedSessionHandlerTest\testWriteSession
testWriteSession()
Definition: MemcachedSessionHandlerTest.php:83