Skip to content

Commit 9710e17

Browse files
committed
Support for throwing exception
1 parent 77112ff commit 9710e17

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ $mock = EasyMock::mock('My\Class', array(
6060
));
6161
```
6262

63+
You can also have methods throw exceptions by providing an `Exception` instance:
64+
65+
```php
66+
$mock = EasyMock::mock('My\Class', array(
67+
'sayHello' => new \RuntimeException('Whoops'),
68+
));
69+
```
70+
6371
### What if?
6472

6573
If you want to use assertions or other PHPUnit features, just do it:

src/EasyMock.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ private static function mockMethod(PHPUnit_Framework_MockObject_MockObject $mock
5151

5252
if (is_callable($return)) {
5353
$methodAssertion->willReturnCallback($return);
54+
} elseif ($return instanceof \Exception) {
55+
$methodAssertion->willThrowException($return);
5456
} else {
5557
$methodAssertion->willReturn($return);
5658
}

tests/Fixture/CustomException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyMock\Test\Fixture;
4+
5+
class CustomException extends \Exception
6+
{
7+
}

tests/MockClassTest.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use EasyMock\EasyMock;
66
use EasyMock\Test\Fixture\ClassFixture;
7+
use EasyMock\Test\Fixture\CustomException;
8+
use EasyMock\Test\Fixture\InterfaceFixture;
79

810
/**
911
* @author Matthieu Napoli <matthieu@mnapoli.fr>
@@ -15,6 +17,7 @@ class MockClassTest extends \PHPUnit_Framework_TestCase
1517
*/
1618
public function should_mock_objects()
1719
{
20+
/** @var ClassFixture $mock */
1821
$mock = EasyMock::mock('EasyMock\Test\Fixture\ClassFixture');
1922

2023
$this->assertInstanceOf('PHPUnit_Framework_MockObject_MockObject', $mock);
@@ -26,12 +29,24 @@ public function should_mock_objects()
2629
*/
2730
public function should_mock_interfaces()
2831
{
32+
/** @var InterfaceFixture $mock */
2933
$mock = EasyMock::mock('EasyMock\Test\Fixture\InterfaceFixture');
3034

3135
$this->assertInstanceOf('PHPUnit_Framework_MockObject_MockObject', $mock);
3236
$this->assertNull($mock->foo());
3337
}
3438

39+
/**
40+
* @test
41+
*/
42+
public function not_mocked_methods_should_return_null()
43+
{
44+
/** @var ClassFixture $mock */
45+
$mock = EasyMock::mock('EasyMock\Test\Fixture\ClassFixture');
46+
47+
$this->assertNull($mock->foo());
48+
}
49+
3550
/**
3651
* @test
3752
*/
@@ -62,12 +77,15 @@ public function should_mock_existing_method_with_a_callback()
6277

6378
/**
6479
* @test
80+
* @expectedException \EasyMock\Test\Fixture\CustomException
81+
* @expectedExceptionMessage My message
6582
*/
66-
public function not_mocked_methods_should_return_null()
83+
public function should_mock_existing_method_to_throw_exception()
6784
{
6885
/** @var ClassFixture $mock */
69-
$mock = EasyMock::mock('EasyMock\Test\Fixture\ClassFixture');
70-
71-
$this->assertSame(null, $mock->foo());
86+
$mock = EasyMock::mock('EasyMock\Test\Fixture\ClassFixture', array(
87+
'foo' => new CustomException('My message'),
88+
));
89+
$mock->foo();
7290
}
7391
}

0 commit comments

Comments
 (0)