| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Track arguments and return values of calls to functions and methods when you cannot use PHPUnit Mocks. Spies do not interfere with the behavior of the code and delegate calls to the actual implementation of the spied on function by default. But, they can be configured to delegate calls to another function.
Add to your composer.json:
{
"require": {
"christopheraue/phpspy": "*"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/christopheraue/phpspy"
}
]
}function id($in)
{
return $in;
}
$spy = new \christopheraue\phpspy\Spy("id");
id(1);
id(2);
id(3);
echo $spy->getCallCount(); //3
echo $spy->getCall->getArgCount(); //1
echo $spy->getCall(1)->getResult(); //2
echo $spy->getCall(0)->getContext(); //nullclass VIP
{
private $_secret;
public function learnSecret($secret)
{
$this->_secret = $secret;
}
}
$vip = new VIP();
$spy = new \christopheraue\phpspy\Spy("VIP", "learnSecret");
$vip->learnSecret("The cake is a lie.");
echo $spy->getCallCount(); //1
echo $spy->getCall(0)->getArg(0); //"The cake is a lie."
echo $spy->getCall(0)->getContext(); //$vipclass VIP
{
public static function id($in)
{
return $in;
}
}
$spy = new \christopheraue\phpspy\Spy("VIP", "id");
VIP::id("static, static, static.");
echo $spy->getCallCount(); //1
echo $spy->getCall(0)->getResult(); //"static, static, static."
echo $spy->getCall(0)->getContext(); //"VIP"Calls can be intercepted by giving the spy a substitute to execute instead. They are still tracked in this case. It works for functions and methods alike.
function id($in)
{
return $in;
}
$spy = new \christopheraue\phpspy\Spy("id");
//substitute for an anonymous function
$spy->actAs(function($in) {
//do something
});
//substitute for another function
$spy->actAs('functionName');
//substitute for a method
$spy->actAs(array($object, 'method'));
//call through again
$spy->actNaturally();All callables qualify as valid substitute. Anonymous functions are executed in the context of the methods they are replacing. This means
To spy on
| Back | FazBrowse Home | New Git URL |