Yaffs site version 1.1
[yaffs-website] / vendor / symfony / http-foundation / Tests / Session / Storage / Handler / 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
12 namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHandler;
16
17 /**
18  * @requires extension memcache
19  * @group time-sensitive
20  */
21 class MemcacheSessionHandlerTest extends TestCase
22 {
23     const PREFIX = 'prefix_';
24     const TTL = 1000;
25     /**
26      * @var MemcacheSessionHandler
27      */
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
103     /**
104      * @dataProvider getOptionFixtures
105      */
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 }