Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #283 from davedevelopment/spies
Basic/naive spy implementation
- Loading branch information
Showing
10 changed files
with
360 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,25 @@ | |||
<?php | |||
|
|||
namespace Mockery; | |||
|
|||
class MethodCall | |||
{ | |||
private $method; | |||
private $args; | |||
|
|||
public function __construct($method, $args) | |||
{ | |||
$this->method = $method; | |||
$this->args = $args; | |||
} | |||
|
|||
public function getMethod() | |||
{ | |||
return $this->method; | |||
} | |||
|
|||
public function getArgs() | |||
{ | |||
return $this->args; | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,30 @@ | |||
<?php | |||
|
|||
namespace Mockery; | |||
|
|||
class ReceivedMethodCalls | |||
{ | |||
private $methodCalls = array(); | |||
|
|||
public function push(MethodCall $methodCall) | |||
{ | |||
$this->methodCalls[] = $methodCall; | |||
} | |||
|
|||
public function verify(Expectation $expectation) | |||
{ | |||
foreach ($this->methodCalls as $methodCall) { | |||
if ($methodCall->getMethod() !== $expectation->getName()) { | |||
continue; | |||
} | |||
|
|||
if (!$expectation->matchArgs($methodCall->getArgs())) { | |||
continue; | |||
} | |||
|
|||
$expectation->verifyCall($methodCall->getArgs()); | |||
} | |||
|
|||
$expectation->verify(); | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,89 @@ | |||
<?php | |||
|
|||
namespace Mockery; | |||
|
|||
class VerificationDirector | |||
{ | |||
private $receivedMethodCalls; | |||
private $expectation; | |||
|
|||
public function __construct(ReceivedMethodCalls $receivedMethodCalls, VerificationExpectation $expectation) | |||
{ | |||
$this->receivedMethodCalls = $receivedMethodCalls; | |||
$this->expectation = $expectation; | |||
} | |||
|
|||
public function verify() | |||
{ | |||
return $this->receivedMethodCalls->verify($this->expectation); | |||
} | |||
|
|||
public function with() | |||
{ | |||
return $this->cloneApplyAndVerify("with", func_get_args()); | |||
} | |||
|
|||
public function withArgs(array $args) | |||
{ | |||
return $this->cloneApplyAndVerify("withArgs", array($args)); | |||
} | |||
|
|||
public function withNoArgs() | |||
{ | |||
return $this->cloneApplyAndVerify("withNoArgs", array()); | |||
} | |||
|
|||
public function withAnyArgs() | |||
{ | |||
return $this->cloneApplyAndVerify("withAnyArgs", array()); | |||
} | |||
|
|||
public function times($limit = null) | |||
{ | |||
return $this->cloneWithoutCountValidatorsApplyAndVerify("times", array($limit)); | |||
} | |||
|
|||
public function once() | |||
{ | |||
return $this->cloneWithoutCountValidatorsApplyAndVerify("once", array()); | |||
} | |||
|
|||
public function twice() | |||
{ | |||
return $this->cloneWithoutCountValidatorsApplyAndVerify("twice", array()); | |||
} | |||
|
|||
public function atLeast() | |||
{ | |||
return $this->cloneWithoutCountValidatorsApplyAndVerify("atLeast", array()); | |||
} | |||
|
|||
public function atMost() | |||
{ | |||
return $this->cloneWithoutCountValidatorsApplyAndVerify("atMost", array()); | |||
} | |||
|
|||
public function between($minimum, $maximum) | |||
{ | |||
return $this->cloneWithoutCountValidatorsApplyAndVerify("between", array($minimum, $maximum)); | |||
} | |||
|
|||
protected function cloneWithoutCountValidatorsApplyAndVerify($method, $args) | |||
{ | |||
$expectation = clone $this->expectation; | |||
$expectation->clearCountValidators(); | |||
call_user_func_array(array($expectation, $method), $args); | |||
$director = new VerificationDirector($this->receivedMethodCalls, $expectation); | |||
$director->verify(); | |||
return $director; | |||
} | |||
|
|||
protected function cloneApplyAndVerify($method, $args) | |||
{ | |||
$expectation = clone $this->expectation; | |||
call_user_func_array(array($expectation, $method), $args); | |||
$director = new VerificationDirector($this->receivedMethodCalls, $expectation); | |||
$director->verify(); | |||
return $director; | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,17 @@ | |||
<?php | |||
|
|||
namespace Mockery; | |||
|
|||
class VerificationExpectation extends Expectation | |||
{ | |||
public function clearCountValidators() | |||
{ | |||
$this->_countValidators = array(); | |||
} | |||
|
|||
public function __clone() | |||
{ | |||
parent::__clone(); | |||
$this->_actualCount = 0; | |||
} | |||
} |
Oops, something went wrong.