Skip to content
Permalink
Browse files

Making MissingView and MissingLayout cake errors into exceptions.

  • Loading branch information...
markstory committed Jul 3, 2010
1 parent a232077 commit 1d983e1cd4d51cc325f75ed83302d8fbebf309e5
Showing with 11 additions and 45 deletions.
  1. +7 −27 cake/libs/view/view.php
  2. +4 −18 cake/tests/cases/libs/view/view.test.php
@@ -747,6 +747,7 @@ public function loadHelper($helperName, $settings = array()) {
*
* @param string $name Controller action to find template filename for
* @return string Template filename
* @throws MissingViewException when a view file could not be found.
*/
protected function _getViewFileName($name = null) {
$subDir = null;
@@ -798,14 +799,15 @@ protected function _getViewFileName($name = null) {
}
}
}
return $this->_missingView($defaultPath . $name . $this->ext, 'missingView');
throw new MissingViewException($defaultPath . $name . $this->ext);
}
/**
* Returns layout filename for this template as a string.
*
* @param string $name The name of the layout to find.
* @return string Filename for layout file (.ctp).
* @throws MissingLayoutException when a layout cannot be located
*/
protected function _getLayoutFileName($name = null) {
if ($name === null) {
@@ -830,32 +832,7 @@ protected function _getLayoutFileName($name = null) {
}
}
}
return $this->_missingView($paths[0] . $file . $this->ext, 'missingLayout');
}
/**
* 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;
}
throw new MissingLayoutException($paths[0] . $file . $this->ext);
}
/**
@@ -886,3 +863,6 @@ protected function _paths($plugin = null, $cached = true) {
return $this->__paths;
}
}
class MissingViewException extends RuntimeException { }
class MissingLayoutException extends RuntimeException { }
@@ -375,6 +375,7 @@ function testGetTemplate() {
/**
* testMissingView method
*
* @expectedException MissingViewException
* @access public
* @return void
*/
@@ -388,15 +389,12 @@ function testMissingView() {
$View = new TestView($this->Controller);
ob_start();
$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
*
* @expectedException MissingLayoutException
* @access public
* @return void
*/
@@ -410,9 +408,6 @@ function testMissingLayout() {
ob_start();
$result = $View->getLayoutFileName();
$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 @@ function testEntityReference() {
/**
* testBadExt method
*
* @expectedException MissingViewException
* @access public
* @return void
*/
function testBadExt() {
$this->PostsController->action = 'something';
$this->PostsController->ext = '.whatever';
ob_start();
$View = new TestView($this->PostsController);
$View->render('this_is_missing');
$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);
}
}

0 comments on commit 1d983e1

Please sign in to comment.
You can’t perform that action at this time.