Browse Source

Action and FrontController classes tested

master
Anton Grebnev 13 years ago
parent
commit
1f8562da51
  1. 83
      tests/app/ActionTest.php
  2. 57
      tests/app/Action_TestCase.php
  3. 66
      tests/app/AjaxActionTest.php
  4. 143
      tests/app/ErrorActionTest.php
  5. 244
      tests/app/FrontControllerTest.php
  6. 101
      tests/app/PagerActionTest.php
  7. 50
      tests/app/StaticActionTest.php

83
tests/app/ActionTest.php

@ -0,0 +1,83 @@
<?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()
{
if(!defined('DEBUG')) {
define('DEBUG', false);
}
Env::setParams(array('param1' => 'value1', 'param2' => 'value2'));
$action = $this->getMockForAbstractClass('Action' );
$this->assertEquals('value1', $action->param1);
}
/**
* @runInSeparateProcess
*/
public function testFetch()
{
if(!defined('DEBUG')) {
define('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->assertEquals('/actions/to/SomeTemplate', $result['template']);
}
/**
* @runInSeparateProcess
*/
public function testFetchNoTemplate()
{
if(!defined('DEBUG')) {
define('DEBUG', false);
}
Env::setParams(array('template' => ''));
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = $this->getMockForAbstractClass('Action', array(), 'ActionMock');
$result = $action->fetch();
$this->assertEquals('/actions//Acti', $result['template']);
}
/**
* @runInSeparateProcess
*/
public function testRedirect()
{
set_exit_overload(function() { return false; });
if(!defined('DEBUG')) {
define('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();
}
}

57
tests/app/Action_TestCase.php

@ -0,0 +1,57 @@
<?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__) . '/../../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';
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
{
private $result = array();
public function fetch($template)
{
$this->result['template'] = $template;
return $this->result;
}
public function assignObject() {}
public function assign($name, $value) {
$this->result[$name] = $value;
}
}

66
tests/app/AjaxActionTest.php

@ -0,0 +1,66 @@
<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-11-1
*
* 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()
{
if(!defined('DEBUG')) {
define('DEBUG', false);
}
Env::setParams(array('ajax' => 'AjaxTemplate', 'param2' => 'value2'));
$action = $this->getMockForAbstractClass('AjaxAction' );
$this->assertAttributeEquals('ajax', 'template', $action);
}
/**
* @runInSeparateProcess
*/
public function testFetchWithEncode()
{
if(!defined('DEBUG')) {
define('DEBUG', false);
}
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = $this->getMockForAbstractClass('AjaxAction' );
$action->data = array('var' => 'val');
$result = $action->fetch();
$this->assertEquals('/actions//ajax', $result['template']);
$this->assertEquals('{"var":"val"}', $result['data']);
}
/**
* @runInSeparateProcess
*/
public function testFetchNoEncode()
{
if(!defined('DEBUG')) {
define('DEBUG', false);
}
Env::setParams(array('encode' => false));
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = $this->getMockForAbstractClass('AjaxAction' );
$action->data = array('var' => 'val');
$result = $action->fetch();
$this->assertEquals('/actions//ajax', $result['template']);
$this->assertEquals( $action->data, $result['data']);
}
}

143
tests/app/ErrorActionTest.php

@ -0,0 +1,143 @@
<?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';
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->assertEquals($exception, $action->exception);
}
/**
* @runInSeparateProcess
*/
public function testErrorExceptionWarning()
{
$this->setConstants(false);
$exception = $this->getMock('ErrorException', array(), array('', 0, E_WARNING));
$action = new ErrorAction($exception);
$this->assertEquals($exception, $action->exception);
}
/**
* @runInSeparateProcess
*/
public function testErrorExceptionError()
{
$this->setConstants(false);
$exception = $this->getMock('ErrorException', array(), array('', 0, E_ERROR));
$action = new ErrorAction($exception);
$this->assertEquals($exception, $action->exception);
}
/**
* @runInSeparateProcess
*/
public function testErrorExceptionCustom()
{
$this->setConstants(false);
$exception = $this->getMock('ErrorException', array(), array('', 0, 211));
$action = new ErrorAction($exception);
$this->assertEquals($exception, $action->exception);
}
/**
* @runInSeparateProcess
*/
public function testFetchNoTemplate()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$exception = $this->getMock('ErrorException');
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = new ErrorAction($exception);
$result = $action->fetch();
$this->assertEquals('/actions/500', $result['template']);
}
/**
* @runInSeparateProcess
*/
public function testError404WithAjax()
{
$this->setConstants(false);
$exception = $this->getMock('Error404Exception', array('getMessage', 'getFile', 'getLine', 'getTrace'));
$exception->expects($this->once())
->method('getTrace')
->will($this->returnValue(array('one' => array('class' => 'AjaxAction'))));
$action = new ErrorAction($exception);
$this->assertEquals($exception, $action->exception);
}
/**
* @runInSeparateProcess
*/
public function testError404NoAjax()
{
$this->setConstants(false);
$exception = $this->getMock('Error404Exception', array('getMessage', 'getFile', 'getLine', 'getTrace'));
$exception->expects($this->once())
->method('getTrace')
->will($this->returnValue(array('one' => array('class' => 'Action'))));
$action = new ErrorAction($exception);
$this->assertEquals($exception, $action->exception);
}
private function setConstants($val = false)
{
if (!defined('DEBUG')) {
define('DEBUG', $val);
}
}
private function header()
{
ob_end_clean();
flush();
}
public function tearDown()
{
parent::tearDown();
ini_set('error_log', $this->log);
unset_exit_overload();
}
}

244
tests/app/FrontControllerTest.php

@ -0,0 +1,244 @@
<?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/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
{
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'), array(), 'ErrorLayoutMock');
}
if (!class_exists('ErrorActionMock')) {
$this->getMock('ErrorAction', array(), 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->assertEquals($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->assertEquals('', $controller->getBaseUrl());
$controller->setBaseUrl('/index/');
$this->assertEquals('/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);
$controller = FrontController::getInstance();
$result = $controller->execute();
$this->assertNotEmpty($result);
$this->assertContains('Route for "" not found', $result);
$this->assertContains('Error404Exception', $result);
}
/**
* @runInSeparateProcess
*/
public function testExecuteNoAction()
{
$_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->assertContains('Action class "userAction" not found.', $result);
}
/**
* @runInSeparateProcess
*/
public function testExecuteNoLayout()
{
$this->getMock('userAction');
$_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->assertContains('Layout class "DefaultLayout" not found.', $result);
}
/**
* @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->assertEmpty($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->assertEmpty($result);
}
private function setConstants($val = false)
{
if (!defined('DEBUG')) {
define('DEBUG', $val);
}
}
public function tearDown()
{
unset_new_overload();
}
protected function newCallback($className)
{
switch ($className) {
case 'PHPView':
return 'PHPViewMock';
case 'DefaultLayout':
return 'DefaultLayoutMock';
case 'userAction':
return 'userAction';
case 'ErrorAction':
return 'ErrorActionMock';
case 'ErrorLayout':
return 'ErrorLayoutMock';
default:
return $className;
}
}
}
class NewAjaxAction extends AjaxAction{
protected function execute() {}
}

101
tests/app/PagerActionTest.php

@ -0,0 +1,101 @@
<?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()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$action = $this->getMockForAbstractClass('PagerAction');
$this->assertEquals(20, $action->getLimit());
$action = $this->getMockForAbstractClass('PagerAction', array(50));
$this->assertEquals(50, $action->getLimit());
}
/**
* @runInSeparateProcess
*/
public function testSetCount()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$action = $this->getMockForAbstractClass('PagerAction');
$action->setCount(50);
$this->assertEquals(1, $action->page);
$this->assertEquals(0, $action->getOffset());
$_GET['p'] = 'last';
$action->setCount(50);
$this->assertEquals(3, $action->page);
$this->assertEquals(40, $action->getOffset());
$_GET['p'] = 2;
$action->setCount(50);
$this->assertEquals(2, $action->page);
$this->assertEquals(20, $action->getOffset());
$_GET['p'] = -3;
$action->setCount(50);
$this->assertEquals(1, $action->page);
}
/**
* @runInSeparateProcess
*/
public function testGetOffset()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$action = $this->getMockForAbstractClass('PagerAction');
$this->assertEquals(0, $action->getOffset());
}
/**
* @runInSeparateProcess
*/
public function testFetchNoTemplate()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
Env::setParams(array('template' => ''));
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = $this->getMockForAbstractClass('PagerAction', array(), 'PagerActionMock');
$result = $action->fetch();
$this->assertEquals('/actions/PagerActi', $result['template']);
}
/**
* @runInSeparateProcess
*/
public function testFetchWithTemplate()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
Env::setParams(array('template' => 'SomeTemplate'));
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = $this->getMockForAbstractClass('PagerAction');
$result = $action->fetch();
$this->assertEquals('/actions/SomeTemplate', $result['template']);
}
}

50
tests/app/StaticActionTest.php

@ -0,0 +1,50 @@
<?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()
{
if(!defined('DEBUG')) {
define('DEBUG', false);
}
Env::setParams(array('template' => ''));
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = $this->getMockForAbstractClass('StaticAction', array(), 'StaticActionMock');
$result = $action->fetch();
$this->assertEquals('/static/StaticActi', $result['template']);
}
/**
* @runInSeparateProcess
*/
public function testFetchWithTemplate()
{
if(!defined('DEBUG')) {
define('DEBUG', false);
}
Env::setParams(array('template' => 'SomeTemplate'));
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = $this->getMockForAbstractClass('StaticAction', array(), 'StaticActionMock');
$result = $action->fetch();
$this->assertEquals('/static/SomeTemplate', $result['template']);
}
}
Loading…
Cancel
Save