Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions src/EasyMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace EasyMock;

use PHPUnit_Framework_MockObject_Generator;
use PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount as AnyInvokedCount;
use PHPUnit_Framework_MockObject_Matcher_Invocation as InvocationMatcher;
use PHPUnit_Framework_MockObject_Matcher_InvokedAtLeastOnce as InvokedAtLeastOnce;
Expand All @@ -13,7 +12,7 @@
*
* @author Matthieu Napoli <matthieu@mnapoli.fr>
*/
class EasyMock
trait EasyMock
{
/**
* Mock the given class.
Expand All @@ -26,16 +25,16 @@ class EasyMock
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
public static function mock($classname, array $methods = array())
protected function easyMock($classname, array $methods = array())
{
if ($classname instanceof MockObject) {
$mock = $classname;
} else {
$mock = self::createMock($classname);
$mock = $this->createMock($classname);
}

foreach ($methods as $method => $return) {
self::mockMethod($mock, $method, new AnyInvokedCount, $return);
$this->mockMethod($mock, $method, new AnyInvokedCount, $return);
}

return $mock;
Expand All @@ -47,29 +46,29 @@ public static function mock($classname, array $methods = array())
* This is the same as EasyMock::mock() except this assert that methods are called at
* least once.
*
* @see mock()
* @see easyMock()
*
* @param string $classname The class to mock. Can also be an existing mock to mock new methods.
* @param array $methods Array of values to return, indexed by the method name.
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
public static function spy($classname, array $methods = array())
protected function easySpy($classname, array $methods = array())
{
if ($classname instanceof MockObject) {
$mock = $classname;
} else {
$mock = self::createMock($classname);
$mock = $this->createMock($classname);
}

foreach ($methods as $method => $return) {
self::mockMethod($mock, $method, new InvokedAtLeastOnce, $return);
$this->mockMethod($mock, $method, new InvokedAtLeastOnce, $return);
}

return $mock;
}

private static function mockMethod(MockObject $mock, $method, InvocationMatcher $invocation, $return)
private function mockMethod(MockObject $mock, $method, InvocationMatcher $invocation, $return)
{
$methodAssertion = $mock->expects($invocation)
->method($method);
Expand All @@ -87,11 +86,9 @@ private static function mockMethod(MockObject $mock, $method, InvocationMatcher
* @param string $classname
* @return MockObject
*/
private static function createMock($classname)
private function createMock($classname)
{
$mockGenerator = new PHPUnit_Framework_MockObject_Generator();

return $mockGenerator->getMock(
return $this->getMock(
$classname,
array(),
array(),
Expand Down
22 changes: 13 additions & 9 deletions tests/MockClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
*/
class MockClassTest extends \PHPUnit_Framework_TestCase
{
use EasyMock;

/**
* @test
*/
public function should_mock_objects()
{
/** @var ClassFixture $mock */
$mock = EasyMock::mock('EasyMock\Test\Fixture\ClassFixture');
$mock = $this->easyMock('EasyMock\Test\Fixture\ClassFixture');

$this->assertInstanceOf('PHPUnit_Framework_MockObject_MockObject', $mock);
$this->assertNull($mock->foo());
Expand All @@ -30,7 +32,7 @@ public function should_mock_objects()
public function should_mock_interfaces()
{
/** @var InterfaceFixture $mock */
$mock = EasyMock::mock('EasyMock\Test\Fixture\InterfaceFixture');
$mock = $this->easyMock('EasyMock\Test\Fixture\InterfaceFixture');

$this->assertInstanceOf('PHPUnit_Framework_MockObject_MockObject', $mock);
$this->assertNull($mock->foo());
Expand All @@ -42,7 +44,7 @@ public function should_mock_interfaces()
public function not_mocked_methods_should_return_null()
{
/** @var ClassFixture $mock */
$mock = EasyMock::mock('EasyMock\Test\Fixture\ClassFixture');
$mock = $this->easyMock('EasyMock\Test\Fixture\ClassFixture');

$this->assertNull($mock->foo());
}
Expand All @@ -53,7 +55,7 @@ public function not_mocked_methods_should_return_null()
public function should_mock_existing_method_with_a_value()
{
/** @var ClassFixture $mock */
$mock = EasyMock::mock('EasyMock\Test\Fixture\ClassFixture', array(
$mock = $this->easyMock('EasyMock\Test\Fixture\ClassFixture', array(
'foo' => 'bar',
));

Expand All @@ -66,7 +68,7 @@ public function should_mock_existing_method_with_a_value()
public function should_mock_existing_method_with_a_callback()
{
/** @var ClassFixture $mock */
$mock = EasyMock::mock('EasyMock\Test\Fixture\ClassFixture', array(
$mock = $this->easyMock('EasyMock\Test\Fixture\ClassFixture', array(
'foo' => function () {
return 'bar';
},
Expand All @@ -83,7 +85,7 @@ public function should_mock_existing_method_with_a_callback()
public function should_mock_existing_method_to_throw_exception()
{
/** @var ClassFixture $mock */
$mock = EasyMock::mock('EasyMock\Test\Fixture\ClassFixture', array(
$mock = $this->easyMock('EasyMock\Test\Fixture\ClassFixture', array(
'foo' => new CustomException('My message'),
));
$mock->foo();
Expand All @@ -95,8 +97,8 @@ public function should_mock_existing_method_to_throw_exception()
public function should_mock_new_methods_on_existing_mock()
{
/** @var ClassFixture $mock */
$mock = EasyMock::mock('EasyMock\Test\Fixture\ClassFixture');
$mock = EasyMock::mock($mock, array(
$mock = $this->easyMock('EasyMock\Test\Fixture\ClassFixture');
$mock = $this->easyMock($mock, array(
'foo' => 'bar',
));

Expand All @@ -108,8 +110,10 @@ public function should_mock_new_methods_on_existing_mock()
*/
public function should_allow_to_spy_method_calls()
{
$mock = EasyMock::spy('EasyMock\Test\Fixture\ClassFixture', array(
$mock = $this->easySpy('EasyMock\Test\Fixture\ClassFixture', array(
'foo' => 'bar',
));

$this->assertEquals('bar', $mock->foo());
}
}