Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add logging ability to Mocker for later assertions.

  • Loading branch information...
commit c87910b5a9c2c88a6c4eb588809d2c1fff034d4e 1 parent 80a13bc
@blainesch blainesch authored
View
2  analysis/Debugger.php
@@ -16,7 +16,7 @@
* The `Debugger` class provides basic facilities for generating and rendering meta-data about the
* 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.
View
29 test/Mocker.php
@@ -123,6 +123,9 @@ class Mocker {
* interface for consumers, instantiation or static method calls, and can
* 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
*/
protected static $_mockIngredients = array(
@@ -130,11 +133,13 @@ class Mocker {
'namespace {:namespace};',
'class Mock extends \{:mocker} {',
' public $mocker;',
+ ' public {:static} $results = array();',
' protected $_safeVars = array(',
' "_classes",',
' "_methodFilters",',
' "mocker",',
- ' "_safeVars"',
+ ' "_safeVars",',
+ ' "results",',
' );',
),
'get' => array(
@@ -163,9 +168,18 @@ class Mocker {
' $args = func_get_args();',
' array_push($args, "1f3870be274f6c49b3e31a0c6728957f");',
' $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);',
' });',
+ ' 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(
@@ -173,9 +187,18 @@ class Mocker {
' $args = func_get_args();',
' array_push($args, spl_object_hash($this->mocker));',
' $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);',
' });',
+ ' 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(
View
40 tests/cases/test/MockerTest.php
@@ -139,6 +139,46 @@ public function testFilteringAFilteredMethod() {
$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']);
+ }
+
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.