Skip to content

Commit

Permalink
Merge pull request #2 from sirbrillig/add/mock-object-ignore-missing
Browse files Browse the repository at this point in the history
Add `MockObject->and_ignore_missing()`
  • Loading branch information
sirbrillig committed Apr 28, 2016
2 parents 5ea707d + 2da1bcb commit b0dac65
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@

- `add_method( $function_name, $function = null )`: Add a public method to this Object as a Spy and return that method. Creates and returns a Spy if no function is provided.
- `spy_on_method( $function_name, $function = null )`: Alias for `add_method()`.
- `and_ignore_missing()`: Prevents throwing an Exception when an unmocked method is called on this object.

# Expectation

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@ function test_greeter() {
}
```

If you'd rather not call `add_method()` and you don't have an original class to copy, you can also just ignore all method calls on the object using `and_ignore_missing()`:

```php
function test_greeter() {
$mock = \Spies\mock_object()->and_ignore_missing();
$this->assertEquals( null, $mock->say_goodbye() );
}
```

## Expectations

Spies can be useful all by themselves, but Spies also provides the `Expectation` class to make writing your test expectations easier.
Expand Down
17 changes: 16 additions & 1 deletion src/Spies/MockObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class MockObject {

private $class_name = null;

private $ignore_missing_methods = false;

public function __construct( $class_name = null ) {
$this->class_name = $class_name;
if ( isset( $class_name ) ) {
Expand All @@ -14,7 +16,10 @@ public function __construct( $class_name = null ) {

public function __call( $function_name, $args ) {
if ( ! isset( $this->$function_name ) || ! is_callable( $this->$function_name ) ) {
throw new \Exception( 'Attempted to call un-mocked method "' . $function_name . '" with ' . json_encode( $args ) );
if ( $this->ignore_missing_methods ) {
return;
}
throw new UndefinedFunctionException( 'Attempted to call un-mocked method "' . $function_name . '" with ' . json_encode( $args ) );
}
return call_user_func_array( $this->$function_name, $args );
}
Expand Down Expand Up @@ -62,6 +67,16 @@ public function add_method( $function_name, $function = null ) {
return $function;
}

/**
* Prevent throwing UndefinedFunctionException when an unmocked method is called
*
* @return MockObject This object.
*/
public function and_ignore_missing() {
$this->ignore_missing_methods = true;
return $this;
}

public static function mock_object( $class_name = null) {
return new MockObject( $class_name );
}
Expand Down
11 changes: 11 additions & 0 deletions tests/MockObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,15 @@ public function test_spy_on_method_is_an_alias_for_add_method() {
$this->assertEquals( 'greetings', $mock->say_hello() );
$this->assertEquals( null, $mock->say_goodbye() );
}

public function test_mock_object_throws_error_when_unmocked_method_is_called() {
$this->setExpectedException( '\Spies\UndefinedFunctionException' );
$mock = \Spies\mock_object();
$mock->foobar();
}

public function test_mock_object_with_ignore_missing_does_not_throw_error_when_unmocked_method_is_called() {
$mock = \Spies\mock_object()->and_ignore_missing();
$mock->foobar();
}
}

0 comments on commit b0dac65

Please sign in to comment.