Skip to content

Commit

Permalink
Changed spy name to Partial Mock
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Revah committed Mar 14, 2015
1 parent 3ef5be8 commit 472953d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* [Installation](#installation)
* [Mocking](#mocking-examples)
* [Stubbing](#stubbing)
* [Spies](#spies)
* [Partial Mock](#partial-mock)
* [Stubbing Method Chaining](#stubbing-method-chaining)
* [Verifying](#verifying)
* [Argument Matcher](#argument-matcher)
Expand Down Expand Up @@ -66,23 +66,23 @@ Methods:
* `returns($response)` - Returns a $response
* `callback(function() { /*...*/ })` - Calling a callback

## Spies
## Partial Mock

Spies are a partial mock, sometimes you need some of the methods to behave normally except for the one method that you need to test. That so called partial mocking can be done using the spy method
sometimes you need some of the methods to behave normally except for the one method that you need to test. That so called partial mocking can be done using the partialMock method

```php
class Foo {
function bar() { return 'bar'; }
}

$mock = ShortifyPunit::mock('Foo');
$spy = ShortifyPunit::spy('Foo');
$partialMock = ShortifyPunit::partialMock('Foo');

$mock->bar(); // returns NULL
echo $spy->bar(); // prints 'bar'
echo $partialMock->bar(); // prints 'bar'

ShortifyPunit::when($spy)->bar()->returns('foo'); // stubbing spy
echo $spy->bar(); // prints 'foo'
ShortifyPunit::when($partialMock)->bar()->returns('foo'); // stubbing partialMock
echo $partialMock->bar(); // prints 'foo'
```

## Stubbing Method Chaining
Expand Down
22 changes: 15 additions & 7 deletions src/ShortifyPunit.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,29 +134,29 @@ public static function mock($mockedClass)
}

/**
* Partial Mocking interfaces|classes
* Partial Mocking interfaces & classes
*
* @desc Partial mock is not stubbing any function by default (to NULL) like in regular mock()
*
* Examples:
* // class to partial mock / spy
* // class to partial mock
* class Foo {
* function bar() { return 'bar'; }
* }
*
* $mock = ShortifyPunit::mock('Foo');
* $spy = ShortifyPunit::spy('Foo');
* $partialMock = ShortifyPunit::partialMock('Foo');
*
* $mock->bar(); // returns NULL
* echo $spy->bar(); // prints 'bar'
* echo $partialMock->bar(); // prints 'bar'
*
* ShortifyPunit::when($spy)->bar()->returns('foo'); // stubbing spy
* echo $spy->bar(); // prints 'foo'
* ShortifyPunit::when($partialMock)->bar()->returns('foo'); // stubbing partial mock
* echo $partialMock->bar(); // prints 'foo'
*
* @param $mockedClass
* @return mixed
*/
public static function spy($mockedClass)
public static function partialMock($mockedClass)
{
$reflection = self::getMockReflection($mockedClass);

Expand All @@ -168,6 +168,14 @@ public static function spy($mockedClass)
);
}

/**
* @deprecated use partialMock($mockedClass) instead
*/
public static function spy($mockedClass)
{
return self::partialMock($mockedClass);
}

/**
* Setting up a when case
*
Expand Down
10 changes: 5 additions & 5 deletions tests/ShortifyPunitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,10 @@ public function testMultipleMocking()
*/
public function testMultipleClassMocking()
{
$mockSimple1 = ShortifyPunit::spy('SimpleClassForMocking');
$mockSimple2 = ShortifyPunit::spy('SimpleClassForMocking');
$mockFoo1 = ShortifyPunit::spy('Foo');
$mockFoo2 = ShortifyPunit::spy('Foo');
$mockSimple1 = ShortifyPunit::partialMock('SimpleClassForMocking');
$mockSimple2 = ShortifyPunit::partialMock('SimpleClassForMocking');
$mockFoo1 = ShortifyPunit::partialMock('Foo');
$mockFoo2 = ShortifyPunit::partialMock('Foo');

ShortifyPunit::when($mockSimple1)->first_method(1)->returns(1);
ShortifyPunit::when($mockSimple2)->first_method(1)->returns(2);
Expand All @@ -366,7 +366,7 @@ public function testMultipleClassMocking()
*/
public function testSpies()
{
$spy = ShortifyPunit::spy('SimpleClassForMocking');
$spy = ShortifyPunit::partialMock('SimpleClassForMocking');

$this->assertEquals($spy->first_method(), 1); // default value
$this->assertEquals($spy->second_method(), 2); // default value
Expand Down

0 comments on commit 472953d

Please sign in to comment.