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

76
Tests/app/ActionTest.php Normal file
View File

@ -0,0 +1,76 @@
<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-11-1
*
* Unit tests for Action class
*/
require_once dirname(__FILE__) . '/Action_TestCase.php';
class ActionTest extends Action_TestCase
{
/**
* @runInSeparateProcess
*/
public function testActionConstructWithParams()
{
Config::set('DEBUG', false);
Env::setParams(array('param1' => 'value1', 'param2' => 'value2'));
$action = $this->getMockForAbstractClass('Action' );
$this->assertSame('value1', $action->param1);
}
/**
* @runInSeparateProcess
*/
public function testFetch()
{
Config::set('DEBUG', false);
$load = new ReflectionClass('Load');
$classes = $load->getProperty('autoload');
$classes->setAccessible(true);
$classes->setValue('autoload', array('ActionMock' =>'some/path/to/action.php'));
Env::setParams(array('template' => 'SomeTemplate', 'param2' => 'value2'));
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = $this->getMockForAbstractClass('Action', array(), 'ActionMock');
$result = $action->fetch();
$this->assertSame('/actions/to/SomeTemplate', $result['template']);
}
/**
* @runInSeparateProcess
*/
public function testFetchNoTemplate()
{
Config::set('DEBUG', false);
Env::setParams(array('template' => ''));
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = $this->getMockForAbstractClass('Action', array(), 'ActionMock');
$result = $action->fetch();
$this->assertSame('/actions//Acti', $result['template']);
}
/**
* @runInSeparateProcess
*/
public function testRedirect()
{
set_exit_overload(function() { return false; });
Config::set('DEBUG', false);
$load = new ReflectionClass('Action');
$redirect = $load->getMethod('redirect');
$redirect->setAccessible(true);
$action = $this->getMockForAbstractClass('Action', array(), 'ActionMock');
$this->assertNull($redirect->invoke($action, '/some/url'));
unset_exit_overload();
}
}

View File

@ -0,0 +1,70 @@
<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-11-1
*
* Action_TestCase class for testing Actions
*/
require_once dirname(__FILE__) . '/../../Registry.php';
require_once dirname(__FILE__) . '/../../Config.php';
require_once dirname(__FILE__) . '/../../Load.php';
require_once dirname(__FILE__) . '/../../classes/Env.class.php';
require_once dirname(__FILE__) . '/../../exception/ErrorHandler.php';
require_once dirname(__FILE__) . '/../../app/FrontController.php';
require_once dirname(__FILE__) . '/../../app/Action.php';
require_once dirname(__FILE__) . '/../../view/iView.php';
class Action_TestCase extends PHPUnit_Framework_TestCase
{
public function run(PHPUnit_Framework_TestResult $result = NULL)
{
$this->setPreserveGlobalState(false);
return parent::run($result);
}
public function setUp()
{
$this->getMock('Router');
$this->getMock('PHPView', array('fetch', 'assignObject'));
}
public function tearDown()
{
$env = new ReflectionClass('Env');
$params = $env->getProperty('params');
$params->setAccessible(true);
$params->setValue('Env', array());
}
}
class SomeView implements iView
{
private $result = array();
public function fetch($template)
{
$this->result['template'] = $template;
return $this->result;
}
public function assignObject() {}
public function assign($name, $value = null) {
$this->result[$name] = $value;
}
public function prepend($name, $value)
{
}
public function append($name, $value)
{
}
}

View File

@ -0,0 +1,93 @@
<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-11-01
*
* Unit tests for AjaxAction class
*/
require_once dirname(__FILE__) . '/Action_TestCase.php';
require_once dirname(__FILE__) . '/../../app/AjaxAction.php';
class AjaxActionTest extends Action_TestCase
{
/**
* @runInSeparateProcess
*/
public function testConstruct()
{
Config::set('DEBUG', false);
Env::setParams(array('ajax' => 'AjaxTemplate', 'param2' => 'value2'));
$action = $this->getMockForAbstractClass('AjaxAction');
$this->assertAttributeEquals('ajax', 'template', $action);
}
/**
* @runInSeparateProcess
*/
public function testFetchWithEncode()
{
Config::set('DEBUG', false);
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = $this->getMockForAbstractClass('AjaxAction');
$action->data = array('var' => 'val');
$result = $action->fetch();
$this->assertSame('/actions//ajax', $result['template']);
$this->assertSame('{"var":"val"}', $result['data']);
}
/**
* @runInSeparateProcess
*/
public function testFetchNoEncode()
{
Config::set('DEBUG', false);
Env::setParams(array('json_encode' => false));
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = $this->getMockForAbstractClass('AjaxAction');
$action->data = array('var' => 'val');
$result = $action->fetch();
$this->assertSame('/actions//ajax', $result['template']);
$this->assertSame('Array', (string) $result['data']);
$action->data = 'stringvalue';
$result = $action->fetch();
$this->assertSame('/actions//ajax', $result['template']);
$this->assertSame('stringvalue', (string) $result['data']);
}
/**
* @runInSeparateProcess
*/
public function testFetchWithEncodeDefault()
{
Config::set('DEBUG', false);
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = $this->getMockForAbstractClass('AjaxAction');
$result = $action->fetch();
$this->assertSame('/actions//ajax', $result['template']);
$this->assertSame('false', (string) $result['data']);
}
/**
* @runInSeparateProcess
*/
public function testFetchNoEncodeDefault()
{
Config::set('DEBUG', false);
Env::setParams(array('json_encode' => false));
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = $this->getMockForAbstractClass('AjaxAction');
$result = $action->fetch();
$this->assertSame('/actions//ajax', $result['template']);
$this->assertSame('', (string) $result['data']);
}
}

View File

@ -0,0 +1,120 @@
<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage Tests app
* @since 10.07.12
*
*/
require_once __DIR__ . '/../../util/profiler/Profiler.php';
require_once __DIR__ . '/../../exception/GeneralException.php';
require_once __DIR__ . '/../../exception/ErrorHTTPException.php';
require_once __DIR__ . '/../../exception/Error404Exception.php';
require_once __DIR__ . '/../../exception/ErrorHandler.php';
require_once __DIR__ . '/../../app/CliController.php';
require_once __DIR__ . '/../../Registry.php';
require_once __DIR__ . '/../../Config.php';
require_once __DIR__ . '/../../app/iCli.php';
require_once __DIR__ . '/../../logger/Logger.php';
require_once __DIR__ . '/../../logger/CliLogger.php';
/**
* @desc CliController tests
* @author Aleksandr Demidov
*/
class CliControllerTest extends PHPUnit_Framework_TestCase
{
protected $stream;
public function testGetInstance()
{
$instance = CliController::getInstance();
$this->assertInstanceOf('CliController', $instance);
}
public function testExecute()
{
Config::set('PROFILER', false);
$cli_class = $this->getMockForAbstractClass('iCli', array(), '', '', '', '', array('run'));
$cli_class->expects($this->once())
->method('run')
->with();
CliController::getInstance()->execute($cli_class);
}
public function testExecuteWithProfiler()
{
ob_start();
Config::set('PROFILER', true);
$cli_class = $this->getMockForAbstractClass('iCli', array(), '', '', '', '', array('run'));
$cli_class->expects($this->once())
->method('run')
->with();
CliController::getInstance()->execute($cli_class);
$output = ob_get_clean();
$this->assertContains('Elapsed time:', $output);
}
public function testExecuteWithLogger()
{
ob_start();
Config::set('PROFILER', true);
Config::set('LOGGING', true);
Config::set('Logger', array('logger' => 'CliLogger'));
$cli_class = $this->getMockForAbstractClass('iCli', array(), '', '', '', '', array('run'));
$cli_class->expects($this->once())
->method('run')
->with();
CliController::getInstance()->execute($cli_class);
$output = ob_get_clean();
$this->assertContains('Elapsed time:', $output);
}
/**
* @runInSeparateProcess
*/
public function testExecuteImplementErrorToFile()
{
Config::set('ErrorStream', __DIR__ . '/temp.txt');
touch(Config::get('ErrorStream'));
$cli_class = new StdClass();
CliController::getInstance()->execute($cli_class);
$this->assertContains('Runner "' . get_class($cli_class) . '" need implement of "iCli" interface.', file_get_contents(Config::get('ErrorStream')));
unlink(Config::get('ErrorStream'));
}
/**
* @runInSeparateProcess
*/
public function testExecuteImplementErrorToConsole()
{
Config::set('ErrorStream', 'php://output');
$cli_class = new StdClass();
$this->expectOutputRegex('/.*Runner "' . get_class($cli_class) . '" need implement of "iCli" interface\..*/');
CliController::getInstance()->execute($cli_class);
}
/**
* @runInSeparateProcess
*/
public function testExecuteWithRunThrowError()
{
Config::set('ErrorStream', 'php://output');
Config::set('PROFILER', false);
$cli_class = $this->getMockForAbstractClass('iCli', array(), '', '', '', '', array('run'));
$cli_class->expects($this->once())
->method('run')
->with()
->will($this->returnCallback(array($this, 'callbackWithThrow')));
$this->expectOutputRegex('/.*Error from callback\..*/');
CliController::getInstance()->execute($cli_class);
}
public function callbackWithThrow()
{
throw new ErrorException('Error from callback.');
}
}

View File

@ -0,0 +1,162 @@
<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-11-1
*
* Unit tests for ErrorAction class
*/
require_once dirname(__FILE__) . '/Action_TestCase.php';
require_once dirname(__FILE__) . '/../../app/ErrorAction.php';
require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
require_once dirname(__FILE__) . '/../../exception/ErrorHTTPException.php';
require_once dirname(__FILE__) . '/../../exception/Error404Exception.php';
class ErrorActionTest extends Action_TestCase
{
private $log;
public function setUp()
{
parent::setUp();
$this->log = ini_get('error_log');
ini_set('error_log', '/dev/null');
set_exit_overload(function () {
return false;
});
}
/**
* @runInSeparateProcess
*/
public function testErrorExceptionNotice()
{
$this->setConstants(false);
$exception = $this->getMock('ErrorException', array(), array('', 0, E_NOTICE));
$action = new ErrorAction($exception);
$this->assertSame($exception, $action->exception);
}
/**
* @runInSeparateProcess
*/
public function testErrorExceptionWarning()
{
$this->setConstants(false);
$exception = $this->getMock('ErrorException', array(), array('', 0, E_WARNING));
$action = new ErrorAction($exception);
$this->assertSame($exception, $action->exception);
}
/**
* @runInSeparateProcess
*/
public function testErrorExceptionError()
{
$this->setConstants(false);
$exception = $this->getMock('ErrorException', array(), array('', 0, E_ERROR));
$action = new ErrorAction($exception);
$this->assertSame($exception, $action->exception);
}
/**
* @runInSeparateProcess
*/
public function testErrorExceptionCustom()
{
$this->setConstants(false);
$exception = $this->getMock('ErrorException', array(), array('', 0, 211));
$action = new ErrorAction($exception);
$this->assertSame($exception, $action->exception);
}
/**
* @runInSeparateProcess
*/
public function testFetchNoTemplate()
{
Config::set('DEBUG', false);
$exception = $this->getMock('ErrorException');
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = new ErrorAction($exception);
$result = $action->fetch();
$this->assertSame('/actions/500', $result['template']);
}
/**
* @runInSeparateProcess
*/
public function testError404WithAjax()
{
$this->setConstants(false);
$exception = new Error404Exception('Some message');
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = new ErrorAction($exception);
$action->setAjaxError();
$this->assertSame($exception, $action->exception);
$result = $action->fetch();
$this->assertSame('Some message', $result);
}
/**
* @runInSeparateProcess
*/
public function testError404NoAjax()
{
$this->setConstants(false);
$exception = new Error404Exception('Some message');
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = new ErrorAction($exception);
$this->assertSame($exception, $action->exception);
$result = $action->fetch();
$this->assertSame('/actions/404', $result['template']);
}
/**
* @runInSeparateProcess
*/
public function testErrorHTTP()
{
$this->setConstants(false);
$exception = new ErrorHTTPException('Some message', 410);
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = new ErrorAction($exception);
$this->assertSame($exception, $action->exception);
$result = $action->fetch();
$this->assertSame('/actions/HTTP', $result['template']);
}
private function setConstants($val = false)
{
Config::set('DEBUG', $val);
}
private function header()
{
ob_end_clean();
flush();
}
public function tearDown()
{
parent::tearDown();
ini_set('error_log', $this->log);
unset_exit_overload();
}
}

View File

@ -0,0 +1,311 @@
<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-11-1
*
* Unit tests for FrontController class
*/
require_once dirname(__FILE__) . '/../../session/Session.php';
require_once dirname(__FILE__) . '/../../classes/Env.class.php';
require_once dirname(__FILE__) . '/../../Registry.php';
require_once dirname(__FILE__) . '/../../Config.php';
require_once dirname(__FILE__) . '/../../util/FirePHPCore-0.3.2/lib/FirePHPCore/fb.php';
require_once dirname(__FILE__) . '/../../util/profiler/Profiler.php';
require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
require_once dirname(__FILE__) . '/../../exception/ErrorHTTPException.php';
require_once dirname(__FILE__) . '/../../exception/Error404Exception.php';
require_once dirname(__FILE__) . '/../../exception/ErrorHandler.php';
require_once dirname(__FILE__) . '/../../app/router/Route.php';
require_once dirname(__FILE__) . '/../../app/router/Router.php';
require_once dirname(__FILE__) . '/../../app/FrontController.php';
require_once dirname(__FILE__) . '/../../app/Action.php';
require_once dirname(__FILE__) . '/../../app/AjaxAction.php';
class FrontControllerTest extends PHPUnit_Framework_TestCase
{
protected $log_file_name = 'error_log_file';
public function run(PHPUnit_Framework_TestResult $result = NULL)
{
$this->setPreserveGlobalState(false);
return parent::run($result);
}
public function setUp()
{
if (!class_exists('PHPViewMock')) {
$this->getMock('PHPView', array('fetch', 'append', 'prepend', 'assign', 'getTemplate'), array(), 'PHPViewMock', false);
}
if (!class_exists('View')) {
$this->getMock('View');
}
if (!class_exists('ErrorLayout')) {
$this->getMock('ErrorLayout', array('fetch', 'setException'), array(), 'ErrorLayoutMock');
}
if (!class_exists('ErrorActionMock')) {
$this->getMock('ErrorAction', array('setAjaxError'), array(), 'ErrorActionMock', false);
}
set_new_overload(array($this, 'newCallback'));
}
/**
* @runInSeparateProcess
*/
public function testGetInstanceNoProfiler()
{
$this->setConstants(false);
$controller = FrontController::getInstance();
$this->assertAttributeEquals($controller, 'instance', 'FrontController');
}
/**
* @runInSeparateProcess
*/
public function testGetInstanceWithProfiler()
{
$this->setConstants(true);
$controller = FrontController::getInstance();
$this->assertAttributeEquals($controller, 'instance', 'FrontController');
}
/**
* @runInSeparateProcess
*/
public function testSetView()
{
$this->setConstants(false);
$controller = FrontController::getInstance();
$this->assertSame($controller, $controller->setView('View'));
}
/**
* @runInSeparateProcess
*/
public function testGetDefaultView()
{
$this->setConstants(false);
$controller = FrontController::getInstance();
$this->assertNotInstanceOf('View', $controller->getView());
$this->assertInstanceOf('PHPView', $controller->getView());
}
/**
* @runInSeparateProcess
*/
public function testGetCustomView()
{
$this->setConstants(false);
$controller = FrontController::getInstance();
$this->assertInstanceOf('View', $controller->getView('View'));
}
/**
* @runInSeparateProcess
*/
public function testSetGetBaseUrl()
{
$this->setConstants(false);
$controller = FrontController::getInstance();
$this->assertSame('', $controller->getBaseUrl());
$controller->setBaseUrl('/index/');
$this->assertSame('/index', $controller->getBaseUrl());
}
/**
* @runInSeparateProcess
*/
public function testGetRouter()
{
$this->setConstants(false);
$controller = FrontController::getInstance();
$this->assertInstanceOf('Router', $controller->getRouter());
}
/**
* @runInSeparateProcess
*/
public function testExecuteNoRoute()
{
$this->setConstants(false);
$controller = FrontController::getInstance();
$this->assertNull($controller->execute());
}
/**
* @runInSeparateProcess
*/
public function testExecuteNoRouteDebug()
{
$this->setConstants(true);
ini_set('error_log', $this->log_file_name);
$controller = FrontController::getInstance();
$result = $controller->execute();
$this->assertNotEmpty($result);
$this->assertContains('Route for "" not found', $result);
$this->assertContains('Error404Exception', $result);
$error = file_get_contents($this->log_file_name);
$this->assertContains('PHP Unknown Error: Error404Exception: Route for "" not found in ', $error);
}
/**
* @runInSeparateProcess
*/
public function testExecuteNoAction()
{
$_SERVER['REQUEST_URI'] = '/user/account/213';
$this->setConstants(true);
ini_set('error_log', $this->log_file_name);
$controller = FrontController::getInstance();
$router = $controller->getRouter();
$router->add('user', 'user/account/:id', 'user');
$result = $controller->execute();
$this->assertContains('Action class "userAction" not found.', $result);
$error = file_get_contents($this->log_file_name);
$this->assertContains('PHP Unknown Error: GeneralException: Action class "userAction" not found. in ', $error);
}
/**
* @runInSeparateProcess
*/
public function testExecuteNoLayout()
{
$this->getMock('userAction');
$_SERVER['REQUEST_URI'] = '/user/account/213';
$this->setConstants(true);
ini_set('error_log', $this->log_file_name);
$controller = FrontController::getInstance();
$router = $controller->getRouter();
$router->add('user', 'user/account/:id', 'user');
$result = $controller->execute();
$this->assertContains('Layout class "DefaultLayout" not found.', $result);
$error = file_get_contents($this->log_file_name);
$this->assertContains('PHP Unknown Error: GeneralException: Layout class "DefaultLayout" not found. in ', $error);
}
/**
* @runInSeparateProcess
*/
public function testExecuteWithLayout()
{
$this->getMock('userAction');
$this->getMock('DefaultLayout', array('fetch'), array(), 'DefaultLayoutMock');
$_SERVER['REQUEST_URI'] = '/user/account/213';
$this->setConstants(true);
$controller = FrontController::getInstance();
$router = $controller->getRouter();
$router->add('user', 'user/account/:id', 'user');
$result = $controller->execute();
$this->assertNull($result);
}
/**
* @runInSeparateProcess
*/
public function testExecuteWithLayoutProfiler()
{
Config::set('PROFILER', true);
$this->getMock('userAction');
$this->getMock('DefaultLayout', array('fetch'), array(), 'DefaultLayoutMock');
$_SERVER['REQUEST_URI'] = '/user/account/213';
$this->setConstants(true);
$controller = FrontController::getInstance();
$router = $controller->getRouter();
$router->add('user', 'user/account/:id', 'user');
$result = $controller->execute();
$this->assertNull($result);
}
/**
* @runInSeparateProcess
*/
public function testExecuteWithAjaxAction()
{
$this->getMock('userAction');
$this->getMock('DefaultLayout', array('fetch'), array(), 'DefaultLayoutMock');
$_SERVER['REQUEST_URI'] = '/user/account/213';
$this->setConstants(true);
$controller = FrontController::getInstance();
$router = $controller->getRouter();
$router->add('user', 'user/account/:id', 'NewAjax');
$result = $controller->execute();
$this->assertNull($result);
}
/**
* @runInSeparateProcess
*/
public function testExecuteWithAjaxActionError()
{
$this->getMock('userAction');
$_SERVER['REQUEST_URI'] = '/user/account/213';
$this->setConstants(false);
$controller = FrontController::getInstance();
$router = $controller->getRouter();
$router->add('user', 'user/account/:id', 'NewAjax');
$result = $controller->execute();
$this->assertNull($result);
}
/**
* @runInSeparateProcess
*/
public function testExecuteWithAjaxActionProfiler()
{
Config::set('PROFILER', true);
$this->getMock('userAction');
$this->getMock('DefaultLayout', array('fetch'), array(), 'DefaultLayoutMock');
$_SERVER['REQUEST_URI'] = '/user/account/213';
$this->setConstants(true);
$controller = FrontController::getInstance();
$router = $controller->getRouter();
$router->add('user', 'user/account/:id', 'NewAjax');
$result = $controller->execute();
$this->assertNull($result);
}
private function setConstants($val = false)
{
Config::set('DEBUG', $val);
}
public function tearDown()
{
unset_new_overload();
if (file_exists($this->log_file_name) && is_writable($this->log_file_name)) {
unlink($this->log_file_name);
}
ini_set('error_log', 'php://stderr');
}
protected function newCallback($className)
{
switch ($className) {
case 'PHPView':
return 'PHPViewMock';
case 'DefaultLayout':
return 'DefaultLayoutMock';
case 'ErrorAction':
return 'ErrorActionMock';
case 'ErrorLayout':
return 'ErrorLayoutMock';
default:
return $className;
}
}
}
/**
* Used in testExecuteWithAjaxAction
*/
class NewAjaxAction extends AjaxAction
{
protected function execute()
{
}
}

View File

@ -0,0 +1,96 @@
<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-11-1
*
* Unit tests for PagerAction class
*/
require_once dirname(__FILE__) . '/Action_TestCase.php';
require_once dirname(__FILE__) . '/../../app/PagerAction.php';
class PagerActionTest extends Action_TestCase
{
/**
* @runInSeparateProcess
*/
public function testConstructWithParams()
{
Config::set('DEBUG', false);
$action = $this->getMockForAbstractClass('PagerAction');
$this->assertSame(20, $action->getLimit());
$action = $this->getMockForAbstractClass('PagerAction', array(50));
$this->assertSame(50, $action->getLimit());
}
/**
* @runInSeparateProcess
*/
public function testSetCount()
{
Config::set('DEBUG', false);
$action = $this->getMockForAbstractClass('PagerAction');
$action->setCount(50);
$this->assertSame(1, $action->page);
$this->assertSame(0, $action->getOffset());
$_GET['p'] = 'last';
$action->setCount(50);
$this->assertSame(3.0, $action->page);
$this->assertSame(40, $action->getOffset());
$_GET['p'] = 2;
$action->setCount(50);
$this->assertSame(2, $action->page);
$this->assertSame(20, $action->getOffset());
$_GET['p'] = -3;
$action->setCount(50);
$this->assertSame(1, $action->page);
}
/**
* @runInSeparateProcess
*/
public function testGetOffset()
{
Config::set('DEBUG', false);
$action = $this->getMockForAbstractClass('PagerAction');
$this->assertSame(0, $action->getOffset());
}
/**
* @runInSeparateProcess
*/
public function testFetchNoTemplate()
{
Config::set('DEBUG', false);
Env::setParams(array('template' => ''));
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = $this->getMockForAbstractClass('PagerAction', array(), 'PagerActionMock');
$result = $action->fetch();
$this->assertSame('/actions/PagerActi', $result['template']);
}
/**
* @runInSeparateProcess
*/
public function testFetchWithTemplate()
{
Config::set('DEBUG', false);
Env::setParams(array('template' => 'SomeTemplate'));
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = $this->getMockForAbstractClass('PagerAction');
$result = $action->fetch();
$this->assertSame('/actions/SomeTemplate', $result['template']);
}
}

View File

@ -0,0 +1,48 @@
<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-11-1
*
* Unit tests for StaticAction class
*/
require_once dirname(__FILE__) . '/Action_TestCase.php';
require_once dirname(__FILE__) . '/../../app/StaticAction.php';
class StaticActionTest extends Action_TestCase
{
/**
* @runInSeparateProcess
*/
public function testFetchNoTemplate()
{
Config::set('DEBUG', false);
Env::setParams(array('template' => ''));
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = $this->getMockForAbstractClass('StaticAction', array(), 'StaticActionMock');
$result = $action->fetch();
$this->assertSame('/static/StaticActi', $result['template']);
}
/**
* @runInSeparateProcess
*/
public function testFetchWithTemplate()
{
Config::set('DEBUG', false);
Env::setParams(array('template' => 'SomeTemplate'));
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = $this->getMockForAbstractClass('StaticAction', array(), 'StaticActionMock');
$result = $action->fetch();
$this->assertSame('/static/SomeTemplate', $result['template']);
}
}

View File

@ -0,0 +1,93 @@
<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-10-31
*
* Unit tests for Router class
*/
require_once dirname(__FILE__) . '/../../../app/router/Route.php';
class RouteTest extends PHPUnit_Framework_TestCase
{
public function testConstructNoParams()
{
$route = new Route('index', 'user');
$this->assertSame('userAction', $route->getAction());
$this->assertSame('Layout', $route->getLayout());
$this->assertEmpty($route->getParams());
$this->assertAttributeEquals('index', 'route', $route);
}
public function testConstructWithParams()
{
$route = new Route('index', 'user', array('name' => 'tony', 'role' => 'user'));
$this->assertArrayHasKey('role', $route->getParams());
$this->assertSame(array('name' => 'tony', 'role' => 'user'), $route->getParams());
}
public function testMatchDiffCount()
{
$route = new Route('user/account/id/142', 'user', array('name' => 'tony', 'role' => 'user'));
$this->assertFalse($route->match(array('user', 'account')));
}
/**
* @TODO: Route::match() - need to trim left-right slashes
*/
public function testMatchDiffParts()
{
$route = new Route('user/account', 'user');
$this->assertFalse($route->match(array('user', 'login')));
}
public function testMatchVariable()
{
$route = new Route('user/account/:id', 'user');
$this->assertTrue($route->match(array('user', 'account', '142')));
$this->assertSame(array('id' => '142'), $route->getParams());
}
public function testMatchDiffPregParts()
{
$route = new Route('user/account/(?<key>value)', 'user');
$this->assertFalse($route->match(array('user', 'account', 'wrong')));
$this->assertSame(0, count($route->getParams()));
}
public function testMatchPregPasses()
{
$route = new Route('user/account/(?<key>[a-z]+)', 'user');
$this->assertTrue($route->match(array('user', 'account', 'value')));
$this->assertSame(array('key' => 'value'), $route->getParams());
$this->assertSame(1, count($route->getParams()));
}
/**
* @TODO Need check empty parameters from constructor Route
*/
public function testMatchEmptyRequest()
{
$route = new Route('', 'user', array('name' => 'tony', 'role' => 'user'));
$this->setExpectedException('PHPUnit_Framework_Error');
$route->match('');
}
public function testGetUri()
{
$route = 'myroute';
$route_mock = $this->getMockBuilder('Route')
->disableOriginalConstructor()
->setMethods(array('__construct'))
->getMock();
$reflection = new ReflectionProperty('Route', 'route');
$reflection->setAccessible(true);
$reflection->setValue($route_mock, $route);
$this->assertEquals('/' . $route, $route_mock->getUri());
}
}

View File

@ -0,0 +1,122 @@
<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-10-31
*
* Unit tests for Router class
*/
require_once dirname(__FILE__) . '/../../../classes/Env.class.php';
require_once dirname(__FILE__) . '/../../../app/router/Route.php';
require_once dirname(__FILE__) . '/../../../app/router/Router.php';
class RouterTest extends PHPUnit_Framework_TestCase
{
public function testGetRouteFirst()
{
$router = new Router();
$this->assertNull($router->getRoute());
$this->assertNull($router->getRouteName());
}
public function testRouterCycle()
{
$router = new Router();
$router->add('user', 'user/account/:id', 'user');
$route = $router->route('user/account/213');
$this->assertSame(1, count($route->getParams()));
$this->assertSame(array('id' => '213'), $route->getParams());
$this->assertSame('user', $router->getRouteName());
}
public function testRouterMultipleRoutes()
{
$router = new Router();
$router->add('user', 'user/account/:id', 'user');
$router->add('sale', 'sale/order/:id', 'user');
$route = $router->route('user/account/213');
$this->assertSame('user', $router->getRouteName());
$this->assertSame(1, count($route->getParams()));
$this->assertSame(array('id' => '213'), $route->getParams());
$route = $router->route('sale/order/22');
$this->assertSame('sale', $router->getRouteName());
$this->assertSame(array('id' => '22'), $route->getParams());
}
public function testRouteNotmatch()
{
$router = new Router();
$router->add('user', 'user/account/:id', 'user');
$this->assertFalse($router->route('user/info/213'));
}
public function testSetDefaultLayout()
{
$router = new Router();
$router->setDefaultLayout('userLayout');
$this->assertAttributeEquals('userLayout', 'default_layout', $router);
}
public function testGetRouteWithNameIsNull()
{
$name = null;
$route = 'route object.';
$router = new Router();
$reflection = new ReflectionProperty('Router', 'route');
$reflection->setAccessible(true);
$reflection->setValue($router, $route);
$this->assertEquals($route, $router->getRoute($name));
}
public function testGetRouteWithNamed()
{
$name = 'nameofroute';
$uri = 'uri from route.';
$route = 'route object.';
$router = new Router();
$reflection = new ReflectionProperty('Router', 'routes');
$reflection->setAccessible(true);
$reflection->setValue($router, array($name => $route));
$this->assertEquals($route, $router->getRoute($name));
}
public function testGetRouteWithNamedWithError()
{
$name = 'name of route';
$router = new Router();
$this->setExpectedException('ErrorException');
$router->getRoute($name);
}
public function testRouteIsExists()
{
$route = 'route object.';
$name = 'nameofroute';
$name_is_not_exists = 'nameofroutenotexists';
$routes = array($name => $route);
$router = new Router();
$reflection = new ReflectionProperty('Router', 'routes');
$reflection->setAccessible(true);
$reflection->setValue($router, $routes);
$this->assertTrue($router->routeIsExists($name));
$this->assertFalse($router->routeIsExists($name_is_not_exists));
}
public function testGetDefaultErrorLayout()
{
$router = new Router();
$this->assertSame('ErrorLayout', $router->getErrorLayout());
}
public function testSetErrorLayout()
{
$router = new Router();
$router->setErrorLayout('CustomError');
$this->assertSame('CustomErrorLayout', $router->getErrorLayout());
}
}