Browse Source

Raise a cell specific error when views are missing.

The standard MissingView error page references controllers. By using
a more specific error message we can avoid referencing the controller
and only mention that the cell view is missing.
mark_story 11 years ago
parent
commit
ad401ec86e
3 changed files with 45 additions and 2 deletions
  1. 8 1
      src/View/Cell.php
  2. 25 0
      src/View/Error/MissingCellViewException.php
  3. 12 1
      tests/TestCase/View/CellTest.php

+ 8 - 1
src/View/Cell.php

@@ -20,6 +20,8 @@ use Cake\Model\ModelAwareTrait;
 use Cake\Network\Request;
 use Cake\Network\Response;
 use Cake\Utility\Inflector;
+use Cake\View\Error\MissingCellViewException;
+use Cake\View\Error\MissingViewException;
 use Cake\View\ViewVarsTrait;
 
 /**
@@ -132,6 +134,7 @@ abstract class Cell {
  * @param string $template Custom template name to render. If not provided (null), the last
  * value will be used. This value is automatically set by `CellTrait::cell()`.
  * @return void
+ * @throws \Cake\Error\MissingCellViewException When a MissingViewException is raised during rendering.
  */
 	public function render($template = null) {
 		if ($template !== null && strpos($template, '/') === false) {
@@ -148,7 +151,11 @@ abstract class Cell {
 		$className = array_pop($className);
 		$this->View->subDir = 'Cell' . DS . substr($className, 0, strpos($className, 'Cell'));
 
-		return $this->View->render($template);
+		try {
+			return $this->View->render($template);
+		} catch (MissingViewException $e) {
+			throw new MissingCellViewException(['file' => $template]);
+		}
 	}
 
 /**

+ 25 - 0
src/View/Error/MissingCellViewException.php

@@ -0,0 +1,25 @@
+<?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 a view file for a cell cannot be found.
+ */
+class MissingCellViewException extends Exception {
+
+	protected $_messageTemplate = 'Cell view file "%s" is missing.';
+
+}

+ 12 - 1
tests/TestCase/View/CellTest.php

@@ -81,7 +81,7 @@ class CellTest extends TestCase {
 		$capture = function ($errno, $msg) {
 			restore_error_handler();
 			$this->assertEquals(E_USER_WARNING, $errno);
-			$this->assertContains('Could not render cell - View file', $msg);
+			$this->assertContains('Could not render cell - Cell view file', $msg);
 		};
 		set_error_handler($capture);
 
@@ -134,6 +134,17 @@ class CellTest extends TestCase {
 	}
 
 /**
+ * Tests manual render() invocation with error
+ *
+ * @expectedException \Cake\View\Error\MissingCellViewException
+ * @return void
+ */
+	public function testCellManualRenderError() {
+		$cell = $this->View->cell('Articles');
+		$cell->render('derp');
+	}
+
+/**
  * Test rendering a cell with a theme.
  *
  * @return void