Skip to content
Browse files

refactor to pass closure to mock method using returning(), rather tha…

…n receives()
  • Loading branch information...
1 parent 9542b8c commit 0a31703d3c5f80c17ff1ce755893e5ba79a863ea @jaz303 committed
Showing with 12 additions and 5 deletions.
  1. +2 −1 examples/test/TestMocking.php
  2. +10 −4 inc/mocking.php
View
3 examples/test/TestMocking.php
@@ -90,7 +90,8 @@ public function test_mock_can_implement_interfaces() {
public function test_mock_can_implement_method_using_closure() {
$obj = Mock::generate()
- ->receives('foo', function($name) { return "my name is $name"; })
+ ->receives('foo')
+ ->returning(function($name) { return "my name is $name"; })
->construct();
assert_equal('my name is jason', $obj->foo('jason'));
View
14 inc/mocking.php
@@ -53,8 +53,8 @@ public function implement($interface) {
return $this;
}
- public function receives($method_pattern, $closure = null) {
- $mock_method = new MockMethodSpecification($this, $method_pattern, $closure);
+ public function receives($method_pattern) {
+ $mock_method = new MockMethodSpecification($this, $method_pattern);
$this->methods[$method_pattern] = $mock_method;
return $mock_method;
}
@@ -158,10 +158,9 @@ class MockMethodSpecification
private $pattern;
private $closure;
- public function __construct(MockSpecification $mock, $pattern, $closure = null) {
+ public function __construct(MockSpecification $mock, $pattern) {
$this->mock = $mock;
$this->pattern = $pattern;
- $this->closure = $closure;
}
public function get_pattern() {
@@ -172,6 +171,13 @@ public function get_closure() {
return $this->closure;
}
+ public function returning($thing) {
+ if ($thing instanceof Closure) {
+ $this->closure = $thing;
+ }
+ return $this;
+ }
+
public function back() {
return $this->mock;
}

0 comments on commit 0a31703

Please sign in to comment.
Something went wrong with that request. Please try again.