Skip to content

Commit

Permalink
Add logging ability to Mocker for later assertions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Blaine Schmeisser committed Jan 17, 2013
1 parent 80a13bc commit c87910b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
2 changes: 1 addition & 1 deletion analysis/Debugger.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* The `Debugger` class provides basic facilities for generating and rendering meta-data about the * The `Debugger` class provides basic facilities for generating and rendering meta-data about the
* state of an application in its current context. * state of an application in its current context.
*/ */
class Debugger extends \lithium\core\Object { class Debugger extends \lithium\core\StaticObject {


/** /**
* Used for temporary closure caching. * Used for temporary closure caching.
Expand Down
29 changes: 26 additions & 3 deletions test/Mocker.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -123,18 +123,23 @@ class Mocker {
* interface for consumers, instantiation or static method calls, and can * interface for consumers, instantiation or static method calls, and can
* have most of its methods filtered. * have most of its methods filtered.
* *
* The `$results` variable holds all method calls allowing you for you
* make your own custom assertions on them.
*
* @var array * @var array
*/ */
protected static $_mockIngredients = array( protected static $_mockIngredients = array(
'startClass' => array( 'startClass' => array(
'namespace {:namespace};', 'namespace {:namespace};',
'class Mock extends \{:mocker} {', 'class Mock extends \{:mocker} {',
' public $mocker;', ' public $mocker;',
' public {:static} $results = array();',
' protected $_safeVars = array(', ' protected $_safeVars = array(',
' "_classes",', ' "_classes",',
' "_methodFilters",', ' "_methodFilters",',
' "mocker",', ' "mocker",',
' "_safeVars"', ' "_safeVars",',
' "results",',
' );', ' );',
), ),
'get' => array( 'get' => array(
Expand Down Expand Up @@ -163,19 +168,37 @@ class Mocker {
' $args = func_get_args();', ' $args = func_get_args();',
' array_push($args, "1f3870be274f6c49b3e31a0c6728957f");', ' array_push($args, "1f3870be274f6c49b3e31a0c6728957f");',
' $method = \'{:namespace}\MockDelegate::{:method}\';', ' $method = \'{:namespace}\MockDelegate::{:method}\';',
' return self::_filter("{:method}", $args, function($self, $args) use(&$method) {', ' $result = self::_filter("{:method}", $args, function($self, $args) use(&$method) {',
' return call_user_func_array($method, $args);', ' return call_user_func_array($method, $args);',
' });', ' });',
' if (!isset(self::$results["{:method}"])) {',
' self::$results["{:method}"] = array();',
' }',
' self::$results["{:method}"][] = array(',
' "args" => func_get_args(),',
' "result" => $result,',
' "time" => microtime(true),',
' );',
' return $result;',
'}', '}',
), ),
'method' => array( 'method' => array(
'{:modifiers} function {:method}({:args}) {', '{:modifiers} function {:method}({:args}) {',
' $args = func_get_args();', ' $args = func_get_args();',
' array_push($args, spl_object_hash($this->mocker));', ' array_push($args, spl_object_hash($this->mocker));',
' $method = array($this->mocker, "{:method}");', ' $method = array($this->mocker, "{:method}");',
' return $this->_filter(__METHOD__, $args, function($self, $args) use(&$method) {', ' $result = $this->_filter(__METHOD__, $args, function($self, $args) use(&$method) {',
' return call_user_func_array($method, $args);', ' return call_user_func_array($method, $args);',
' });', ' });',
' if (!isset($this->results["{:method}"])) {',
' $this->results["{:method}"] = array();',
' }',
' $this->results["{:method}"][] = array(',
' "args" => func_get_args(),',
' "result" => $result,',
' "time" => microtime(true),',
' );',
' return $result;',
'}', '}',
), ),
'endClass' => array( 'endClass' => array(
Expand Down
40 changes: 40 additions & 0 deletions tests/cases/test/MockerTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -139,6 +139,46 @@ public function testFilteringAFilteredMethod() {
$this->assertFalse($adapt::_initAdapter('foo', array())); $this->assertFalse($adapt::_initAdapter('foo', array()));
} }


public function testStaticResults() {
$docblock = 'lithium\analysis\docblock\Mock';
$docblock::applyFilter(array('comment', 'tags'), function($self, $params, $chain) {
return false;
});
$docblock::comment('foo', 'foobar');
$docblock::comment('bar');
$docblock::tags('baz');

$this->assertIdentical(2, count($docblock::$results['comment']));
$this->assertIdentical(array('foo', 'foobar'), $docblock::$results['comment'][0]['args']);
$this->assertIdentical(false, $docblock::$results['comment'][0]['result']);
$this->assertIdentical(array('bar'), $docblock::$results['comment'][1]['args']);
$this->assertIdentical(false, $docblock::$results['comment'][1]['result']);

$this->assertIdentical(1, count($docblock::$results['tags']));
$this->assertIdentical(array('baz'), $docblock::$results['tags'][0]['args']);
$this->assertIdentical(false, $docblock::$results['tags'][0]['result']);
}

public function testInstanceResults() {
$debugger = new \lithium\data\schema\Mock;
$debugger->applyFilter(array('names', 'meta'), function($self, $params, $chain) {
return false;
});
$debugger->names('foo', 'foobar');
$debugger->names('bar');
$debugger->meta('baz');

$this->assertIdentical(2, count($debugger->results['names']));
$this->assertIdentical(array('foo', 'foobar'), $debugger->results['names'][0]['args']);
$this->assertIdentical(false, $debugger->results['names'][0]['result']);
$this->assertIdentical(array('bar'), $debugger->results['names'][1]['args']);
$this->assertIdentical(false, $debugger->results['names'][1]['result']);

$this->assertIdentical(1, count($debugger->results['meta']));
$this->assertIdentical(array('baz'), $debugger->results['meta'][0]['args']);
$this->assertIdentical(false, $debugger->results['meta'][0]['result']);
}

} }


?> ?>

0 comments on commit c87910b

Please sign in to comment.