1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
13:
14: use Symfony\Component\HttpFoundation\Session\Storage\Handler\LegacyPdoSessionHandler;
15:
16: class LegacyPdoSessionHandlerTest extends \PHPUnit_Framework_TestCase
17: {
18: private $pdo;
19:
20: protected function setUp()
21: {
22: $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
23:
24: if (!class_exists('PDO') || !in_array('sqlite', \PDO::getAvailableDrivers())) {
25: $this->markTestSkipped('This test requires SQLite support in your environment');
26: }
27:
28: $this->pdo = new \PDO('sqlite::memory:');
29: $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
30: $sql = 'CREATE TABLE sessions (sess_id VARCHAR(128) PRIMARY KEY, sess_data TEXT, sess_time INTEGER)';
31: $this->pdo->exec($sql);
32: }
33:
34: public function testIncompleteOptions()
35: {
36: $this->setExpectedException('InvalidArgumentException');
37: $storage = new LegacyPdoSessionHandler($this->pdo, array());
38: }
39:
40: public function testWrongPdoErrMode()
41: {
42: $pdo = new \PDO('sqlite::memory:');
43: $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT);
44: $pdo->exec('CREATE TABLE sessions (sess_id VARCHAR(128) PRIMARY KEY, sess_data TEXT, sess_time INTEGER)');
45:
46: $this->setExpectedException('InvalidArgumentException');
47: $storage = new LegacyPdoSessionHandler($pdo, array('db_table' => 'sessions'));
48: }
49:
50: public function testWrongTableOptionsWrite()
51: {
52: $storage = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'bad_name'));
53: $this->setExpectedException('RuntimeException');
54: $storage->write('foo', 'bar');
55: }
56:
57: public function testWrongTableOptionsRead()
58: {
59: $storage = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'bad_name'));
60: $this->setExpectedException('RuntimeException');
61: $storage->read('foo', 'bar');
62: }
63:
64: public function testWriteRead()
65: {
66: $storage = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'sessions'));
67: $storage->write('foo', 'bar');
68: $this->assertEquals('bar', $storage->read('foo'), 'written value can be read back correctly');
69: }
70:
71: public function testMultipleInstances()
72: {
73: $storage1 = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'sessions'));
74: $storage1->write('foo', 'bar');
75:
76: $storage2 = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'sessions'));
77: $this->assertEquals('bar', $storage2->read('foo'), 'values persist between instances');
78: }
79:
80: public function testSessionDestroy()
81: {
82: $storage = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'sessions'));
83: $storage->write('foo', 'bar');
84: $this->assertCount(1, $this->pdo->query('SELECT * FROM sessions')->fetchAll());
85:
86: $storage->destroy('foo');
87:
88: $this->assertCount(0, $this->pdo->query('SELECT * FROM sessions')->fetchAll());
89: }
90:
91: public function testSessionGC()
92: {
93: $storage = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'sessions'));
94:
95: $storage->write('foo', 'bar');
96: $storage->write('baz', 'bar');
97:
98: $this->assertCount(2, $this->pdo->query('SELECT * FROM sessions')->fetchAll());
99:
100: $storage->gc(-1);
101: $this->assertCount(0, $this->pdo->query('SELECT * FROM sessions')->fetchAll());
102: }
103:
104: public function testGetConnection()
105: {
106: $storage = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'sessions'), array());
107:
108: $method = new \ReflectionMethod($storage, 'getConnection');
109: $method->setAccessible(true);
110:
111: $this->assertInstanceOf('\PDO', $method->invoke($storage));
112: }
113: }
114: