1. Go to this page and download the library: Download bovigo/callmap library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bovigo / callmap example snippets
// set up the instance to be used
$yourClass = NewInstance::of(YourClass::class, ['some', 'arguments'])
->returns([
'aMethod' => 313,
'otherMethod' => function() { return 'yeah'; },
'play' => onConsecutiveCalls(303, 808, 909, throws(new \Exception('error')),
'ups' => throws(new \Exception('error')),
'hey' => 'strtoupper'
]);
// do some stuff, e.g. execute the logic to test
...
// verify method invocations and received arguments
verify($yourClass, 'aMethod')->wasCalledOnce();
verify($yourClass, 'hey')->received('foo');
use bovigo\callmap\NewInstance;
use bovigo\callmap\NewCallable;
use function bovigo\callmap\throws;
use function bovigo\callmap\onConsecutiveCalls;
use function bovigo\callmap\verify;
verify($yourClass, 'aMethod')->receivedNothing(); // received nothing on first invocation
verify($yourClass, 'aMethod')->receivedNothing(3); // received nothing on third invocation
class Socket
{
public function connect(string $host, int $port, float $timeout)
{
$errno = 0;
$errstr = '';
$resource = fsockopen($host, $port, $errno, $errstr, $timeout);
if (false === $resource) {
throw new ConnectionFailure(
'Connect to ' . $host . ':'. $port
. ' within ' . $timeout . ' seconds failed: '
. $errstr . ' (' . $errno . ').'
);
}
// continue working with $resource
}
// other methods here
}
class Socket
{
private $fsockopen = 'fsockopen';
public function openWith(callable $fsockopen)
{
$this->fsockopen = $fsockopen;
}
public function connect(string $host, int $port, float $timeout)
{
$errno = 0;
$errstr = '';
$fsockopen = $this->fsockopen;
$resource = $fsockopen($host, $port, $errno, $errstr, $timeout);
if (false === $resource) {
throw new ConnectionFailure(
'Connect to ' . $host . ':'. $port
. ' within ' . $timeout . ' seconds failed: '
. $errstr . ' (' . $errno . ').'
);
}
// continue working with $resource
}
// other methods here
}
class SocketTest extends \PHPUnit\Framework\TestCase
{
/**
* @expectedException ConnectionFailure
*/
public function testSocketFailure()
{
$socket = new Socket();
$socket->openWith(NewCallable::of('fsockopen')->returns(false));
$socket->connect('example.org', 80, 1.0);
}
}
$strlen = NewCallable::of('strlen');
// int(5), as original function will be called because no mapped return value defined
var_dump($strlen('hello'));
$strlen = NewCallable::stub('strlen');
// NULL, as no return value defined and original function not called
var_dump($strlen('hello'));