Skip to content

Commit

Permalink
Raise exceptions on missing elements.
Browse files Browse the repository at this point in the history
We should be avoiding trigger_error() in Cake3, and instead use
exceptions.
  • Loading branch information
markstory committed Jun 8, 2014
1 parent a343146 commit 4e49b63
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
30 changes: 30 additions & 0 deletions src/View/Error/MissingElementException.php
@@ -0,0 +1,30 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\View\Error;

use Cake\Error\Exception;

/**
* Used when an element file cannot be found.
*/
class MissingElementException extends Exception {

/**
* Message template
*
* @var string
*/
protected $_messageTemplate = 'Element file "%s" is missing.';

}
5 changes: 3 additions & 2 deletions src/View/View.php
Expand Up @@ -375,8 +375,9 @@ public function __construct(Request $request = null, Response $response = null,
* - `key` - Used to define the key used in the Cache::write(). It will be prefixed with `element_`
* - `callbacks` - Set to true to fire beforeRender and afterRender helper callbacks for this element.
* Defaults to false.
* - `ignoreMissing` - Used to allow missing elements. Set to true to not trigger notices.
* - `ignoreMissing` - Used to allow missing elements. Set to true to not throw exceptions.
* @return string Rendered Element
* @throws
*/
public function element($name, array $data = array(), array $options = array()) {
$file = $plugin = null;
Expand All @@ -401,7 +402,7 @@ public function element($name, array $data = array(), array $options = array())
list ($plugin, $name) = pluginSplit($name, true);
$name = str_replace('/', DS, $name);
$file = $plugin . 'Element' . DS . $name . $this->_ext;
trigger_error(sprintf('Element Not Found: %s', $file), E_USER_NOTICE);
throw new Error\MissingElementException($file);
}
}

Expand Down
15 changes: 2 additions & 13 deletions tests/TestCase/View/ViewTest.php
Expand Up @@ -98,17 +98,6 @@ public function index() {
*/
class TestThemeView extends View {

/**
* renderElement method
*
* @param string $name Element name.
* @param array $params Params list.
* @return string The given name
*/
public function renderElement($name, $params = array()) {
return $name;
}

/**
* getViewFileName method
*
Expand Down Expand Up @@ -653,7 +642,7 @@ public function testElement() {
/**
* Test elementInexistent method
*
* @expectedException PHPUnit_Framework_Error_Notice
* @expectedException Cake\View\Error\MissingElementException
* @return void
*/
public function testElementInexistent() {
Expand All @@ -663,7 +652,7 @@ public function testElementInexistent() {
/**
* Test elementInexistent3 method
*
* @expectedException PHPUnit_Framework_Error_Notice
* @expectedException Cake\View\Error\MissingElementException
* @return void
*/
public function testElementInexistent3() {
Expand Down

0 comments on commit 4e49b63

Please sign in to comment.