4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Session\Storage\Handler\LegacyPdoSessionHandler;
19 * @group time-sensitive
20 * @requires extension pdo_sqlite
22 class LegacyPdoSessionHandlerTest extends TestCase
26 protected function 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);
35 public function testIncompleteOptions()
37 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
38 $storage = new LegacyPdoSessionHandler($this->pdo, array());
41 public function testWrongPdoErrMode()
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)');
47 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
48 $storage = new LegacyPdoSessionHandler($pdo, array('db_table' => 'sessions'));
51 public function testWrongTableOptionsWrite()
53 $storage = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'bad_name'));
54 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('RuntimeException');
55 $storage->write('foo', 'bar');
58 public function testWrongTableOptionsRead()
60 $storage = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'bad_name'));
61 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('RuntimeException');
62 $storage->read('foo');
65 public function testWriteRead()
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');
72 public function testMultipleInstances()
74 $storage1 = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'sessions'));
75 $storage1->write('foo', 'bar');
77 $storage2 = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'sessions'));
78 $this->assertEquals('bar', $storage2->read('foo'), 'values persist between instances');
81 public function testSessionDestroy()
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());
87 $storage->destroy('foo');
89 $this->assertCount(0, $this->pdo->query('SELECT * FROM sessions')->fetchAll());
92 public function testSessionGC()
94 $storage = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'sessions'));
96 $storage->write('foo', 'bar');
97 $storage->write('baz', 'bar');
99 $this->assertCount(2, $this->pdo->query('SELECT * FROM sessions')->fetchAll());
102 $this->assertCount(0, $this->pdo->query('SELECT * FROM sessions')->fetchAll());
105 public function testGetConnection()
107 $storage = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'sessions'), array());
109 $method = new \ReflectionMethod($storage, 'getConnection');
110 $method->setAccessible(true);
112 $this->assertInstanceOf('\PDO', $method->invoke($storage));