Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Change `Mocker` to overwrite of `__call` and `__callStatic` methods. #816

Merged
merged 1 commit into from

2 participants

@blainesch
Collaborator

The benefit is that the calls are now stored inside of results which can then be asserted against.

I honestly don't recall why I blacklisted all of those methods, I wish I would have documented it better.

@blainesch blainesch 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.
a296106
@nateabele nateabele merged commit 583a0c4 into UnionOfRAD:dev
@blainesch blainesch deleted the unknown repository branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 6, 2013
  1. @blainesch

    Change `Mocker` to overwrite of `__call` and `__callStatic` methods.

    blainesch authored
    The benefit is that the calls are now stored inside of results which can then be asserted against.
This page is out of date. Refresh to see the latest.
View
21 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',
View
30 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']);
+ }
+
}
?>
View
17 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));
+ }
+
}
?>
Something went wrong with that request. Please try again.