Making MissingView and MissingLayout cake errors into exceptions.

This commit is contained in:
mark_story 2010-07-03 01:00:09 -04:00
parent a23207791a
commit 1d983e1cd4
2 changed files with 11 additions and 45 deletions

View file

@ -747,6 +747,7 @@ class View extends Object {
* *
* @param string $name Controller action to find template filename for * @param string $name Controller action to find template filename for
* @return string Template filename * @return string Template filename
* @throws MissingViewException when a view file could not be found.
*/ */
protected function _getViewFileName($name = null) { protected function _getViewFileName($name = null) {
$subDir = null; $subDir = null;
@ -798,7 +799,7 @@ class View extends Object {
} }
} }
} }
return $this->_missingView($defaultPath . $name . $this->ext, 'missingView'); throw new MissingViewException($defaultPath . $name . $this->ext);
} }
/** /**
@ -806,6 +807,7 @@ class View extends Object {
* *
* @param string $name The name of the layout to find. * @param string $name The name of the layout to find.
* @return string Filename for layout file (.ctp). * @return string Filename for layout file (.ctp).
* @throws MissingLayoutException when a layout cannot be located
*/ */
protected function _getLayoutFileName($name = null) { protected function _getLayoutFileName($name = null) {
if ($name === null) { if ($name === null) {
@ -830,32 +832,7 @@ class View extends Object {
} }
} }
} }
return $this->_missingView($paths[0] . $file . $this->ext, 'missingLayout'); throw new MissingLayoutException($paths[0] . $file . $this->ext);
}
/**
* Return a misssing view error message
*
* @param string $viewFileName the filename that should exist
* @return false
*/
protected function _missingView($file, $error = 'missingView') {
if ($error === 'missingView') {
$this->cakeError('missingView', array(
'className' => $this->name,
'action' => $this->action,
'file' => $file,
'base' => $this->base
));
return false;
} elseif ($error === 'missingLayout') {
$this->cakeError('missingLayout', array(
'layout' => $this->layout,
'file' => $file,
'base' => $this->base
));
return false;
}
} }
/** /**
@ -886,3 +863,6 @@ class View extends Object {
return $this->__paths; return $this->__paths;
} }
} }
class MissingViewException extends RuntimeException { }
class MissingLayoutException extends RuntimeException { }

View file

@ -375,6 +375,7 @@ class ViewTest extends CakeTestCase {
/** /**
* testMissingView method * testMissingView method
* *
* @expectedException MissingViewException
* @access public * @access public
* @return void * @return void
*/ */
@ -388,15 +389,12 @@ class ViewTest extends CakeTestCase {
$View = new TestView($this->Controller); $View = new TestView($this->Controller);
ob_start(); ob_start();
$result = $View->getViewFileName('does_not_exist'); $result = $View->getViewFileName('does_not_exist');
$expected = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
$this->assertPattern("/PagesController::/", $expected);
$this->assertPattern("/pages(\/|\\\)does_not_exist.ctp/", $expected);
} }
/** /**
* testMissingLayout method * testMissingLayout method
* *
* @expectedException MissingLayoutException
* @access public * @access public
* @return void * @return void
*/ */
@ -410,9 +408,6 @@ class ViewTest extends CakeTestCase {
ob_start(); ob_start();
$result = $View->getLayoutFileName(); $result = $View->getLayoutFileName();
$expected = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean()); $expected = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
$this->assertPattern("/Missing Layout/", $expected);
$this->assertPattern("/layouts(\/|\\\)whatever.ctp/", $expected);
} }
/** /**
@ -895,25 +890,16 @@ class ViewTest extends CakeTestCase {
/** /**
* testBadExt method * testBadExt method
* *
* @expectedException MissingViewException
* @access public * @access public
* @return void * @return void
*/ */
function testBadExt() { function testBadExt() {
$this->PostsController->action = 'something'; $this->PostsController->action = 'something';
$this->PostsController->ext = '.whatever'; $this->PostsController->ext = '.whatever';
ob_start();
$View = new TestView($this->PostsController); $View = new TestView($this->PostsController);
$View->render('this_is_missing'); $View->render('this_is_missing');
$result = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean()); $result = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
$this->assertPattern("/<em>PostsController::<\/em><em>something\(\)<\/em>/", $result);
$this->assertPattern("/posts(\/|\\\)this_is_missing.whatever/", $result);
$this->PostsController->ext = ".bad";
$View = new TestView($this->PostsController);
$result = str_replace(array("\t", "\r\n", "\n"), "", $View->render('index'));
$this->assertPattern("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/><title>/", $result);
$this->assertPattern("/<div id=\"content\">posts index<\/div>/", $result);
} }
} }