From 2f3a0d170fca694ed62589d01329f274a360b404 Mon Sep 17 00:00:00 2001 From: Anton Grebnev Date: Thu, 17 Nov 2011 15:13:53 +0400 Subject: [PATCH] Session class tested and modified for new Model hierarchy --- session/Session.model.php | 2 +- tests/session/SessionModelTest.php | 110 +++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 tests/session/SessionModelTest.php diff --git a/session/Session.model.php b/session/Session.model.php index 22d57cd..769be5e 100644 --- a/session/Session.model.php +++ b/session/Session.model.php @@ -9,7 +9,7 @@ * @filesource $URL$ */ -class SessionModel extends Model +class SessionModel extends SqlModel { protected $life_time; diff --git a/tests/session/SessionModelTest.php b/tests/session/SessionModelTest.php new file mode 100644 index 0000000..f608d04 --- /dev/null +++ b/tests/session/SessionModelTest.php @@ -0,0 +1,110 @@ + + * @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; + } + } +}