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 @@
- 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 @@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 @@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) {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">//@codeCoverageIgnoreEndGenerated 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 @@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 @@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 @@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 @@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()); } +}