updating error handling, error.test, view.test, theme.test, change socket.test to use App::import()

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6479 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2008-02-27 21:22:40 +00:00
parent 3316731dd9
commit d3e2d51cbc
6 changed files with 101 additions and 146 deletions

View file

@ -102,7 +102,7 @@ class Component extends Object {
'file' => Inflector::underscore($component) . '.php', 'file' => Inflector::underscore($component) . '.php',
'base' => $this->controller->base 'base' => $this->controller->base
))); )));
exit(); return false;
} }
} }
@ -113,7 +113,7 @@ class Component extends Object {
'file' => Inflector::underscore($component) . '.php', 'file' => Inflector::underscore($component) . '.php',
'base' => $this->controller->base 'base' => $this->controller->base
))); )));
exit(); return false;
} }
} }
$base = null; $base = null;

View file

@ -29,7 +29,7 @@
/** /**
* Included libraries. * Included libraries.
*/ */
App::import('Core', array('view' . DS . 'helper', 'ClassRegistry')); App::import('Core', array('Helper', 'ClassRegistry'));
/** /**
* View, the V in the MVC triad. * View, the V in the MVC triad.
@ -330,8 +330,7 @@ class View extends Object {
$action = $file; $action = $file;
} }
if ($action !== false) { if ($action !== false && $viewFileName = $this->_getViewFileName($action)) {
$viewFileName = $this->_getViewFileName($action);
if (substr($viewFileName, -3) === 'ctp' || substr($viewFileName, -5) === 'thtml') { if (substr($viewFileName, -3) === 'ctp' || substr($viewFileName, -5) === 'thtml') {
$out = View::_render($viewFileName, $this->viewVars); $out = View::_render($viewFileName, $this->viewVars);
} else { } else {
@ -722,7 +721,7 @@ class View extends Object {
'file' => Inflector::underscore($helper) . '.php', 'file' => Inflector::underscore($helper) . '.php',
'base' => $this->base 'base' => $this->base
))); )));
exit(); return false;
} }
} }
if (!class_exists($helperCn)) { if (!class_exists($helperCn)) {
@ -731,7 +730,7 @@ class View extends Object {
'file' => Inflector::underscore($helper) . '.php', 'file' => Inflector::underscore($helper) . '.php',
'base' => $this->base 'base' => $this->base
))); )));
exit(); return false;
} }
} }
$loaded[$helper] =& new $helperCn($options); $loaded[$helper] =& new $helperCn($options);
@ -766,7 +765,6 @@ class View extends Object {
*/ */
function _getViewFileName($name = null) { function _getViewFileName($name = null) {
$subDir = null; $subDir = null;
if (!is_null($this->subDir)) { if (!is_null($this->subDir)) {
$subDir = $this->subDir . DS; $subDir = $this->subDir . DS;
} }
@ -801,6 +799,8 @@ class View extends Object {
foreach ($paths as $path) { foreach ($paths as $path) {
if (file_exists($path . $name . $this->ext)) { if (file_exists($path . $name . $this->ext)) {
return $path . $name . $this->ext; return $path . $name . $this->ext;
} elseif (file_exists($path . $name . '.ctp')) {
return $path . $name . '.ctp';
} elseif (file_exists($path . $name . '.thtml')) { } elseif (file_exists($path . $name . '.thtml')) {
return $path . $name . '.thtml'; return $path . $name . '.thtml';
} }
@ -831,6 +831,8 @@ class View extends Object {
foreach ($paths as $path) { foreach ($paths as $path) {
if (file_exists($path . $file . $this->ext)) { if (file_exists($path . $file . $this->ext)) {
return $path . $file . $this->ext; return $path . $file . $this->ext;
} elseif (file_exists($path . $name . '.ctp')) {
return $path . $file . '.ctp';
} elseif (file_exists($path . $file . '.thtml')) { } elseif (file_exists($path . $file . '.thtml')) {
return $path . $file . '.thtml'; return $path . $file . '.thtml';
} }
@ -845,43 +847,24 @@ class View extends Object {
* @return cakeError * @return cakeError
*/ */
function _missingView($file, $error = 'missingView') { function _missingView($file, $error = 'missingView') {
if (Configure::read() == 0) {
$this->cakeError('error404');
exit();
}
$paths = $this->_paths($this->plugin);
$name = 'errors' . DS . Inflector::underscore($error);
foreach ($paths as $path) {
if (file_exists($path . $name . $this->ext)) {
$name = $path . $name . $this->ext;
break;
} elseif (file_exists($path . $name . '.ctp')) {
$name = $path . $name . '.ctp';
$this->ext = '.ctp';
break;
} elseif (file_exists($path . $name . '.thtml')) {
$name = $path . $name . '.thtml';
break;
}
}
if ($error === 'missingView') { if ($error === 'missingView') {
$this->set(array( $this->cakeError('missingView', array(
'controller' => $this->name, 'className' => $this->name,
'action' => $this->action, 'action' => $this->action,
'file' => $file, 'file' => $file,
'base' => $this->base 'base' => $this->base
)); ));
return false;
} elseif ($error === 'missingLayout') { } elseif ($error === 'missingLayout') {
$this->set(array( $this->cakeError('missingLayout', array(
'layout' => $this->layout, 'layout' => $this->layout,
'file' => $file, 'file' => $file,
'base' => $this->base 'base' => $this->base
)); ));
return false;
} }
return $name;
} }
/** /**
* Return all possible paths to find view files in order * Return all possible paths to find view files in order

View file

@ -26,10 +26,12 @@
* @lastmodified $Date$ * @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/ */
App::import('Core', array('Error', 'Controller'));
if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) { if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
define('CAKEPHP_UNIT_TEST_EXECUTION', 1); define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
} }
uses('error', 'controller' . DS .'controller');
if (!class_exists('TestAppController')) { if (!class_exists('TestAppController')) {
class TestAppController extends Controller { class TestAppController extends Controller {
function beforeFilter() { function beforeFilter() {
@ -105,10 +107,33 @@ class ErrorHandlerTest extends UnitTestCase {
} }
function testMissingView() { function testMissingView() {
restore_error_handler();
ob_start();
$ErrorHandler = new ErrorHandler('missingView', array(
'className' => 'Pages',
'action' => 'display',
'file' => 'pages/about.ctp',
'base' => ''
));
$expected = ob_get_clean();
set_error_handler('simpleTestErrorHandler');
$this->assertPattern("/PagesController::/", $expected);
$this->assertPattern("/pages\/about.ctp/", $expected);
} }
function testMissingLayout() { function testMissingLayout() {
restore_error_handler();
ob_start();
$ErrorHandler = new ErrorHandler('missingLayout', array(
'layout' => 'my_layout',
'file' => 'layouts/my_layout.ctp',
'base' => ''
));
$expected = ob_get_clean();
set_error_handler('simpleTestErrorHandler');
$this->assertPattern("/Missing Layout/", $expected);
$this->assertPattern("/layouts\/my_layout.ctp/", $expected);
} }

View file

@ -26,7 +26,7 @@
* @lastmodified $Date$ * @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/ */
uses('socket'); App::import('Core', 'Socket');
/** /**
* Short description for class. * Short description for class.
* *

View file

@ -26,7 +26,11 @@
* @lastmodified $Date$ * @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/ */
uses('controller' . DS . 'controller', 'view'.DS.'theme'); App::import('Core', array('Theme', 'Controller'));
if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
}
class ThemePostsController extends Controller { class ThemePostsController extends Controller {
var $name = 'ThemePosts'; var $name = 'ThemePosts';
@ -50,10 +54,6 @@ class TestThemeView extends ThemeView {
function getLayoutFileName($name = null) { function getLayoutFileName($name = null) {
return $this->_getLayoutFileName($name); return $this->_getLayoutFileName($name);
} }
function cakeError($name, $params) {
return $name;
}
} }
/** /**
@ -134,14 +134,18 @@ class ThemeViewTest extends UnitTestCase {
$this->Controller->name = 'Pages'; $this->Controller->name = 'Pages';
$this->Controller->viewPath = 'pages'; $this->Controller->viewPath = 'pages';
$this->Controller->action = 'display'; $this->Controller->action = 'display';
$this->Controller->theme = 'my_theme';
$this->Controller->params['pass'] = array('home'); $this->Controller->params['pass'] = array('home');
$ThemeView = new TestThemeView($this->Controller); restore_error_handler();
$View = new TestThemeView($this->Controller);
$expected = TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS . 'errors' . DS . 'missing_view.ctp'; ob_start();
$result = $ThemeView->getViewFileName('does_not_exist'); $result = $View->getViewFileName('does_not_exist');
$this->assertEqual($result, $expected); $expected = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
set_error_handler('simpleTestErrorHandler');
$this->assertPattern("/PagesController::/", $expected);
$this->assertPattern("/views\/themed\/my_theme\/pages\/does_not_exist.ctp/", $expected);
} }
function testMissingLayout() { function testMissingLayout() {
@ -149,11 +153,16 @@ class ThemeViewTest extends UnitTestCase {
$this->Controller->name = 'Posts'; $this->Controller->name = 'Posts';
$this->Controller->viewPath = 'posts'; $this->Controller->viewPath = 'posts';
$this->Controller->layout = 'whatever'; $this->Controller->layout = 'whatever';
$this->Controller->theme = 'my_theme';
$ThemeView = new TestThemeView($this->Controller); restore_error_handler();
$expected = TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS . 'errors' . DS . 'missing_layout.ctp'; $View = new TestThemeView($this->Controller);
$result = $ThemeView->getLayoutFileName(); ob_start();
$this->assertEqual($result, $expected); $result = $View->getLayoutFileName();
$expected = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
set_error_handler('simpleTestErrorHandler');
$this->assertPattern("/Missing Layout/", $expected);
$this->assertPattern("/views\/themed\/my_theme\/layouts\/whatever.ctp/", $expected);
} }
function tearDown() { function tearDown() {

View file

@ -26,7 +26,11 @@
* @lastmodified $Date$ * @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/ */
uses('controller' . DS . 'controller', 'view'.DS.'view'); App::import('Core', array('View', 'Controller'));
if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
}
class ViewPostsController extends Controller { class ViewPostsController extends Controller {
var $name = 'Posts'; var $name = 'Posts';
@ -55,7 +59,6 @@ class TestView extends View {
return $this->_loadHelpers($loaded, $helpers, $parent); return $this->_loadHelpers($loaded, $helpers, $parent);
} }
} }
/** /**
* Short description for class. * Short description for class.
* *
@ -133,11 +136,14 @@ class ViewTest extends UnitTestCase {
$this->Controller->action = 'display'; $this->Controller->action = 'display';
$this->Controller->params['pass'] = array('home'); $this->Controller->params['pass'] = array('home');
restore_error_handler();
$View = new TestView($this->Controller); $View = new TestView($this->Controller);
ob_start();
$expected = TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS . 'errors' . DS . 'missing_view.ctp';
$result = $View->getViewFileName('does_not_exist'); $result = $View->getViewFileName('does_not_exist');
$this->assertEqual($result, $expected); $expected = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
set_error_handler('simpleTestErrorHandler');
$this->assertPattern("/PagesController::/", $expected);
$this->assertPattern("/pages\/does_not_exist.ctp/", $expected);
} }
function testMissingLayout() { function testMissingLayout() {
@ -146,10 +152,15 @@ class ViewTest extends UnitTestCase {
$this->Controller->viewPath = 'posts'; $this->Controller->viewPath = 'posts';
$this->Controller->layout = 'whatever'; $this->Controller->layout = 'whatever';
restore_error_handler();
$View = new TestView($this->Controller); $View = new TestView($this->Controller);
$expected = TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS . 'errors' . DS . 'missing_layout.ctp'; ob_start();
$result = $View->getLayoutFileName(); $result = $View->getLayoutFileName();
$this->assertEqual($result, $expected); $expected = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
set_error_handler('simpleTestErrorHandler');
$this->assertPattern("/Missing Layout/", $expected);
$this->assertPattern("/layouts\/whatever.ctp/", $expected);
} }
function testViewVars() { function testViewVars() {
@ -259,117 +270,44 @@ class ViewTest extends UnitTestCase {
} }
function testRender() { function testRender() {
restore_error_handler();
$View = new TestView($this->PostsController); $View = new TestView($this->PostsController);
ob_start(); ob_start();
$View->render('index'); $View->render('index');
$result = ob_get_clean(); $result = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
$expected = ' set_error_handler('simpleTestErrorHandler');
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> $this->assertPattern("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/><title>/", $result);
<html xmlns="http://www.w3.org/1999/xhtml"> $this->assertPattern("/<div id=\"content\">posts index<\/div>/", $result);
<head> $this->assertPattern("/<div id=\"content\">posts index<\/div>/", $result);
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
CakePHP: the rapid development php framework: Posts </title>
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<link rel="stylesheet" type="text/css" href="css/cake.generic.css" /> </head>
<body>
<div id="container">
<div id="header">
<h1><a href="http://cakephp.org">CakePHP: the rapid development php framework</a></h1>
</div>
<div id="content">
posts index
</div>
<div id="footer">
<a href="http://www.cakephp.org/" target="_new"><img src="img/cake.power.gif" alt="CakePHP: the rapid development php framework" border="0" /></a> </div>
</div>
</body>
</html>
';
$result = str_replace(array("\t", "\r\n", "\n"), "", $result);
$expected = str_replace(array("\t", "\r\n", "\n"), "", $expected);
$this->assertEqual($result, $expected);
$this->PostsController->set('url', 'flash'); $this->PostsController->set('url', 'flash');
$this->PostsController->set('message', 'yo what up'); $this->PostsController->set('message', 'yo what up');
$this->PostsController->set('pause', 3); $this->PostsController->set('pause', 3);
$this->PostsController->set('page_title', 'yo what up'); $this->PostsController->set('page_title', 'yo what up');
restore_error_handler();
$View = new TestView($this->PostsController); $View = new TestView($this->PostsController);
ob_start(); ob_start();
$View->render(false, 'flash'); $View->render(false, 'flash');
$result = ob_get_clean(); $result = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
set_error_handler('simpleTestErrorHandler');
$expected = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> $this->assertPattern("/<title>yo what up<\/title>/", $result);
<html xmlns="http://www.w3.org/1999/xhtml"> $this->assertPattern("/<p><a href=\"flash\">yo what up<\/a><\/p>/", $result);
<head>
<title>yo what up</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style><!--
P { text-align:center; font:bold 1.1em sans-serif }
A { color:#444; text-decoration:none }
A:HOVER { text-decoration: underline; color:#44E }
--></style>
</head>
<body>
<p><a href="flash">yo what up</a></p>
</body>
</html>';
$result = str_replace(array("\t", "\r\n", "\n"), "", $result);
$expected = str_replace(array("\t", "\r\n", "\n"), "", $expected);
$this->assertEqual($result, $expected);
} }
function testBadExt() { function testBadExt() {
$this->PostsController->action = 'something'; $this->PostsController->action = 'something';
$this->PostsController->ext = '.whatever'; $this->PostsController->ext = '.whatever';
$View = new TestView($this->PostsController); restore_error_handler();
ob_start(); ob_start();
$View = new TestView($this->PostsController);
$View->render('this_is_missing'); $View->render('this_is_missing');
$result = ob_get_clean(); $result = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
set_error_handler('simpleTestErrorHandler');
$expected = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> $this->assertPattern("/<em>PostsController::<\/em><em>something\(\)<\/em>/", $result);
<html xmlns="http://www.w3.org/1999/xhtml"> $this->assertPattern("/posts\/this_is_missing.whatever/", $result);
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
CakePHP: the rapid development php framework: Posts </title>
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<link rel="stylesheet" type="text/css" href="css/cake.generic.css" /> </head>
<body>
<div id="container">
<div id="header">
<h1><a href="http://cakephp.org">CakePHP: the rapid development php framework</a></h1>
</div>
<div id="content">
<h2>Missing View</h2>
<p class="error">
<strong>Error: </strong>
The view for <em>PostsController::</em><em>something()</em> was not found.</p>
<p class="error">
<strong>Error: </strong>
Confirm you have created the file: '.TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS .'posts'. DS . 'this_is_missing.whatever</p>
<p class="notice">
<strong>Notice: </strong>
If you want to customize this error message, create '.APP_DIR.'/views/errors/missing_view.ctp</p>
</div>
<div id="footer">
<a href="http://www.cakephp.org/" target="_new"><img src="img/cake.power.gif" alt="CakePHP: the rapid development php framework" border="0" /></a> </div>
</div>
</body>
</html>';
$result = str_replace(array("\t", "\r\n", "\n"), "", $result);
$expected = str_replace(array("\t", "\r\n", "\n"), "", $expected);
$this->assertEqual($result, $expected);
} }
function tearDown() { function tearDown() {