Add namespace.

This commit is contained in:
2014-06-02 18:58:49 +04:00
parent aec1a60985
commit 1ba341b064
159 changed files with 265 additions and 264 deletions

View File

@ -0,0 +1,110 @@
<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-11-15
*
* Unit tests for SessionModel class
*/
require_once dirname(__FILE__) . '/../../Registry.php';
require_once dirname(__FILE__) . '/../../Config.php';
require_once dirname(__FILE__) . '/../../classes/Env.class.php';
require_once dirname(__FILE__) . '/../../model/Db.php';
require_once dirname(__FILE__) . '/../../model/DbDriver.php';
require_once dirname(__FILE__) . '/../model/MyDbDriver.php';
require_once dirname(__FILE__) . '/../../model/Model.php';
require_once dirname(__FILE__) . '/../../model/SqlModel.php';
require_once dirname(__FILE__) . '/../../session/Session.model.php';
class SessionModelTest extends PHPUnit_Framework_TestCase
{
protected $model;
public function setUp()
{
$conf = array('default' => array('driver' => 'MockDbDriver', 'hostname' => 'somehost', 'database' => 'db', 'username' => 'test', 'password' => '1234'));
if (!class_exists('MockDbDriver')) {
$this->getMockForAbstractClass('MyDbDriver', array($conf), 'MockDbDriver', false);
}
if (!class_exists('MockDbExpr')) {
$this->getMock('DbExpr', array(), array(), 'MockDbExpr', false);
}
Config::set('Db', $conf);
set_new_overload(array($this, 'newCallback'));
}
public function testOpen()
{
$this->model = new SessionModel();
$this->assertTrue($this->model->open('path', 'name'));
}
public function testClose()
{
$this->model = new SessionModel();
$this->assertTrue($this->model->close());
}
public function testRead()
{
$this->model = new SessionModel();
$this->assertEquals('data', $this->model->read(1));
}
public function testWrite()
{
$this->model = new SessionModel();
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$this->assertEmpty(Env::Server('HTTP_X_FORWARDED_FOR'));
$this->assertTrue($this->model->write(2, 'user|.s:20;id=2;id=2'));
}
public function testDestroy()
{
$this->model = new SessionModel();
$this->assertTrue($this->model->destroy(2));
}
public function testGc()
{
$this->model = new SessionModel();
$this->assertTrue($this->model->gc(2000));
}
public function testDestroyByUserId()
{
$this->model = new SessionModel();
$this->assertEquals('session', $this->model->destroyByUserId(12));
}
public function tearDown()
{
Config::getInstance()->offsetUnset('Db');
$config = new ReflectionClass('Db');
$registry = $config->getProperty('connections');
$registry->setAccessible(true);
$registry->setValue('Db', array());
unset_new_overload();
}
protected function newCallback($className)
{
switch ($className) {
case 'DbExpr':
return 'MockDbExpr';
case 'MockDbDriver':
return 'MockDbDriver';
case 'CacheKey':
return 'MockCacheKey';
default:
return $className;
}
}
}

View File

@ -0,0 +1,187 @@
<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-10-25
*
* Unit tests for Session class
* @TODO: Session::destroy() - check if session was started
* @TODO: Session::destroy() - uncheck started flag after destroy
*/
require_once dirname(__FILE__) . '/../../session/Session.php';
class SessionTest extends PHPUnit_Framework_TestCase
{
private $started = null;
protected function setUp()
{
$class = new ReflectionClass('Session');
$this->started = $class->getProperty('started');
$this->started->setAccessible(true);
Session::start();
$sid = Session::getId();
if (!empty($sid)) {
Session::destroy();
}
$this->started->setValue(null, false);
}
public function testStart()
{
Session::start();
$this->assertAttributeEquals(true, 'started', 'Session');
}
public function testSetGet()
{
Session::set('one', 1);
Session::set('two', 'three');
Session::set(array('first' => '1st', 'second' => '2nd'));
$this->assertSame('1st', Session::get('first'));
$this->assertSame('three', Session::get('two'));
$this->assertNotEquals('three', Session::get('thr'));
}
public function testNullKey()
{
$this->assertNull(Session::get());
Session::start();
$this->assertEmpty(Session::get());
}
public function testReturnDefaultValue()
{
Session::start();
$this->assertSame(1, Session::get('key', 1));
}
public function testDestroyedGet()
{
$this->assertFalse($this->started->getValue());
$_COOKIE[session_name()] = session_name();
$this->assertSame(1, Session::get('key', 1));
}
public function testDel()
{
Session::set('one', 1);
Session::set('two', 'three');
$this->assertSame('three', Session::get('two'));
Session::del('two');
$this->assertNull(Session::get('two'));
}
public function testDestroyedDel()
{
Session::del('two');
$this->assertNull(Session::get('two'));
$this->assertFalse($this->started->getValue());
$_COOKIE[session_name()] = session_name();
Session::del('two');
$this->assertNull(Session::get('two'));
}
public function testRegenerateId()
{
$this->assertEmpty(session_id());
Session::start();
$ssid = Session::getId();
$this->assertNotEmpty($ssid);
Session::regenerateId();
$new_ssid = Session::getId();
$this->assertNotEmpty($new_ssid);
$this->assertNotEquals($new_ssid, $ssid);
}
public function testRememberUntil()
{
Session::start();
$ssid = Session::getId();
$params = session_get_cookie_params();
Session::rememberUntil(400);
$new_ssid = Session::getId();
$new_params = session_get_cookie_params();
$this->assertNotEquals($ssid, $new_ssid);
$this->assertNotEquals($params, $new_params);
$this->assertSame(400, $new_params['lifetime']);
Session::rememberUntil();
$new_params = session_get_cookie_params();
$this->assertSame(0, $new_params['lifetime']);
}
public function testForget()
{
Session::start();
$ssid = Session::getId();
Session::forget();
$new_ssid = Session::getId();
$new_params = session_get_cookie_params();
$this->assertNotEquals($ssid, $new_ssid);
$this->assertSame(0, $new_params['lifetime']);
}
public function testRemember()
{
Session::start();
$ssid = Session::getId();
Session::remember();
$new_params = session_get_cookie_params();
$this->assertSame(1209600, $new_params['lifetime']);
Session::remember(-30);
$new_params = session_get_cookie_params();
$this->assertSame(1209600, $new_params['lifetime']);
Session::remember(530);
$new_params = session_get_cookie_params();
$this->assertSame(530, $new_params['lifetime']);
}
public function testExpireSessionCookie()
{
Session::start();
$params = session_get_cookie_params();
$_COOKIE[session_name()] = true;
Session::expireSessionCookie();
$this->assertNotNull($_COOKIE);
}
public function testSetSessionHandler()
{
Session::setSessionHandler('Handler');
$this->assertTrue(TRUE);
}
}
class Handler
{
public static function open()
{
}
public static function close()
{
}
public static function read()
{
}
public static function write()
{
}
public static function destroy()
{
}
public static function gc()
{
}
}