Skip to content

Commit 526ab19

Browse files
committed
Refactor to be a trait instead of using static methods
This allows us to use PHPUnit's getMock() method for mock creation which registers the created mocks to check their expectations which is needed to check if a spy was actually called.
1 parent b6b08a3 commit 526ab19

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

src/EasyMock.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace EasyMock;
44

5-
use PHPUnit_Framework_MockObject_Generator;
65
use PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount as AnyInvokedCount;
76
use PHPUnit_Framework_MockObject_Matcher_Invocation as InvocationMatcher;
87
use PHPUnit_Framework_MockObject_Matcher_InvokedAtLeastOnce as InvokedAtLeastOnce;
@@ -13,7 +12,7 @@
1312
*
1413
* @author Matthieu Napoli <[email protected]>
1514
*/
16-
class EasyMock
15+
trait EasyMock
1716
{
1817
/**
1918
* Mock the given class.
@@ -26,16 +25,16 @@ class EasyMock
2625
*
2726
* @return \PHPUnit_Framework_MockObject_MockObject
2827
*/
29-
public static function mock($classname, array $methods = array())
28+
protected function easyMock($classname, array $methods = array())
3029
{
3130
if ($classname instanceof MockObject) {
3231
$mock = $classname;
3332
} else {
34-
$mock = self::createMock($classname);
33+
$mock = $this->createMock($classname);
3534
}
3635

3736
foreach ($methods as $method => $return) {
38-
self::mockMethod($mock, $method, new AnyInvokedCount, $return);
37+
$this->mockMethod($mock, $method, new AnyInvokedCount, $return);
3938
}
4039

4140
return $mock;
@@ -47,29 +46,29 @@ public static function mock($classname, array $methods = array())
4746
* This is the same as EasyMock::mock() except this assert that methods are called at
4847
* least once.
4948
*
50-
* @see mock()
49+
* @see easyMock()
5150
*
5251
* @param string $classname The class to mock. Can also be an existing mock to mock new methods.
5352
* @param array $methods Array of values to return, indexed by the method name.
5453
*
5554
* @return \PHPUnit_Framework_MockObject_MockObject
5655
*/
57-
public static function spy($classname, array $methods = array())
56+
protected function easySpy($classname, array $methods = array())
5857
{
5958
if ($classname instanceof MockObject) {
6059
$mock = $classname;
6160
} else {
62-
$mock = self::createMock($classname);
61+
$mock = $this->createMock($classname);
6362
}
6463

6564
foreach ($methods as $method => $return) {
66-
self::mockMethod($mock, $method, new InvokedAtLeastOnce, $return);
65+
$this->mockMethod($mock, $method, new InvokedAtLeastOnce, $return);
6766
}
6867

6968
return $mock;
7069
}
7170

72-
private static function mockMethod(MockObject $mock, $method, InvocationMatcher $invocation, $return)
71+
private function mockMethod(MockObject $mock, $method, InvocationMatcher $invocation, $return)
7372
{
7473
$methodAssertion = $mock->expects($invocation)
7574
->method($method);
@@ -87,11 +86,9 @@ private static function mockMethod(MockObject $mock, $method, InvocationMatcher
8786
* @param string $classname
8887
* @return MockObject
8988
*/
90-
private static function createMock($classname)
89+
private function createMock($classname)
9190
{
92-
$mockGenerator = new PHPUnit_Framework_MockObject_Generator();
93-
94-
return $mockGenerator->getMock(
91+
return $this->getMock(
9592
$classname,
9693
array(),
9794
array(),

tests/MockClassTest.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
*/
1313
class MockClassTest extends \PHPUnit_Framework_TestCase
1414
{
15+
use EasyMock;
16+
1517
/**
1618
* @test
1719
*/
1820
public function should_mock_objects()
1921
{
2022
/** @var ClassFixture $mock */
21-
$mock = EasyMock::mock('EasyMock\Test\Fixture\ClassFixture');
23+
$mock = $this->easyMock('EasyMock\Test\Fixture\ClassFixture');
2224

2325
$this->assertInstanceOf('PHPUnit_Framework_MockObject_MockObject', $mock);
2426
$this->assertNull($mock->foo());
@@ -30,7 +32,7 @@ public function should_mock_objects()
3032
public function should_mock_interfaces()
3133
{
3234
/** @var InterfaceFixture $mock */
33-
$mock = EasyMock::mock('EasyMock\Test\Fixture\InterfaceFixture');
35+
$mock = $this->easyMock('EasyMock\Test\Fixture\InterfaceFixture');
3436

3537
$this->assertInstanceOf('PHPUnit_Framework_MockObject_MockObject', $mock);
3638
$this->assertNull($mock->foo());
@@ -42,7 +44,7 @@ public function should_mock_interfaces()
4244
public function not_mocked_methods_should_return_null()
4345
{
4446
/** @var ClassFixture $mock */
45-
$mock = EasyMock::mock('EasyMock\Test\Fixture\ClassFixture');
47+
$mock = $this->easyMock('EasyMock\Test\Fixture\ClassFixture');
4648

4749
$this->assertNull($mock->foo());
4850
}
@@ -53,7 +55,7 @@ public function not_mocked_methods_should_return_null()
5355
public function should_mock_existing_method_with_a_value()
5456
{
5557
/** @var ClassFixture $mock */
56-
$mock = EasyMock::mock('EasyMock\Test\Fixture\ClassFixture', array(
58+
$mock = $this->easyMock('EasyMock\Test\Fixture\ClassFixture', array(
5759
'foo' => 'bar',
5860
));
5961

@@ -66,7 +68,7 @@ public function should_mock_existing_method_with_a_value()
6668
public function should_mock_existing_method_with_a_callback()
6769
{
6870
/** @var ClassFixture $mock */
69-
$mock = EasyMock::mock('EasyMock\Test\Fixture\ClassFixture', array(
71+
$mock = $this->easyMock('EasyMock\Test\Fixture\ClassFixture', array(
7072
'foo' => function () {
7173
return 'bar';
7274
},
@@ -83,7 +85,7 @@ public function should_mock_existing_method_with_a_callback()
8385
public function should_mock_existing_method_to_throw_exception()
8486
{
8587
/** @var ClassFixture $mock */
86-
$mock = EasyMock::mock('EasyMock\Test\Fixture\ClassFixture', array(
88+
$mock = $this->easyMock('EasyMock\Test\Fixture\ClassFixture', array(
8789
'foo' => new CustomException('My message'),
8890
));
8991
$mock->foo();
@@ -95,8 +97,8 @@ public function should_mock_existing_method_to_throw_exception()
9597
public function should_mock_new_methods_on_existing_mock()
9698
{
9799
/** @var ClassFixture $mock */
98-
$mock = EasyMock::mock('EasyMock\Test\Fixture\ClassFixture');
99-
$mock = EasyMock::mock($mock, array(
100+
$mock = $this->easyMock('EasyMock\Test\Fixture\ClassFixture');
101+
$mock = $this->easyMock($mock, array(
100102
'foo' => 'bar',
101103
));
102104

@@ -108,8 +110,10 @@ public function should_mock_new_methods_on_existing_mock()
108110
*/
109111
public function should_allow_to_spy_method_calls()
110112
{
111-
$mock = EasyMock::spy('EasyMock\Test\Fixture\ClassFixture', array(
113+
$mock = $this->easySpy('EasyMock\Test\Fixture\ClassFixture', array(
112114
'foo' => 'bar',
113115
));
116+
117+
$this->assertEquals('bar', $mock->foo());
114118
}
115119
}

0 commit comments

Comments
 (0)