From 4ef17b5ab4a335f297a1332a6dba527b09feda54 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 8 Jun 2014 15:01:04 -0400 Subject: [PATCH] Throw exceptions when cell methods are missing. Use reflection to capture missing cell methods and raise proper errors on them. Refs #3664 --- src/View/CellTrait.php | 14 +++++++++++--- tests/TestCase/View/CellTest.php | 11 +++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/View/CellTrait.php b/src/View/CellTrait.php index cb7895a1b62..4896a50cdb9 100644 --- a/src/View/CellTrait.php +++ b/src/View/CellTrait.php @@ -80,9 +80,17 @@ public function cell($cell, $data = [], $options = []) { $data = array_values($data); } - call_user_func_array([$cellInstance, $action], $data); - - return $cellInstance; + try { + $reflect = new \ReflectionMethod($cellInstance, $action); + $reflect->invokeArgs($cellInstance, $data); + return $cellInstance; + } catch (\ReflectionException $e) { + throw new \BadMethodCallException(sprintf( + 'Class %s does not have a "%s" method.', + $className, + $action + )); + } } } diff --git a/tests/TestCase/View/CellTest.php b/tests/TestCase/View/CellTest.php index eb73bc0e627..648cba2a7ef 100644 --- a/tests/TestCase/View/CellTest.php +++ b/tests/TestCase/View/CellTest.php @@ -147,4 +147,15 @@ public function testUnexistingCell() { $cell = $this->View->cell('Void::echoThis', ['arg1' => 'v1', 'arg2' => 'v2']); } +/** + * Tests missing method errors + * + * @expectedException \BadMethodCallException + * @expectedExceptionMessage Class TestApp\View\Cell\ArticlesCell does not have a "nope" method. + * @return void + */ + public function testCellMissingMethod() { + $this->View->cell('Articles::nope'); + } + }