diff --git a/coverage/AbstractSession.php.html b/coverage/AbstractSession.php.html index 72e51e4..8701f92 100644 --- a/coverage/AbstractSession.php.html +++ b/coverage/AbstractSession.php.html @@ -649,10 +649,10 @@
40
     * @param LoggerInterface $logger A logger object. If omitted, AloFramework\Log will be used.
41
     */
42
    function __construct(Config $cfg = null, LoggerInterface $logger = null) { -
43
        $this->config = Alo::ifnull($cfg, new Config()); -
44
        $this->log    = Alo::ifnull($logger, new Log()); -
45
        $this->setID(); -
46
    } +
43
        $this->config = Alo::ifnull($cfg, new Config()); +
44
        $this->log    = Alo::ifnull($logger, new Log()); +
45
        $this->setID(); +
46
    }
47
48
    /**
49
     * Sets the session ID variable & the cookie @@ -661,22 +661,22 @@
52
     * @return string The generated ID
53
     */
54
    protected function setID() { -
55
        $c = Alo::nullget($_COOKIE[$this->config->cookie]); +
55
        $c = Alo::nullget($_COOKIE[$this->config->cookie]);
56
        //@codeCoverageIgnoreStart
57
        if ($c && strlen($c) == strlen(hash($this->config->sessionAlgo, 1))) {
58
            $sid = $c;
59
        } else {
60
            //@codeCoverageIgnoreEnd -
61
            do { -
62
                $sid = Alo::getUniqid($this->config->sessionAlgo, 'session' . Alo::getFingerprint('md5')); -
63
            } while ($this->idExists($sid)); +
61
            do { +
62
                $sid = Alo::getUniqid($this->config->sessionAlgo, 'session' . Alo::getFingerprint('md5')); +
63
            } while ($this->idExists($sid));
64
        }
65
-
66
        session_id($sid); +
66
        session_id($sid);
67
-
68
        $this->log->debug('Session ID set to ' . $sid); +
68
        $this->log->debug('Session ID set to ' . $sid);
69
-
70
        return $sid; +
70
        return $sid;
71
    }
72
73
    /** @@ -696,14 +696,14 @@
87
     * @return bool
88
     */
89
    static function destroySafely() { -
90
        if (self::isActive()) { -
91
            session_destroy(); +
90
        if (self::isActive()) { +
91
            session_destroy();
92
-
93
            return true; +
93
            return true;
94
        } else { -
95
            return false; +
95
            return false;
96
        } -
97
    } +
97
    }
98
99
    /**
100
     * Returns the last started session object. This is reset back to null after the session is destroyed or closed. @@ -712,7 +712,7 @@
103
     * @since  1.2
104
     */
105
    static function getLastActiveSession() { -
106
        return self::$activeSession; +
106
        return self::$activeSession;
107
    }
108
109
    /** @@ -723,7 +723,7 @@
114
     * @since  1.2.1
115
     */
116
    function jsonSerialize() { -
117
        return self::isActive() ? $_SESSION : []; +
117
        return self::isActive() ? $_SESSION : [];
118
    }
119
120
    /** @@ -732,7 +732,7 @@
123
     * @return bool
124
     */
125
    static function isActive() { -
126
        return session_status() === PHP_SESSION_ACTIVE; +
126
        return session_status() === PHP_SESSION_ACTIVE;
127
    }
128
129
    /** @@ -741,27 +741,27 @@
132
     * @return self
133
     */
134
    function start() { -
135
        $this->log->debug('Starting session with ' . __CLASS__); -
136
        if (self::isActive()) { +
135
        $this->log->debug('Starting session with ' . __CLASS__); +
136
        if (self::isActive()) {
137
            //Can't test this via PHPUnit
138
            //@codeCoverageIgnoreStart
139
            session_write_close();
140
            //@codeCoverageIgnoreEnd -
141
            trigger_error('A session has already been started - it has now been destroyed to start the new one', -
142
                          E_USER_WARNING); +
141
            trigger_error('A session has already been started - it has now been destroyed to start the new one', +
142
                          E_USER_WARNING);
143
            //@codeCoverageIgnoreStart
144
        }
145
        //@codeCoverageIgnoreEnd
146
-
147
        self::$activeSession = &$this; -
148
        session_set_cookie_params($this->config->timeout, '/', null, $this->config->secure, true); -
149
        session_name($this->config->cookie); +
147
        self::$activeSession = &$this; +
148
        session_set_cookie_params($this->config->timeout, '/', null, $this->config->secure, true); +
149
        session_name($this->config->cookie);
150
-
151
        session_set_save_handler($this, false); -
152
        session_start(); -
153
        $this->identityCheck(); +
151
        session_set_save_handler($this, false); +
152
        session_start(); +
153
        $this->identityCheck();
154
-
155
        return $this; +
155
        return $this;
156
    }
157
158
    /** @@ -771,20 +771,20 @@
162
     * @return boolean TRUE if the check has passed, FALSE if not and the session has been terminated.
163
     */
164
    private function identityCheck() { -
165
        $fingerprint = self::getFingerprint(); +
165
        $fingerprint = self::getFingerprint();
166
-
167
        if (!Alo::nullget($_SESSION[$this->config->fingerprint])) { -
168
            $_SESSION[$this->config->fingerprint] = $fingerprint; -
169
        } elseif ($fingerprint !== $_SESSION[$this->config->fingerprint]) { +
167
        if (!Alo::nullget($_SESSION[$this->config->fingerprint])) { +
168
            $_SESSION[$this->config->fingerprint] = $fingerprint; +
169
        } elseif ($fingerprint !== $_SESSION[$this->config->fingerprint]) {
170
            //@codeCoverageIgnoreStart
171
            $this->handleIdentityCheckFailure(session_id());
172
173
            return false;
174
            //@codeCoverageIgnoreEnd
175
        } -
176
        $this->log->debug('Identity check passed for session ID ' . session_id()); +
176
        $this->log->debug('Identity check passed for session ID ' . session_id());
177
-
178
        return true; +
178
        return true;
179
    }
180
181
    /** @@ -794,7 +794,7 @@
185
     * @return string
186
     */
187
    private static function getFingerprint() { -
188
        return md5('AloSession' . Alo::getFingerprint('md5')); +
188
        return md5('AloSession' . Alo::getFingerprint('md5'));
189
    }
190
191
    /** @@ -804,9 +804,9 @@
195
     * @param string $sessionID The session ID that failed
196
     */
197
    protected function handleIdentityCheckFailure($sessionID) { -
198
        $this->log->notice('Session identity check failed for session ID ' . $sessionID); -
199
        session_destroy(); -
200
    } +
198
        $this->log->notice('Session identity check failed for session ID ' . $sessionID); +
199
        session_destroy(); +
200
    }
201
202
    /**
203
     * Close the session @@ -833,10 +833,10 @@
224
     * internally to PHP for processing.
225
     */
226
    function destroy($sessionID) { -
227
        $this->log->info('Destroyed session ' . $sessionID); -
228
        self::$activeSession = null; +
227
        $this->log->info('Destroyed session ' . $sessionID); +
228
        self::$activeSession = null;
229
-
230
        return setcookie($this->config->cookie, '', time() - 3, null, null, $this->config->secure, true); +
230
        return setcookie($this->config->cookie, '', time() - 3, null, null, $this->config->secure, true);
231
    }
232
233
    /** @@ -880,15 +880,15 @@
271
     * @return boolean
272
     */
273
    function offsetExists($offset) { -
274
        if (!self::isActive()) { -
275
            self::sessionRequiredWarning(__METHOD__); +
274
        if (!self::isActive()) { +
275
            self::sessionRequiredWarning(__METHOD__);
276
277
            //@codeCoverageIgnoreStart
278
            return false;
279
            //@codeCoverageIgnoreEnd
280
        }
281
-
282
        return isset($_SESSION[$offset]); +
282
        return isset($_SESSION[$offset]);
283
    }
284
285
    /** @@ -898,7 +898,7 @@
289
     * @param string $method The method used
290
     */
291
    private static function sessionRequiredWarning($method) { -
292
        trigger_error($method . ' failed: the session must be started first', E_USER_WARNING); +
292
        trigger_error($method . ' failed: the session must be started first', E_USER_WARNING);
293
        //@codeCoverageIgnoreStart
294
    }
295
    //@codeCoverageIgnoreEnd @@ -913,7 +913,7 @@
304
     * @uses   AbstractSession::offsetGet()
305
     */
306
    function __get($key) { -
307
        return $this->offsetGet($key); +
307
        return $this->offsetGet($key);
308
    }
309
310
    /** @@ -926,8 +926,8 @@
317
     * @uses   AbstractSession::offsetSet()
318
     */
319
    function __set($key, $value) { -
320
        $this->offsetSet($key, $value); -
321
    } +
320
        $this->offsetSet($key, $value); +
321
    }
322
323
    /**
324
     * Offset to retrieve @@ -939,15 +939,15 @@
330
     * @return mixed
331
     */
332
    function offsetGet($offset) { -
333
        if (!self::isActive()) { -
334
            self::sessionRequiredWarning(__METHOD__); +
333
        if (!self::isActive()) { +
334
            self::sessionRequiredWarning(__METHOD__);
335
336
            //@codeCoverageIgnoreStart
337
            return null;
338
            //@codeCoverageIgnoreEnd
339
        }
340
-
341
        return Alo::get($_SESSION[$offset]); +
341
        return Alo::get($_SESSION[$offset]);
342
    }
343
344
    /** @@ -961,14 +961,14 @@
352
     * @return void
353
     */
354
    function offsetSet($offset, $value) { -
355
        if (!self::isActive()) { -
356
            self::sessionRequiredWarning(__METHOD__); +
355
        if (!self::isActive()) { +
356
            self::sessionRequiredWarning(__METHOD__);
357
            //@codeCoverageIgnoreStart
358
        } else {
359
            //@codeCoverageIgnoreEnd -
360
            $_SESSION[$offset] = $value; +
360
            $_SESSION[$offset] = $value;
361
        } -
362
    } +
362
    }
363
364
    /**
365
     * Offset to unset @@ -980,24 +980,24 @@
371
     * @return void
372
     */
373
    function offsetUnset($offset) { -
374
        if (!self::isActive()) { -
375
            self::sessionRequiredWarning(__METHOD__); +
374
        if (!self::isActive()) { +
375
            self::sessionRequiredWarning(__METHOD__);
376
            //@codeCoverageIgnoreStart
377
        } else {
378
            //@codeCoverageIgnoreEnd -
379
            unset($_SESSION[$offset]); +
379
            unset($_SESSION[$offset]);
380
        } -
381
    } +
381
    }
382
383
    /**
384
     * Saves session data
385
     * @author Art <a.molcanovas@gmail.com>
386
     */
387
    function __destruct() { -
388
        if (self::isActive()) { -
389
            session_write_close(); -
390
        } -
391
    } +
388
        if (self::isActive()) { +
389
            session_write_close(); +
390
        } +
391
    }
392
393
    /**
394
     * Checks if the session should be saved/written @@ -1006,9 +1006,9 @@
397
     * @since  1.1
398
     */
399
    protected function shouldBeSaved() { -
400
        $isCli = Alo::isCliRequest(); +
400
        $isCli = Alo::isCliRequest();
401
-
402
        return !$isCli || ($isCli && $this->config->saveCLI); +
402
        return !$isCli || ($isCli && $this->config->saveCLI);
403
    }
404
} @@ -1023,7 +1023,7 @@

Legend

Dead Code

- Generated by PHP_CodeCoverage 3.1.1 using PHP 5.6.18 and PHPUnit 5.2.3 at Mon Feb 8 19:54:38 GMT 2016. + Generated by PHP_CodeCoverage 3.1.1 using PHP 5.6.18 and PHPUnit 5.2.3 at Mon Feb 8 20:12:41 GMT 2016.

diff --git a/coverage/Config.php.html b/coverage/Config.php.html index 44b67d5..f71c816 100644 --- a/coverage/Config.php.html +++ b/coverage/Config.php.html @@ -261,8 +261,8 @@
102
         * @param array $cfg Your custom config overrides
103
         */
104
        function __construct(array $cfg = []) { -
105
@@ -271,8 +271,8 @@ class="default">self::setDefaultConfig(); -
106
@@ -284,8 +284,8 @@ class="default"> $cfg); -
107
@@ -299,8 +299,8 @@
111
         * @author Art <a.molcanovas@gmail.com>
112
         */
113
        private static function setDefaultConfig() { -
114
@@ -344,8 +344,8 @@ class="keyword">];
125
            } -
126
@@ -368,7 +368,7 @@

Legend

Generated by PHP_CodeCoverage 3.1.1 using PHP 5.6.18 and PHPUnit - 5.2.3 at Mon Feb 8 19:54:38 GMT 2016. + 5.2.3 at Mon Feb 8 20:12:41 GMT 2016.

diff --git a/coverage/MySQLNoEventSession.php.html b/coverage/MySQLNoEventSession.php.html index 534533d..bcc9e7c 100644 --- a/coverage/MySQLNoEventSession.php.html +++ b/coverage/MySQLNoEventSession.php.html @@ -201,7 +201,7 @@

Legend

Generated by PHP_CodeCoverage 3.1.1 using PHP 5.6.18 and PHPUnit - 5.2.3 at Mon Feb 8 19:54:38 GMT 2016. + 5.2.3 at Mon Feb 8 20:12:41 GMT 2016.

diff --git a/coverage/MySQLSession.php.html b/coverage/MySQLSession.php.html index 87b0f64..3eb1ff6 100644 --- a/coverage/MySQLSession.php.html +++ b/coverage/MySQLSession.php.html @@ -402,8 +402,8 @@ class="default"> null) { -
31
@@ -422,8 +422,8 @@ class="comment">//Parent constructor must be called after $this->client is set -
34
@@ -434,8 +434,8 @@ class="keyword">, $logger); -
35
@@ -518,8 +518,8 @@ class="default">$sessionID) { -
48
@@ -536,8 +536,8 @@         try { -
50
@@ -553,8 +553,8 @@ class="keyword">. '` WHERE `id`=? LIMIT 1'); -
51
@@ -567,8 +567,8 @@ class="keyword">;
52
-
53
@@ -723,8 +723,8 @@         try { -
75
@@ -741,8 +741,8 @@ class="keyword">);
76
-
77
@@ -754,8 +754,8 @@ class="keyword">])) { -
78
@@ -814,8 +814,8 @@                 //@codeCoverageIgnoreEnd -
85
@@ -898,8 +898,8 @@ -
95
@@ -1017,8 +1017,8 @@ class="default">$sessionData) { -
112
@@ -1043,8 +1043,8 @@                 $sql  = -
115
@@ -1059,8 +1059,8 @@ class="default"> '`(`id`,`data`) VALUES(?,?)'); -
116
@@ -1079,8 +1079,8 @@ -
118
@@ -1309,8 +1309,8 @@                 //@codeCoverageIgnoreEnd -
150
@@ -1328,8 +1328,8 @@ -
152
@@ -1447,7 +1447,7 @@

Legend

Generated by PHP_CodeCoverage 3.1.1 using PHP 5.6.18 and PHPUnit - 5.2.3 at Mon Feb 8 19:54:38 GMT 2016. + 5.2.3 at Mon Feb 8 20:12:41 GMT 2016.

diff --git a/coverage/RedisSession.php.html b/coverage/RedisSession.php.html index 42ded66..2ab8dda 100644 --- a/coverage/RedisSession.php.html +++ b/coverage/RedisSession.php.html @@ -493,8 +493,8 @@ class="comment">//@codeCoverageIgnoreEnd
42
-
43
@@ -513,8 +513,8 @@ class="comment">//Parent constructor must be called after $this->client is set -
46
@@ -525,8 +525,8 @@ class="keyword">, $logger); -
47
@@ -609,8 +609,8 @@ class="default">$sessionID) { -
60
@@ -621,8 +621,8 @@ class="default">destroy($sessionID); -
61
@@ -636,8 +636,8 @@ class="keyword">);
62
-
63
@@ -728,8 +728,8 @@ class="default">$sessionID) { -
77
@@ -749,8 +749,8 @@ -
79
@@ -872,8 +872,8 @@ class="default">$sessionData) { -
96
@@ -884,8 +884,8 @@ class="default">shouldBeSaved()) { -
97
@@ -898,8 +898,8 @@ class="default">prefix . $sessionID, -
98
@@ -908,8 +908,8 @@ class="default">$this->config->timeout, -
99
@@ -1025,8 +1025,8 @@ class="keyword">($sessionID) { -
116
@@ -1069,7 +1069,7 @@

Legend

Generated by PHP_CodeCoverage 3.1.1 using PHP 5.6.18 and PHPUnit - 5.2.3 at Mon Feb 8 19:54:38 GMT 2016. + 5.2.3 at Mon Feb 8 20:12:41 GMT 2016.

diff --git a/coverage/SessionException.php.html b/coverage/SessionException.php.html index 7d293cb..31ca01c 100644 --- a/coverage/SessionException.php.html +++ b/coverage/SessionException.php.html @@ -145,7 +145,7 @@

Legend

Generated by PHP_CodeCoverage 3.1.1 using PHP 5.6.18 and PHPUnit - 5.2.3 at Mon Feb 8 19:54:38 GMT 2016. + 5.2.3 at Mon Feb 8 20:12:41 GMT 2016.

diff --git a/coverage/Token.php.html b/coverage/Token.php.html index 2e85215..ed50f7f 100644 --- a/coverage/Token.php.html +++ b/coverage/Token.php.html @@ -1916,7 +1916,7 @@

Legend

Generated by PHP_CodeCoverage 3.1.1 using PHP 5.6.18 and PHPUnit - 5.2.3 at Mon Feb 8 19:54:38 GMT 2016. + 5.2.3 at Mon Feb 8 20:12:41 GMT 2016.

diff --git a/coverage/dashboard.html b/coverage/dashboard.html index 8509101..36d89a6 100644 --- a/coverage/dashboard.html +++ b/coverage/dashboard.html @@ -138,7 +138,7 @@

Project Risks

Generated by PHP_CodeCoverage 3.1.1 using PHP 5.6.18 and PHPUnit - 5.2.3 at Mon Feb 8 19:54:38 GMT 2016. + 5.2.3 at Mon Feb 8 20:12:41 GMT 2016.

diff --git a/coverage/index.html b/coverage/index.html index 1f25bad..94142c3 100644 --- a/coverage/index.html +++ b/coverage/index.html @@ -294,7 +294,7 @@

Legend

Generated by PHP_CodeCoverage 3.1.1 using PHP 5.6.18 and PHPUnit - 5.2.3 at Mon Feb 8 19:54:38 GMT 2016. + 5.2.3 at Mon Feb 8 20:12:41 GMT 2016.

diff --git a/tests/AbstractSessionTest.php b/tests/AbstractSessionTest.php index dc217da..b41f5a9 100644 --- a/tests/AbstractSessionTest.php +++ b/tests/AbstractSessionTest.php @@ -2,12 +2,18 @@ namespace AloFramework\Session\Tests; -use AloFramework\Session\Config; +use AloFramework\Common\Alo; use AloFramework\Session\Config as Cfg; use AloFramework\Session\RedisSession as Sess; use PHPUnit_Framework_TestCase; use Redis; +if (file_exists('vendor/autoload.php')) { + require_once 'vendor/autoload.php'; +} else { + require_once '../vendor/autoload.php'; +} + class AbstractSessionTest extends PHPUnit_Framework_TestCase { /** @var Cfg */ @@ -60,7 +66,7 @@ function testDestruct() { $this->assertEquals('s:3:"foo"', self::sessionUnserialize($this->redis->get($key))[__METHOD__]); } - protected static function sessionUnserialize($str) { + public static function sessionUnserialize($str) { $spl = explode(';', $str); foreach ($spl as $k => $s) { @@ -76,18 +82,19 @@ protected static function sessionUnserialize($str) { function testJsonSerializeNoSession() { $sess = new Sess($this->redis, $this->cfg); - $this->assertEquals(json_encode([]), json_encode($sess)); + $enc = json_encode($sess); + $this->assertEquals(json_encode([]), $enc); } function testJsonSerializeSession() { $sess = new Sess($this->redis, $this->cfg); $sess->start(); - $finger = (new Config())->get(Config::CFG_FINGERPRINT_NAME); + $finger = (new Cfg())->get(Cfg::CFG_FINGERPRINT_NAME); $_SESSION[__METHOD__] = 'bar'; - $pattern = '~{"' . $finger . '":"[a-z0-9]{32}","' . __METHOD__ . '":"bar"}~i'; - $str = json_encode($sess); + $code = json_decode(json_encode($sess), true); - $this->assertEquals(1, preg_match($pattern, $str)); + $this->assertEquals('bar', Alo::get($code[__METHOD__])); + $this->assertTrue(isset($code[$finger])); } function testDestroySafely() { diff --git a/tests/MySQLNoEventTest.php b/tests/MySQLNoEventTest.php index 0c7a37b..200fb4b 100644 --- a/tests/MySQLNoEventTest.php +++ b/tests/MySQLNoEventTest.php @@ -1,41 +1,53 @@ assertFalse(Sess::isActive()); - (new Sess($this->client, $this->cfg))->start(); - - $this->assertEquals(1, ini_get('session.gc_probability')); - $this->assertEquals((int)$this->cfg->gc, ini_get('session.gc_divisor')); - $this->assertEquals((int)$this->cfg->timeout, ini_get('session.gc_maxlifetime')); - } - - function testGc() { - $gc = 1; - $cfg = new Cfg([Cfg::CFG_SECURE => false, - Cfg::CFG_TIMEOUT => $gc, - Cfg::CFG_SAVE_CLI => true, - Cfg::CFG_GC => $gc]); - - $sess = (new Sess($this->client, $cfg))->start(); - $id = session_id(); - $_SESSION[__METHOD__] = 'foo'; - session_write_close(); - - $this->assertNotEmpty($this->client->query('SELECT `data` FROM `' . $this->cfg->table . '` WHERE `id`=\'' . - $id . '\'')->fetchAll(PDO::FETCH_ASSOC)); - sleep(2); - $sess->gc($gc); - $this->assertEmpty($this->client->query('SELECT `data` FROM `' . $this->cfg->table . '` WHERE `id`=\'' . - $id . '\'')->fetchAll(PDO::FETCH_ASSOC)); - } +namespace AloFramework\Session\Tests; + +use AloFramework\Session\Config as Cfg; +use AloFramework\Session\MySQLNoEventSession as Sess; +use PDO; +use PHPUnit_Framework_TestCase; + +require_once 'MySQLTest.php'; + +class MySQLNoEventTest extends PHPUnit_Framework_TestCase { + /** @var Cfg */ + protected $cfg; + /** @var PDO */ + protected $client; + + function __construct($name = null, array $data = [], $dataName = '') { + parent::__construct($name, $data, $dataName); + $this->cfg = new Cfg([Cfg::CFG_SECURE => false, + Cfg::CFG_SAVE_CLI => true]); + $this->client = MySQLTest::initPDO(); } + + function testStart() { + $this->assertFalse(Sess::isActive()); + (new Sess($this->client, $this->cfg))->start(); + + $this->assertEquals(1, ini_get('session.gc_probability')); + $this->assertEquals((int)$this->cfg->gc, ini_get('session.gc_divisor')); + $this->assertEquals((int)$this->cfg->timeout, ini_get('session.gc_maxlifetime')); + } + + function testGc() { + $gc = 1; + $cfg = new Cfg([Cfg::CFG_SECURE => false, + Cfg::CFG_TIMEOUT => $gc, + Cfg::CFG_SAVE_CLI => true, + Cfg::CFG_GC => $gc]); + + $sess = (new Sess($this->client, $cfg))->start(); + $id = session_id(); + $_SESSION[__METHOD__] = 'foo'; + session_write_close(); + + $this->assertNotEmpty($this->client->query('SELECT `data` FROM `' . $this->cfg->table . '` WHERE `id`=\'' . + $id . '\'')->fetchAll(PDO::FETCH_ASSOC)); + sleep(2); + $sess->gc($gc); + $this->assertEmpty($this->client->query('SELECT `data` FROM `' . $this->cfg->table . '` WHERE `id`=\'' . + $id . '\'')->fetchAll(PDO::FETCH_ASSOC)); + } +} diff --git a/tests/MySQLTest.php b/tests/MySQLTest.php index 1135893..d088aa3 100644 --- a/tests/MySQLTest.php +++ b/tests/MySQLTest.php @@ -1,24 +1,35 @@ client = new PDO('mysql:dbname=phpunit;host=localhost;charset=utf8mb4;port=3306', 'root', ''); - $this->client->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $this->client->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); - $this->client->query('CREATE TABLE IF NOT EXISTS `alo_session` ( + function __construct($name = null, array $data = [], $dataName = '') { + parent::__construct($name, $data, $dataName); + $this->cfg = new Cfg([Cfg::CFG_SECURE => false, + Cfg::CFG_SAVE_CLI => true]); + + $this->client = self::initPDO(); + } + + public static function initPDO() { + $pdo = new PDO('mysql:dbname=phpunit;host=localhost;charset=utf8mb4;port=3306', 'root', ''); + $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); + $pdo->query('CREATE TABLE IF NOT EXISTS `alo_session` ( `id` CHAR(128) CHARACTER SET ascii NOT NULL, `data` TEXT NOT NULL, @@ -28,50 +39,51 @@ function __construct($name = null, array $data = [], $dataName = '') { ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;')->execute(); - } + return $pdo; + } - function testRW() { - $this->assertFalse(Sess::isActive()); + function testRW() { + $this->assertFalse(Sess::isActive()); - (new Sess($this->client, $this->cfg))->start(); - $this->assertTrue(Sess::isActive()); - $prefix = session_id(); - $_SESSION[__METHOD__] = 'foo'; - session_write_close(); - $this->assertFalse(Sess::isActive()); + (new Sess($this->client, $this->cfg))->start(); + $this->assertTrue(Sess::isActive()); + $prefix = session_id(); + $_SESSION[__METHOD__] = 'foo'; + session_write_close(); + $this->assertFalse(Sess::isActive()); - $this->assertEquals($_SESSION[__METHOD__], 'foo'); - $select = - $this->client->query('SELECT `data` FROM `' . $this->cfg->table . '` WHERE `id`=\'' . $prefix . '\'') - ->fetchAll(PDO::FETCH_ASSOC); + $this->assertEquals($_SESSION[__METHOD__], 'foo'); + $select = + $this->client->query('SELECT `data` FROM `' . $this->cfg->table . '` WHERE `id`=\'' . $prefix . '\'') + ->fetchAll(PDO::FETCH_ASSOC); - $this->assertNotEmpty($select); + $this->assertNotEmpty($select); - $sess = self::sessionUnserialize($select[0]['data']); + $sess = AbstractSessionTest::sessionUnserialize($select[0]['data']); - $this->assertTrue(array_key_exists($this->cfg->fingerprint, $sess)); - $this->assertEquals('s:3:"foo"', $sess[__METHOD__]); - } + $this->assertTrue(array_key_exists($this->cfg->fingerprint, $sess)); + $this->assertEquals('s:3:"foo"', $sess[__METHOD__]); + } - function testDestroy() { - $this->assertFalse(Sess::isActive()); + function testDestroy() { + $this->assertFalse(Sess::isActive()); - $session = new Sess($this->client, $this->cfg); - $session->start(); - $this->assertTrue(Sess::isActive()); + $session = new Sess($this->client, $this->cfg); + $session->start(); + $this->assertTrue(Sess::isActive()); - $_SESSION[__METHOD__] = 1; - $session->write(session_id(), serialize($_SESSION)); + $_SESSION[__METHOD__] = 1; + $session->write(session_id(), serialize($_SESSION)); - $prefix = session_id(); + $prefix = session_id(); - $this->assertNotEmpty($this->client->query('SELECT `data` FROM `' . $this->cfg->table . '` WHERE `id`=\'' . - $prefix . '\'')->fetchAll(PDO::FETCH_ASSOC)); + $this->assertNotEmpty($this->client->query('SELECT `data` FROM `' . $this->cfg->table . '` WHERE `id`=\'' . + $prefix . '\'')->fetchAll(PDO::FETCH_ASSOC)); - session_destroy(); - $this->assertEmpty($this->client->query('SELECT `data` FROM `' . $this->cfg->table . '` WHERE `id`=\'' . - $prefix . '\'')->fetchAll(PDO::FETCH_ASSOC)); + session_destroy(); + $this->assertEmpty($this->client->query('SELECT `data` FROM `' . $this->cfg->table . '` WHERE `id`=\'' . + $prefix . '\'')->fetchAll(PDO::FETCH_ASSOC)); - $this->assertFalse(Sess::isActive()); - } + $this->assertFalse(Sess::isActive()); } +} diff --git a/tests/RedisTest.php b/tests/RedisTest.php index db011a5..491aab0 100644 --- a/tests/RedisTest.php +++ b/tests/RedisTest.php @@ -1,58 +1,64 @@ client = new \Redis(); - $this->client->connect('127.0.0.1'); - } + $this->client = new \Redis(); + $this->client->connect('127.0.0.1'); + $this->cfg = new Cfg([Cfg::CFG_SECURE => false, + Cfg::CFG_SAVE_CLI => true]); + } - function testRW() { - $this->assertFalse(Sess::isActive()); + function testRW() { + $this->assertFalse(Sess::isActive()); - (new Sess($this->client, $this->cfg))->start(); - $this->assertTrue(Sess::isActive()); - $prefix = $this->cfg->prefix . session_id(); - $_SESSION[__METHOD__] = 'foo'; - session_write_close(); - $this->assertFalse(Sess::isActive()); + (new Sess($this->client, $this->cfg))->start(); + $this->assertTrue(Sess::isActive()); + $prefix = $this->cfg->prefix . session_id(); + $_SESSION[__METHOD__] = 'foo'; + session_write_close(); + $this->assertFalse(Sess::isActive()); - $this->assertEquals($_SESSION[__METHOD__], 'foo'); - $this->assertTrue($this->client->exists($prefix)); + $this->assertEquals($_SESSION[__METHOD__], 'foo'); + $this->assertTrue($this->client->exists($prefix)); - $sess = self::sessionUnserialize($this->client->get($prefix)); + $sess = AbstractSessionTest::sessionUnserialize($this->client->get($prefix)); - $this->assertTrue(array_key_exists($this->cfg->fingerprint, $sess)); - $this->assertEquals('s:3:"foo"', $sess[__METHOD__]); - } + $this->assertTrue(array_key_exists($this->cfg->fingerprint, $sess)); + $this->assertEquals('s:3:"foo"', $sess[__METHOD__]); + } - function testDestroy() { - $this->assertFalse(Sess::isActive()); + function testDestroy() { + $this->assertFalse(Sess::isActive()); - $session = new Sess($this->client, $this->cfg); - $session->start(); - $this->assertTrue(Sess::isActive()); + $session = new Sess($this->client, $this->cfg); + $session->start(); + $this->assertTrue(Sess::isActive()); - $_SESSION[__METHOD__] = 1; - $session->write(session_id(), $_SESSION); + $_SESSION[__METHOD__] = 1; + $session->write(session_id(), $_SESSION); - $prefix = $this->cfg->prefix . session_id(); - $this->assertTrue($this->client->exists($prefix)); + $prefix = $this->cfg->prefix . session_id(); + $this->assertTrue($this->client->exists($prefix)); - session_destroy(); - $this->assertFalse($this->client->exists($prefix)); + session_destroy(); + $this->assertFalse($this->client->exists($prefix)); - $this->assertFalse(Sess::isActive()); - } + $this->assertFalse(Sess::isActive()); } +}