From a296106fac8403f3fccd91c56497b4e105380396 Mon Sep 17 00:00:00 2001 From: Blaine Schmeisser Date: Wed, 6 Feb 2013 13:11:40 -0600 Subject: [PATCH] Change `Mocker` to overwrite of `__call` and `__callStatic` methods. The benefit is that the calls are now stored inside of results which can then be asserted against. --- test/Mocker.php | 21 ++++++++++----------- tests/cases/test/MockerTest.php | 30 ++++++++++++++++++++++++++++++ tests/mocks/test/MockStdClass.php | 17 +++++++++++++++++ 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/test/Mocker.php b/test/Mocker.php index f2590adfcd..3312cec5ed 100644 --- a/test/Mocker.php +++ b/test/Mocker.php @@ -153,7 +153,7 @@ class Mocker { 'constructor' => array( '{:modifiers} function __construct({:args}) {', ' $args = compact({:stringArgs});', - ' $this->parent = func_get_arg(count(func_get_args()) - 1);', + ' $this->parent = func_get_arg(func_num_args() - 1);', ' $this->parent->mocker = $this;', ' if (method_exists("{:mocker}", "__construct")) {', ' call_user_func_array("parent::__construct", $args);', @@ -164,23 +164,22 @@ class Mocker { '{:modifiers} function {:method}({:args}) {', ' $args = compact({:stringArgs});', ' $token = spl_object_hash($this);', - ' if (func_get_arg(count(func_get_args()) - 1) !== $token) {', - ' $method = array($this->parent, "{:method}");', - ' return call_user_func_array($method, $args);', + ' if (func_num_args() > 0 && func_get_arg(func_num_args() - 1) === $token) {', + ' return call_user_func_array("parent::{:method}", compact({:stringArgs}));', ' }', - ' return call_user_func_array("parent::{:method}", compact({:stringArgs}));', + ' $method = array($this->parent, "{:method}");', + ' return call_user_func_array($method, $args);', '}', ), 'staticMethod' => array( '{:modifiers} function {:method}({:args}) {', ' $args = compact({:stringArgs});', ' $token = "1f3870be274f6c49b3e31a0c6728957f";', - ' $id = func_get_arg(count(func_get_args()) - 1);', - ' if (func_get_arg(count(func_get_args()) - 1) !== $token) {', - ' $method = \'{:namespace}\Mock::{:method}\';', - ' return call_user_func_array($method, $args);', + ' if (func_num_args() > 0 && func_get_arg(func_num_args() - 1) === $token) {', + ' return call_user_func_array("parent::{:method}", compact({:stringArgs}));', ' }', - ' return call_user_func_array("parent::{:method}", compact({:stringArgs}));', + ' $method = \'{:namespace}\Mock::{:method}\';', + ' return call_user_func_array($method, $args);', '}', ), 'endClass' => array( @@ -323,7 +322,7 @@ class Mocker { * @var array */ protected static $_blackList = array( - '__destruct', '__call', '__callStatic', '_parents', + '__destruct', '_parents', '__get', '__set', '__isset', '__unset', '__sleep', '__wakeup', '__toString', '__clone', '__invoke', '_stop', '_init', 'invokeMethod', '__set_state', diff --git a/tests/cases/test/MockerTest.php b/tests/cases/test/MockerTest.php index ec948adab6..71e255dfb3 100644 --- a/tests/cases/test/MockerTest.php +++ b/tests/cases/test/MockerTest.php @@ -392,6 +392,36 @@ public function testResetAllFunctions() { $this->assertInternalType('bool', $obj->isExecutable()); } + public function testMagicCallGetStoredResultsWhenCalled() { + $obj = new \lithium\tests\mocks\test\mockStdClass\Mock; + + $obj->__call('foo', array()); + $results = Mocker::mergeResults($obj->results, $obj::$staticResults); + + $this->assertArrayHasKey('__call', $results); + $this->assertArrayNotHasKey('__callStatic', $results); + } + + public function testMagicCallStaticGetStoredResultsWhenCalled() { + $obj = new \lithium\tests\mocks\test\mockStdClass\Mock; + + $obj->__callStatic('foo', array()); + $results = Mocker::mergeResults($obj->results, $obj::$staticResults); + + $this->assertArrayHasKey('__callStatic', $results); + $this->assertArrayNotHasKey('__call', $results); + } + + public function testMagicCallGetStoredResultsWhenCalledIndirectly() { + $obj = new \lithium\tests\mocks\test\mockStdClass\Mock; + + $obj->methodBar(); + $results = Mocker::mergeResults($obj->results, $obj::$staticResults); + + $this->assertArrayHasKey('__call', $results); + $this->assertCount(2, $results['__call']); + } + } ?> \ No newline at end of file diff --git a/tests/mocks/test/MockStdClass.php b/tests/mocks/test/MockStdClass.php index 2461cfa357..9ef3c50239 100644 --- a/tests/mocks/test/MockStdClass.php +++ b/tests/mocks/test/MockStdClass.php @@ -44,6 +44,23 @@ public function isExecutable() { return is_executable(__FILE__); } + public static function methodFoo() { + return true; + } + + public function __call($method, $params) { + return $this->methodFoo(); + } + + public static function __callStatic($method, $params) { + return static::methodFoo(); + } + + public function methodBar() { + $this->methodBaz(1); + return $this->__call('methodBaz', array(2)); + } + } ?> \ No newline at end of file