Skip to content

Commit

Permalink
In case of stubbing twice, the secondary stub should reset the counter
Browse files Browse the repository at this point in the history
  • Loading branch information
danrevah committed Dec 24, 2015
1 parent 240788c commit 41854ff
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

## Installation

> PHP Version >= 5.4 is required!
> PHP Version >= 5.4 is required!
The following instructions outline installation using Composer. If you don't
have Composer, you can download it from [http://getcomposer.org/](http://getcomposer.org/)
Expand Down
2 changes: 1 addition & 1 deletion src/Stub/WhenChainCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private function addChainedMethodResponse($chainedMethodsBefore, $currentMethod,
$rResponse = &$rResponse[$chainedMethodName][$serializedChainedMethodArgs];
}

$rResponse[$currentMethodName][serialize(current($currentMethod))] = ['response' => ['action' => $action, 'value' => $lastValue]];
$rResponse[$currentMethodName][serialize(current($currentMethod))] = ['response' => ['action' => $action, 'value' => $lastValue, 'counter' => 0]];

ShortifyPunit::addChainedResponse(array(get_class($this->mockClass) => [$mockClassInstanceId => $response]));
}
Expand Down
41 changes: 41 additions & 0 deletions tests/Verify/VerifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,47 @@ public function testVerifyChainNoParameters()
$this->assertFalse(ShortifyPunit::verify($mock)->bar()->foo()->atLeast(2));
}

/**
* Secondary stub should reset call counter
*/
public function testVerifyResetCounterOnMock()
{
// First stub
$mock = ShortifyPunit::mock('Foo');
ShortifyPunit::when($mock)->bar()->foo()->returns(1);
$mock->bar()->foo();
$this->assertTrue(ShortifyPunit::verify($mock)->bar()->foo()->atLeast(1));

// Secondary stub should reset counter
ShortifyPunit::when($mock)->bar()->foo()->returns(1);
$this->assertTrue(ShortifyPunit::verify($mock)->bar()->foo()->calledTimes(0));
}

/**
* Testing secondary stub is not effecting child elements
*/
public function testSecondaryStubNotCausingIssues()
{
$mock = ShortifyPunit::mock('Foo');
ShortifyPunit::when($mock)->bar()->foo()->returns(1);
ShortifyPunit::when($mock)->bar(1)->foo()->returns(2);
ShortifyPunit::when($mock)->bar()->foo(2)->returns(3);

$this->assertEquals($mock->bar()->foo(), 1);
$this->assertEquals($mock->bar(1)->foo(), 2);
$this->assertEquals($mock->bar()->foo(2), 3);

$this->assertTrue(ShortifyPunit::verify($mock)->bar()->foo()->calledTimes(1));
$this->assertTrue(ShortifyPunit::verify($mock)->bar(1)->foo()->calledTimes(1));
$this->assertTrue(ShortifyPunit::verify($mock)->bar()->foo(2)->calledTimes(1));

ShortifyPunit::when($mock)->bar()->foo()->returns(1);

$this->assertTrue(ShortifyPunit::verify($mock)->bar()->foo()->calledTimes(0));
$this->assertTrue(ShortifyPunit::verify($mock)->bar(1)->foo()->calledTimes(1));
$this->assertTrue(ShortifyPunit::verify($mock)->bar()->foo(2)->calledTimes(1));
}

/**
* Basic single stubbing
*/
Expand Down

0 comments on commit 41854ff

Please sign in to comment.