<?php<liu21st@gmail.com>
/**
* Session测试
* @author 大漠 <zhylninc@gmail.com>
*/
namespace tests\thinkphp\library\think;
use think\Session;
class sessionTest extends \PHPUnit_Framework_TestCase
{
protected $object;
protected function setUp()
{
set_exception_handler(function () {});
set_error_handler(function () {});
}
protected function tearDown()
{
register_shutdown_function('think\Error::appShutdown');
set_error_handler('think\Error::appError');
set_exception_handler('think\Error::appException');
}
public function testPrefix()
{
Session::prefix(null);
Session::prefix('think_');
$this->assertEquals('think_', Session::prefix());
}
public function testInit()
{
Session::prefix(null);
$config = [
'prefix' => 'think_',
'expire' => 60,
'path' => '/path/to/test/session/',
'domain' => '.thinkphp.cn',
'var_session_id' => 'sessionidtest',
'id' => 'sess_8fhgkjuakhatbeg2fa14lo84q1',
'name' => 'session_name',
'use_trans_sid' => '1',
'use_cookies' => '1',
'cache_limiter' => '60',
'cache_expire' => '60',
'type' => '', 'namespace' => '\\think\\session\\driver\\', 'auto_start' => '1',
];
$_REQUEST[$config['var_session_id']] = $config['id'];
Session::init($config);
$this->assertEquals($config['prefix'], Session::prefix());
$this->assertEquals($config['id'], $_REQUEST[$config['var_session_id']]);
$this->assertEquals($config['name'], session_name());
$this->assertEquals($config['path'], session_save_path());
$this->assertEquals($config['use_cookies'], ini_get('session.use_cookies'));
$this->assertEquals($config['domain'], ini_get('session.cookie_domain'));
$this->assertEquals($config['expire'], ini_get('session.gc_maxlifetime'));
$this->assertEquals($config['expire'], ini_get('session.cookie_lifetime'));
$this->assertEquals($config['cache_limiter'], session_cache_limiter($config['cache_limiter']));
$this->assertEquals($config['cache_expire'], session_cache_expire($config['cache_expire']));
$_REQUEST[$config['var_session_id']] = null;
session_write_close();
session_destroy();
Session::init($config);
if (strstr(PHP_VERSION, 'hhvm')) {
$this->assertEquals('', ini_get('session.auto_start'));
} else {
$this->assertEquals(0, ini_get('session.auto_start'));
}
$this->assertEquals($config['use_trans_sid'], ini_get('session.use_trans_sid'));
Session::init($config);
$this->assertEquals($config['id'], session_id());
}
public function testException()
{
$config = [
'prefix' => 'think_',
'expire' => 0,
'path' => '/path/to/test/session/',
'domain' => '.thinkphp.cn',
'var_session_id' => 'sessionidtest',
'id' => 'sess_8fhgkjuakhatbeg2fa14lo84q1',
'name' => 'session_name',
'use_trans_sid' => '1',
'use_cookies' => '1',
'cache_limiter' => '60',
'cache_expire' => '60',
'type' => '\\think\\session\\driver\\Memcache', 'auto_start' => '1',
];
$this->setExpectedException('\think\exception\ClassNotFoundException', 'error session handler');
Session::init($config);
}
public function testSet()
{
Session::prefix(null);
Session::set('sessionname', 'sessionvalue');
$this->assertEquals('sessionvalue', $_SESSION['sessionname']);
Session::set('sessionnamearr.subname', 'sessionvalue');
$this->assertEquals('sessionvalue', $_SESSION['sessionnamearr']['subname']);
Session::set('sessionnameper', 'sessionvalue', 'think_');
$this->assertEquals('sessionvalue', $_SESSION['think_']['sessionnameper']);
Session::set('sessionnamearrper.subname', 'sessionvalue', 'think_');
$this->assertEquals('sessionvalue', $_SESSION['think_']['sessionnamearrper']['subname']);
}
public function testGet()
{
Session::prefix(null);
Session::set('sessionnameget', 'sessionvalue');
$this->assertEquals(Session::get('sessionnameget'), $_SESSION['sessionnameget']);
Session::set('sessionnamegetarr.subname', 'sessionvalue');
$this->assertEquals(Session::get('sessionnamegetarr.subname'), $_SESSION['sessionnamegetarr']['subname']);
Session::set('sessionnamegetarrperall', 'sessionvalue', 'think_');
$this->assertEquals(Session::get('', 'think_')['sessionnamegetarrperall'], $_SESSION['think_']['sessionnamegetarrperall']);
Session::set('sessionnamegetper', 'sessionvalue', 'think_');
$this->assertEquals(Session::get('sessionnamegetper', 'think_'), $_SESSION['think_']['sessionnamegetper']);
Session::set('sessionnamegetarrper.subname', 'sessionvalue', 'think_');
$this->assertEquals(Session::get('sessionnamegetarrper.subname', 'think_'), $_SESSION['think_']['sessionnamegetarrper']['subname']);
}
public function testPull()
{
Session::prefix(null);
Session::set('sessionnamedel', 'sessionvalue');
$this->assertEquals('sessionvalue', Session::pull('sessionnameget'));
$this->assertNull(Session::get('sessionnameget'));
}
public function testDelete()
{
Session::prefix(null);
Session::set('sessionnamedel', 'sessionvalue');
Session::delete('sessionnamedel');
$this->assertEmpty($_SESSION['sessionnamedel']);
Session::set('sessionnamedelarr.subname', 'sessionvalue');
Session::delete('sessionnamedelarr.subname');
$this->assertEmpty($_SESSION['sessionnamedelarr']['subname']);
Session::set('sessionnamedelper', 'sessionvalue', 'think_');
Session::delete('sessionnamedelper', 'think_');
$this->assertEmpty($_SESSION['think_']['sessionnamedelper']);
Session::set('sessionnamedelperarr.subname', 'sessionvalue', 'think_');
Session::delete('sessionnamedelperarr.subname', 'think_');
$this->assertEmpty($_SESSION['think_']['sessionnamedelperarr']['subname']);
}
public function testClear()
{
Session::prefix(null);
Session::set('sessionnameclsper', 'sessionvalue1', 'think_');
Session::clear('think_');
$this->assertNull($_SESSION['think_']);
Session::set('sessionnameclsper', 'sessionvalue1', 'think_');
Session::clear();
$this->assertEmpty($_SESSION);
}
public function testHas()
{
Session::prefix(null);
Session::set('sessionnamehas', 'sessionvalue');
$this->assertTrue(Session::has('sessionnamehas'));
Session::set('sessionnamehasarr.subname', 'sessionvalue');
$this->assertTrue(Session::has('sessionnamehasarr.subname'));
Session::set('sessionnamehasper', 'sessionvalue', 'think_');
$this->assertTrue(Session::has('sessionnamehasper', 'think_'));
Session::set('sessionnamehasarrper.subname', 'sessionvalue', 'think_');
$this->assertTrue(Session::has('sessionnamehasarrper.subname', 'think_'));
}
public function testPause()
{
Session::pause();
}
public function testStart()
{
Session::start();
}
public function testDestroy()
{
Session::set('sessionnamedestroy', 'sessionvalue');
Session::destroy();
$this->assertEmpty($_SESSION['sessionnamedestroy']);
}
}