2e7be359efcfff04427367f21d8d216a660b7882
[yaffs-website] / vendor / symfony / http-foundation / Tests / Session / Storage / Handler / 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
12 namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcachedSessionHandler;
16
17 /**
18  * @requires extension memcached
19  * @group time-sensitive
20  */
21 class MemcachedSessionHandlerTest extends TestCase
22 {
23     const PREFIX = 'prefix_';
24     const TTL = 1000;
25
26     /**
27      * @var MemcachedSessionHandler
28      */
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
109     /**
110      * @dataProvider getOptionFixtures
111      */
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 }