public function testSessionGC() { $storage = new PdoSessionHandler($this->pdo, array('db_table' => 'sessions'), array()); $storage->write('foo', 'bar'); $storage->write('baz', 'bar'); $this->assertEquals(2, count($this->pdo->query('SELECT * FROM sessions')->fetchAll())); $storage->gc(-1); $this->assertEquals(0, count($this->pdo->query('SELECT * FROM sessions')->fetchAll())); }
/** * @param Schema $schema */ public function up(Schema $schema) { $tableName = 'dtb_session'; if ($schema->hasTable($tableName)) { return; } $pdoSessionHandler = new PdoSessionHandler($this->connection->getWrappedConnection(), array('db_table' => $tableName)); $pdoSessionHandler->createTable(); }
public function __construct($pdoOrDsn = null, array $options = array()) { parent::__construct($pdoOrDsn, $options); if (array_key_exists('gc_maxlifetime', $options)) { ini_set('session.gc_maxlifetime', $options['gc_maxlifetime']); } }
/** * {@inheritdoc} */ public function open($savePath, $sessionName) { parent::open($savePath, $sessionName); if (null === $this->_pdo) { $this->_connect($this->_dsn ?: $savePath); } return true; }
/** * I think I have severely butchered this test...it's not so much of a unit test as it is a full-fledged component test */ public function testConnectionValueFromPdo() { if (!extension_loaded('PDO') || !extension_loaded('pdo_sqlite')) { return $this->markTestSkipped('Session test requires PDO and pdo_sqlite'); } $sessionId = md5('testSession'); $dbOptions = array('db_table' => 'sessions', 'db_id_col' => 'sess_id', 'db_data_col' => 'sess_data', 'db_time_col' => 'sess_time', 'db_lifetime_col' => 'sess_lifetime'); $pdo = new \PDO("sqlite::memory:"); $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); $pdo->exec(vsprintf("CREATE TABLE %s (%s TEXT NOT NULL PRIMARY KEY, %s BLOB NOT NULL, %s INTEGER NOT NULL, %s INTEGER)", $dbOptions)); $pdoHandler = new PdoSessionHandler($pdo, $dbOptions); $pdoHandler->write($sessionId, '_sf2_attributes|a:2:{s:5:"hello";s:5:"world";s:4:"last";i:1332872102;}_sf2_flashes|a:0:{}'); $component = new SessionProvider($this->getMock('Ratchet\\MessageComponentInterface'), $pdoHandler, array('auto_start' => 1)); $connection = $this->getMock('Ratchet\\ConnectionInterface'); $headers = $this->getMock('Guzzle\\Http\\Message\\Request', array('getCookie'), array('POST', '/', array())); $headers->expects($this->once())->method('getCookie', array(ini_get('session.name')))->will($this->returnValue($sessionId)); $connection->WebSocket = new \StdClass(); $connection->WebSocket->request = $headers; $component->onOpen($connection); $this->assertEquals('world', $connection->Session->get('hello')); }
private function testOperations(PDO $connection, array $parameters) { $options = array('db_table' => $parameters['session_db_table'], 'db_id_col' => $parameters['session_db_id_col'], 'db_data_col' => $parameters['session_db_data_col'], 'db_time_col' => $parameters['session_db_time_col']); $errors = array(); $handler = new PdoSessionHandler($connection, $options); $connection->beginTransaction(); try { $handler->write('session_1', 'session 1 data...'); $handler->write('session_2', 'session 2 data...'); $handler->read('session_1'); $handler->read('session_2'); $handler->destroy('session 1'); $handler->gc(1); } catch (\Exception $ex) { $errors[] = 'session_db_cannot_perform_operations'; } $connection->rollBack(); return $errors; }
/** * */ public function testApiHandleConnect_AttachesSessionHandler() { if (!extension_loaded('PDO') || !extension_loaded('pdo_sqlite')) { $this->markTestSkipped('Session test requires PDO and pdo_sqlite'); } $sessionId = md5('testSession'); $dbOptions = ['db_table' => 'sessions', 'db_id_col' => 'sess_id', 'db_data_col' => 'sess_data', 'db_time_col' => 'sess_time', 'db_lifetime_col' => 'sess_lifetime']; $pdo = new PDO("sqlite::memory:"); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->exec(vsprintf("CREATE TABLE %s (%s TEXT NOT NULL PRIMARY KEY, %s BLOB NOT NULL, %s INTEGER NOT NULL, %s INTEGER)", $dbOptions)); $pdoHandler = new PdoSessionHandler($pdo, $dbOptions); $pdoHandler->write($sessionId, '_sf2_attributes|a:2:{s:5:"hello";s:5:"world";s:4:"last";i:1332872102;}_sf2_flashes|a:0:{}'); $conn = $this->getMock(NetworkConnection::class, [], [], '', false); $server = $this->createServer(); $component = $this->createComponent(['handleConnect']); $component->expects($this->once())->method('handleConnect')->with($conn); $session = $this->createSession($server, $component, $pdoHandler, ['auto_start' => 1]); $req = $this->getMock(HttpRequest::class, ['getCookie'], ['POST', '/', []]); $req->expects($this->once())->method('getCookie')->with(ini_get('session.name'))->will($this->returnValue($sessionId)); $conn->WebSocket = new StdClass(); $conn->WebSocket->request = $req; $session->handleConnect($conn); $this->assertSame('world', $conn->Session->get('hello')); }
public function testSessionGC() { $previousLifeTime = ini_set('session.gc_maxlifetime', 1000); $pdo = $this->getMemorySqlitePdo(); $storage = new PdoSessionHandler($pdo); $storage->open('', 'sid'); $storage->read('id'); $storage->write('id', 'data'); $storage->close(); $storage->open('', 'sid'); $storage->read('gc_id'); ini_set('session.gc_maxlifetime', -1); // test that you can set lifetime of a session after it has been read $storage->write('gc_id', 'data'); $storage->close(); $this->assertEquals(2, $pdo->query('SELECT COUNT(*) FROM sessions')->fetchColumn(), 'No session pruned because gc not called'); $storage->open('', 'sid'); $data = $storage->read('gc_id'); $storage->gc(-1); $storage->close(); ini_set('session.gc_maxlifetime', $previousLifeTime); $this->assertSame('', $data, 'Session already considered garbage, so not returning data even if it is not pruned yet'); $this->assertEquals(1, $pdo->query('SELECT COUNT(*) FROM sessions')->fetchColumn(), 'Expired session is pruned'); }
public function testNewSessionCreated() { $this->assertEquals(self::$SESSION_DATA_EMPTY, $this->migrationSession->read(self::$SESSION_ID)); $this->assertEquals(self::$SESSION_DATA_EMPTY, $this->legacySessionTable->read(self::$SESSION_ID)); $this->assertEquals(self::$SESSION_DATA_EMPTY, $this->sessionTable->read(self::$SESSION_ID)); }
public function testSessionGC() { $previousLifeTime = ini_set('session.gc_maxlifetime', 0); $storage = new PdoSessionHandler($this->pdo); $storage->open('', 'sid'); $storage->read('id'); $storage->write('id', 'data'); $storage->close(); $this->assertEquals(1, $this->pdo->query('SELECT COUNT(*) FROM sessions')->fetchColumn()); $storage->open('', 'sid'); $this->assertSame('', $storage->read('id'), 'Session already considered garbage, so not returning data even if it is not pruned yet'); $storage->gc(0); $storage->close(); $this->assertEquals(0, $this->pdo->query('SELECT COUNT(*) FROM sessions')->fetchColumn()); ini_set('session.gc_maxlifetime', $previousLifeTime); }