1. Go to this page and download the library: Download chubbyphp/chubbyphp-mock 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/ */
chubbyphp / chubbyphp-mock example snippets
declare(strict_types=1);
namespace MyProject\Tests\Unit\RequestHandler;
use Chubbyphp\Mock\MockMethod\WithCallback;
use Chubbyphp\Mock\MockMethod\WithReturn;
use Chubbyphp\Mock\MockMethod\WithReturnSelf;
use Chubbyphp\Mock\MockObjectBuilder;
use MyProject\RequestHandler\PingRequestHandler;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
final class PingRequestHandlerTest extends TestCase
{
public function testHandle(): void
{
$builder = new MockObjectBuilder();
// no calls expected: any method call on this mock fails the test
$request = $builder->create(ServerRequestInterface::class, []);
$responseBody = $builder->create(StreamInterface::class, [
// the written JSON is not known in advance, so assert on it in a callback
new WithCallback('write', static function (string $string): int {
$data = json_decode($string, true);
self::assertArrayHasKey('datetime', $data);
return \strlen($string);
}),
]);
$response = $builder->create(ResponseInterface::class, [
// calls must happen in exactly this order
new WithReturnSelf('withHeader', ['Content-Type', 'application/json']),
new WithReturnSelf('withHeader', ['Cache-Control', 'no-cache, no-store, must-revalidate']),
new WithReturnSelf('withHeader', ['Pragma', 'no-cache']),
new WithReturnSelf('withHeader', ['Expires', '0']),
new WithReturn('getBody', [], $responseBody),
]);
$responseFactory = $builder->create(ResponseFactoryInterface::class, [
new WithReturn('createResponse', [200, ''], $response),
]);
$requestHandler = new PingRequestHandler($responseFactory);
self::assertSame($response, $requestHandler->handle($request));
}
}
declare(strict_types=1);
use Chubbyphp\Mock\MockMethod\WithCallback;
use Chubbyphp\Mock\MockMethod\WithReturn;
use Chubbyphp\Mock\MockMethod\WithReturnSelf;
use Chubbyphp\Mock\MockObjectBuilder;
use MyProject\RequestHandler\PingRequestHandler;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
it('handles a ping request', function (): void {
$builder = new MockObjectBuilder();
$request = $builder->create(ServerRequestInterface::class, []);
$responseBody = $builder->create(StreamInterface::class, [
new WithCallback('write', static function (string $string): int {
expect(json_decode($string, true))->toHaveKey('datetime');
return \strlen($string);
}),
]);
$response = $builder->create(ResponseInterface::class, [
new WithReturnSelf('withHeader', ['Content-Type', 'application/json']),
new WithReturnSelf('withHeader', ['Cache-Control', 'no-cache, no-store, must-revalidate']),
new WithReturnSelf('withHeader', ['Pragma', 'no-cache']),
new WithReturnSelf('withHeader', ['Expires', '0']),
new WithReturn('getBody', [], $responseBody),
]);
$responseFactory = $builder->create(ResponseFactoryInterface::class, [
new WithReturn('createResponse', [200, ''], $response),
]);
$requestHandler = new PingRequestHandler($responseFactory);
expect($requestHandler->handle($request))->toBe($response);
});
declare(strict_types=1);
namespace MyProject\Tests\Unit\RequestHandler;
use Chubbyphp\Mock\MockMethod\WithCallback;
use Chubbyphp\Mock\MockMethod\WithReturn;
use Chubbyphp\Mock\MockMethod\WithReturnSelf;
use Chubbyphp\Mock\MockObjectBuilder;
use Codeception\Test\Unit;
use MyProject\RequestHandler\PingRequestHandler;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
final class PingRequestHandlerTest extends Unit
{
public function testHandle(): void
{
$builder = new MockObjectBuilder();
$request = $builder->create(ServerRequestInterface::class, []);
$responseBody = $builder->create(StreamInterface::class, [
new WithCallback('write', function (string $string): int {
$data = json_decode($string, true);
$this->assertArrayHasKey('datetime', $data);
return \strlen($string);
}),
]);
$response = $builder->create(ResponseInterface::class, [
new WithReturnSelf('withHeader', ['Content-Type', 'application/json']),
new WithReturnSelf('withHeader', ['Cache-Control', 'no-cache, no-store, must-revalidate']),
new WithReturnSelf('withHeader', ['Pragma', 'no-cache']),
new WithReturnSelf('withHeader', ['Expires', '0']),
new WithReturn('getBody', [], $responseBody),
]);
$responseFactory = $builder->create(ResponseFactoryInterface::class, [
new WithReturn('createResponse', [200, ''], $response),
]);
$requestHandler = new PingRequestHandler($responseFactory);
$this->assertSame($response, $requestHandler->handle($request));
}
}
declare(strict_types=1);
namespace spec\MyProject\RequestHandler;
use Chubbyphp\Mock\MockMethod\WithCallback;
use Chubbyphp\Mock\MockMethod\WithReturn;
use Chubbyphp\Mock\MockMethod\WithReturnSelf;
use Chubbyphp\Mock\MockObjectBuilder;
use MyProject\RequestHandler\PingRequestHandler;
use PhpSpec\ObjectBehavior;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
final class PingRequestHandlerSpec extends ObjectBehavior
{
public function it_handles_a_ping_request(): void
{
$builder = new MockObjectBuilder();
$request = $builder->create(ServerRequestInterface::class, []);
$responseBody = $builder->create(StreamInterface::class, [
// phpspec has no standalone assertion API, so fail with an exception
new WithCallback('write', static function (string $string): int {
$data = json_decode($string, true);
if (!\array_key_exists('datetime', $data)) {
throw new \RuntimeException('Missing key "datetime" in written JSON');
}
return \strlen($string);
}),
]);
$response = $builder->create(ResponseInterface::class, [
new WithReturnSelf('withHeader', ['Content-Type', 'application/json']),
new WithReturnSelf('withHeader', ['Cache-Control', 'no-cache, no-store, must-revalidate']),
new WithReturnSelf('withHeader', ['Pragma', 'no-cache']),
new WithReturnSelf('withHeader', ['Expires', '0']),
new WithReturn('getBody', [], $responseBody),
]);
$responseFactory = $builder->create(ResponseFactoryInterface::class, [
new WithReturn('createResponse', [200, ''], $response),
]);
$this->beConstructedWith($responseFactory);
$this->handle($request)->shouldReturn($response);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.