db backup prior to drupal security update
[yaffs-website] / vendor / symfony / http-foundation / Tests / Session / Storage / Handler / LegacyPdoSessionHandlerTest.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\LegacyPdoSessionHandler;
16
17 /**
18  * @group legacy
19  * @group time-sensitive
20  * @requires extension pdo_sqlite
21  */
22 class LegacyPdoSessionHandlerTest extends TestCase
23 {
24     private $pdo;
25
26     protected function setUp()
27     {
28         parent::setUp();
29         $this->pdo = new \PDO('sqlite::memory:');
30         $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
31         $sql = 'CREATE TABLE sessions (sess_id VARCHAR(128) PRIMARY KEY, sess_data TEXT, sess_time INTEGER)';
32         $this->pdo->exec($sql);
33     }
34
35     public function testIncompleteOptions()
36     {
37         $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
38         $storage = new LegacyPdoSessionHandler($this->pdo, array());
39     }
40
41     public function testWrongPdoErrMode()
42     {
43         $pdo = new \PDO('sqlite::memory:');
44         $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT);
45         $pdo->exec('CREATE TABLE sessions (sess_id VARCHAR(128) PRIMARY KEY, sess_data TEXT, sess_time INTEGER)');
46
47         $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
48         $storage = new LegacyPdoSessionHandler($pdo, array('db_table' => 'sessions'));
49     }
50
51     public function testWrongTableOptionsWrite()
52     {
53         $storage = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'bad_name'));
54         $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('RuntimeException');
55         $storage->write('foo', 'bar');
56     }
57
58     public function testWrongTableOptionsRead()
59     {
60         $storage = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'bad_name'));
61         $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('RuntimeException');
62         $storage->read('foo');
63     }
64
65     public function testWriteRead()
66     {
67         $storage = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'sessions'));
68         $storage->write('foo', 'bar');
69         $this->assertEquals('bar', $storage->read('foo'), 'written value can be read back correctly');
70     }
71
72     public function testMultipleInstances()
73     {
74         $storage1 = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'sessions'));
75         $storage1->write('foo', 'bar');
76
77         $storage2 = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'sessions'));
78         $this->assertEquals('bar', $storage2->read('foo'), 'values persist between instances');
79     }
80
81     public function testSessionDestroy()
82     {
83         $storage = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'sessions'));
84         $storage->write('foo', 'bar');
85         $this->assertCount(1, $this->pdo->query('SELECT * FROM sessions')->fetchAll());
86
87         $storage->destroy('foo');
88
89         $this->assertCount(0, $this->pdo->query('SELECT * FROM sessions')->fetchAll());
90     }
91
92     public function testSessionGC()
93     {
94         $storage = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'sessions'));
95
96         $storage->write('foo', 'bar');
97         $storage->write('baz', 'bar');
98
99         $this->assertCount(2, $this->pdo->query('SELECT * FROM sessions')->fetchAll());
100
101         $storage->gc(-1);
102         $this->assertCount(0, $this->pdo->query('SELECT * FROM sessions')->fetchAll());
103     }
104
105     public function testGetConnection()
106     {
107         $storage = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'sessions'), array());
108
109         $method = new \ReflectionMethod($storage, 'getConnection');
110         $method->setAccessible(true);
111
112         $this->assertInstanceOf('\PDO', $method->invoke($storage));
113     }
114 }