Browse Source

Raise exceptions on missing elements.

We should be avoiding trigger_error() in Cake3, and instead use
exceptions.
mark_story 11 years ago
parent
commit
4e49b63140
3 changed files with 35 additions and 15 deletions
  1. 30 0
      src/View/Error/MissingElementException.php
  2. 3 2
      src/View/View.php
  3. 2 13
      tests/TestCase/View/ViewTest.php

+ 30 - 0
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.';
+
+}

+ 3 - 2
src/View/View.php

@@ -375,8 +375,9 @@ class View {
  *   - `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;
@@ -401,7 +402,7 @@ class View {
 			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);
 		}
 	}
 

+ 2 - 13
tests/TestCase/View/ViewTest.php

@@ -99,17 +99,6 @@ class ThemePostsController extends Controller {
 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
  *
  * @param string $name Controller action to find template filename for
@@ -653,7 +642,7 @@ class ViewTest extends TestCase {
 /**
  * Test elementInexistent method
  *
- * @expectedException PHPUnit_Framework_Error_Notice
+ * @expectedException Cake\View\Error\MissingElementException
  * @return void
  */
 	public function testElementInexistent() {
@@ -663,7 +652,7 @@ class ViewTest extends TestCase {
 /**
  * Test elementInexistent3 method
  *
- * @expectedException PHPUnit_Framework_Error_Notice
+ * @expectedException Cake\View\Error\MissingElementException
  * @return void
  */
 	public function testElementInexistent3() {