Browse Source

Merge branch 'new_error_layout'

master
Anton Grebnev 13 years ago
parent
commit
0f1acaa7d6
  1. 12
      app/FrontController.php
  2. 20
      app/router/Router.php
  3. 17
      layout/Error.layout.php
  4. 8
      tests/app/FrontControllerTest.php
  5. 13
      tests/app/router/RouterTest.php
  6. 82
      tests/layout/ErrorLayoutTest.php

12
app/FrontController.php

@ -41,7 +41,9 @@ class FrontController
/** /**
* Refuse cloning * Refuse cloning
*/ */
private function __clone(){}
private function __clone()
{
}
/** /**
* @return FrontController * @return FrontController
@ -133,7 +135,13 @@ class FrontController
} }
return ErrorHandler::showDebug($e); return ErrorHandler::showDebug($e);
} }
$layout = new ErrorLayout();
$layout_class = $this->getRouter()->getErrorLayout();
/**
* @var ErrorLayout $layout
*/
$layout = new $layout_class();
$layout->setException($e);
return $layout->fetch(new ErrorAction($e)); return $layout->fetch(new ErrorAction($e));
} }
} }

20
app/router/Router.php

@ -18,6 +18,8 @@ class Router
protected $default_layout = 'Default'; protected $default_layout = 'Default';
protected $error_layout = 'Error';
/** /**
* @var Route * @var Route
*/ */
@ -51,6 +53,24 @@ class Router
$this->default_layout = $layout; $this->default_layout = $layout;
} }
/**
* Sets the name of error page layout
* @param string $layout
*/
public function setErrorLayout($layout = 'Error')
{
$this->error_layout = $layout;
}
/**
* Returns error layout name
* @return string Error layout name
*/
public function getErrorLayout()
{
return $this->error_layout . 'Layout';
}
public function getRouteName() public function getRouteName()
{ {
return $this->route_name; return $this->route_name;

17
layout/Error.layout.php

@ -11,5 +11,20 @@
class ErrorLayout extends Layout class ErrorLayout extends Layout
{ {
protected function execute(){}
/**
* @var GeneralException
*/
protected $exception;
protected function execute()
{
}
/**
* @param GeneralException $exception
*/
public function setException(GeneralException $exception)
{
$this->exception = $exception;
}
} }

8
tests/app/FrontControllerTest.php

@ -44,7 +44,7 @@ class FrontControllerTest extends PHPUnit_Framework_TestCase
$this->getMock('View'); $this->getMock('View');
} }
if (!class_exists('ErrorLayout')) { if (!class_exists('ErrorLayout')) {
$this->getMock('ErrorLayout', array('fetch'), array(), 'ErrorLayoutMock');
$this->getMock('ErrorLayout', array('fetch', 'setException'), array(), 'ErrorLayoutMock');
} }
if (!class_exists('ErrorActionMock')) { if (!class_exists('ErrorActionMock')) {
$this->getMock('ErrorAction', array(), array(), 'ErrorActionMock', false); $this->getMock('ErrorAction', array(), array(), 'ErrorActionMock', false);
@ -190,7 +190,7 @@ class FrontControllerTest extends PHPUnit_Framework_TestCase
$router = $controller->getRouter(); $router = $controller->getRouter();
$router->add('user', 'user/account/:id', 'user'); $router->add('user', 'user/account/:id', 'user');
$result = $controller->execute(); $result = $controller->execute();
$this->assertEmpty($result);
$this->assertNull($result);
} }
/** /**
* @runInSeparateProcess * @runInSeparateProcess
@ -205,7 +205,7 @@ class FrontControllerTest extends PHPUnit_Framework_TestCase
$router = $controller->getRouter(); $router = $controller->getRouter();
$router->add('user', 'user/account/:id', 'NewAjax'); $router->add('user', 'user/account/:id', 'NewAjax');
$result = $controller->execute(); $result = $controller->execute();
$this->assertEmpty($result);
$this->assertNull($result);
} }
private function setConstants($val = false) private function setConstants($val = false)
@ -227,8 +227,6 @@ class FrontControllerTest extends PHPUnit_Framework_TestCase
return 'PHPViewMock'; return 'PHPViewMock';
case 'DefaultLayout': case 'DefaultLayout':
return 'DefaultLayoutMock'; return 'DefaultLayoutMock';
case 'userAction':
return 'userAction';
case 'ErrorAction': case 'ErrorAction':
return 'ErrorActionMock'; return 'ErrorActionMock';
case 'ErrorLayout': case 'ErrorLayout':

13
tests/app/router/RouterTest.php

@ -106,4 +106,17 @@ class RouterTest extends PHPUnit_Framework_TestCase
$this->assertTrue($router->routeIsExists($name)); $this->assertTrue($router->routeIsExists($name));
$this->assertFalse($router->routeIsExists($name_is_not_exists)); $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());
}
} }

82
tests/layout/ErrorLayoutTest.php

@ -0,0 +1,82 @@
<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 04-06-2012
* @user: agrebnev
*/
require_once dirname(__FILE__) . '/../../exception/ErrorHandler.php';
require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
require_once dirname(__FILE__) . '/../../app/FrontController.php';
require_once dirname(__FILE__) . '/../../layout/Layout.php';
require_once dirname(__FILE__) . '/../../layout/Error.layout.php';
/**
* @runTestsInSeparateProcesses
*/
class ErrorLayoutTest 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('RouterMock')) {
$this->getMock('Router', array(), array(), 'RouterMock', false);
}
if (!class_exists('PHPViewMock')) {
$this->getMock('PHPView', array('fetch', 'append', 'prepend', 'assign', 'getTemplate'), array(), 'PHPViewMock', false);
}
set_new_overload(array($this, 'newCallback'));
}
public function testSetException()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$layout = new ErrorLayout();
$layout->setException(new GeneralException());
$this->assertAttributeInstanceOf('GeneralException', 'exception', $layout);
}
public function testExecute()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$action = $this->getMock('Action', array('fetch'));
$action->expects($this->once())
->method('fetch')
->will($this->returnValue('this is the result of Action::fetch()'));
$layout = new ErrorLayout();
$layout->fetch($action);
}
protected function newCallback($className)
{
switch ($className) {
case 'Router':
return 'RouterMock';
case 'PHPView':
return 'PHPViewMock';
default:
return $className;
}
}
public function tearDown()
{
unset_new_overload();
}
}
Loading…
Cancel
Save