Skip to content

Commit

Permalink
Change Mocker to overwrite of __call and __callStatic methods.
Browse files Browse the repository at this point in the history
The benefit is that the calls are now stored inside of results which can then be asserted against.
  • Loading branch information
Blaine Schmeisser committed Feb 6, 2013
1 parent 6092755 commit a296106
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 11 deletions.
21 changes: 10 additions & 11 deletions test/Mocker.php
Expand Up @@ -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);',
Expand All @@ -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(
Expand Down Expand Up @@ -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',
Expand Down
30 changes: 30 additions & 0 deletions tests/cases/test/MockerTest.php
Expand Up @@ -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']);
}

}

?>
17 changes: 17 additions & 0 deletions tests/mocks/test/MockStdClass.php
Expand Up @@ -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));
}

}

?>

0 comments on commit a296106

Please sign in to comment.