Skip to content

Commit

Permalink
Support for throwing exception
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Jan 16, 2015
1 parent 77112ff commit 9710e17
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ $mock = EasyMock::mock('My\Class', array(
));
```

You can also have methods throw exceptions by providing an `Exception` instance:

```php
$mock = EasyMock::mock('My\Class', array(
'sayHello' => new \RuntimeException('Whoops'),
));
```

### What if?

If you want to use assertions or other PHPUnit features, just do it:
Expand Down
2 changes: 2 additions & 0 deletions src/EasyMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ private static function mockMethod(PHPUnit_Framework_MockObject_MockObject $mock

if (is_callable($return)) {
$methodAssertion->willReturnCallback($return);
} elseif ($return instanceof \Exception) {
$methodAssertion->willThrowException($return);
} else {
$methodAssertion->willReturn($return);
}
Expand Down
7 changes: 7 additions & 0 deletions tests/Fixture/CustomException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EasyMock\Test\Fixture;

class CustomException extends \Exception
{
}
26 changes: 22 additions & 4 deletions tests/MockClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use EasyMock\EasyMock;
use EasyMock\Test\Fixture\ClassFixture;
use EasyMock\Test\Fixture\CustomException;
use EasyMock\Test\Fixture\InterfaceFixture;

/**
* @author Matthieu Napoli <[email protected]>
Expand All @@ -15,6 +17,7 @@ class MockClassTest extends \PHPUnit_Framework_TestCase
*/
public function should_mock_objects()
{
/** @var ClassFixture $mock */
$mock = EasyMock::mock('EasyMock\Test\Fixture\ClassFixture');

$this->assertInstanceOf('PHPUnit_Framework_MockObject_MockObject', $mock);
Expand All @@ -26,12 +29,24 @@ public function should_mock_objects()
*/
public function should_mock_interfaces()
{
/** @var InterfaceFixture $mock */
$mock = EasyMock::mock('EasyMock\Test\Fixture\InterfaceFixture');

$this->assertInstanceOf('PHPUnit_Framework_MockObject_MockObject', $mock);
$this->assertNull($mock->foo());
}

/**
* @test
*/
public function not_mocked_methods_should_return_null()
{
/** @var ClassFixture $mock */
$mock = EasyMock::mock('EasyMock\Test\Fixture\ClassFixture');

$this->assertNull($mock->foo());
}

/**
* @test
*/
Expand Down Expand Up @@ -62,12 +77,15 @@ public function should_mock_existing_method_with_a_callback()

/**
* @test
* @expectedException \EasyMock\Test\Fixture\CustomException
* @expectedExceptionMessage My message
*/
public function not_mocked_methods_should_return_null()
public function should_mock_existing_method_to_throw_exception()
{
/** @var ClassFixture $mock */
$mock = EasyMock::mock('EasyMock\Test\Fixture\ClassFixture');

$this->assertSame(null, $mock->foo());
$mock = EasyMock::mock('EasyMock\Test\Fixture\ClassFixture', array(
'foo' => new CustomException('My message'),
));
$mock->foo();
}
}

0 comments on commit 9710e17

Please sign in to comment.