From 39882fbb16d04fefb63ed91a3dc52a767219fdbe Mon Sep 17 00:00:00 2001
From: Sebastian Kroczek <skroczek@wavecon.de>
Date: Tue, 25 Aug 2015 13:32:30 +0200
Subject: [PATCH] Fixes VirtualSessionStorage with pdo_sqlite as storage

---
 .../Session/Storage/VirtualSessionStorage.php |  6 ++
 .../Storage/VirtualSessionStoragePDOTest.php  | 58 +++++++++++++++++++
 2 files changed, 64 insertions(+)
 create mode 100644 tests/unit/Session/Storage/VirtualSessionStoragePDOTest.php

diff --git a/src/Ratchet/Session/Storage/VirtualSessionStorage.php b/src/Ratchet/Session/Storage/VirtualSessionStorage.php
index 9fb0eb7..daa10bb 100644
--- a/src/Ratchet/Session/Storage/VirtualSessionStorage.php
+++ b/src/Ratchet/Session/Storage/VirtualSessionStorage.php
@@ -30,6 +30,12 @@ class VirtualSessionStorage extends NativeSessionStorage {
             return true;
         }
 
+        // You have to call Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler::open() to use
+        // pdo_sqlite (and possible pdo_*) as session storage, if you are using a DSN string instead of a \PDO object
+        // in the constructor. The method arguments are filled with the values, which are also used by the symfony
+        // framework in this case. This must not be the best choice, but it works.
+        $this->saveHandler->open(session_save_path(), session_name());
+
         $rawData     = $this->saveHandler->read($this->saveHandler->getId());
         $sessionData = $this->_serializer->unserialize($rawData);
 
diff --git a/tests/unit/Session/Storage/VirtualSessionStoragePDOTest.php b/tests/unit/Session/Storage/VirtualSessionStoragePDOTest.php
new file mode 100644
index 0000000..9909bce
--- /dev/null
+++ b/tests/unit/Session/Storage/VirtualSessionStoragePDOTest.php
@@ -0,0 +1,58 @@
+<?php
+
+namespace Ratchet\Session\Storage;
+
+
+use Ratchet\Session\Serialize\PhpHandler;
+use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
+use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
+use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
+
+class VirtualSessionStoragePDOTest extends \PHPUnit_Framework_TestCase
+{
+    /**
+     * @var VirtualSessionStorage
+     */
+    protected $_virtualSessionStorage;
+
+    protected $_pathToDB;
+
+    public function setUp()
+    {
+        $schema = <<<SQL
+CREATE TABLE `sessions` (
+    `sess_id` VARBINARY(128) NOT NULL PRIMARY KEY,
+    `sess_data` BLOB NOT NULL,
+    `sess_time` INTEGER UNSIGNED NOT NULL,
+    `sess_lifetime` MEDIUMINT NOT NULL
+);
+SQL;
+        $this->_pathToDB = tempnam(sys_get_temp_dir(), 'SQ3');;
+        $dsn = 'sqlite:' . $this->_pathToDB;
+
+        $pdo = new \PDO($dsn);
+        $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
+        $pdo->exec($schema);
+        $pdo = null;
+
+        $sessionHandler = new PdoSessionHandler($dsn);
+        $serializer = new PhpHandler();
+        $this->_virtualSessionStorage = new VirtualSessionStorage($sessionHandler, 'foobar', $serializer);
+        $this->_virtualSessionStorage->registerBag(new FlashBag());
+        $this->_virtualSessionStorage->registerBag(new AttributeBag());
+    }
+
+    public function tearDown()
+    {
+        unlink($this->_pathToDB);
+    }
+
+    public function testStartWithDSN()
+    {
+        $this->_virtualSessionStorage->start();
+
+        $this->assertTrue($this->_virtualSessionStorage->isStarted());
+    }
+
+
+}