PHP code example of tijmenwierenga / guzzle-mock-handler

1. Go to this page and download the library: Download tijmenwierenga/guzzle-mock-handler 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/ */

    

tijmenwierenga / guzzle-mock-handler example snippets




use GuzzleHttp\Client;
use TijmenWierenga\Guzzle\Mocking\MockHandler;

$mockHandler = new MockHandler();
$handlerStack = GuzzleHttp\HandlerStack::create($mockHandler);
$client = new Client(['handler' => $handlerStack]);



use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;
use TijmenWierenga\Guzzle\Mocking\MockHandler;

/** @var MockHandler $mockHandler */
$mockHandler
    ->when(fn (RequestInterface $request): bool => $request->getMethod() === 'GET')
    ->respondWith(new Response(200));



use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;
use TijmenWierenga\Guzzle\Mocking\MockHandler;

/** @var MockHandler $mockHandler */
$mockHandler
    ->when(fn (RequestInterface $request): bool => $request->getMethod() === 'GET')
    ->withMaxInvocations(1)
    ->respondWith(new Response(200));

use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;
use TijmenWierenga\Guzzle\Mocking\Conditions\All;
use TijmenWierenga\Guzzle\Mocking\MockHandler;

/** @var MockHandler $mockHandler */
$mockHandler
    ->when(
        new All(
            fn (RequestInterface $request): bool => $request->getMethod() === 'GET',
            fn (RequestInterface $request): bool => $request->getUri()->getHost() === 'google.com'
        )
    )
    ->respondWith(new Response(200));

use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;
use TijmenWierenga\Guzzle\Mocking\Conditions\Any;
use TijmenWierenga\Guzzle\Mocking\MockHandler;

/** @var MockHandler $mockHandler */
$mockHandler
    ->when(
        new Any(
            fn (RequestInterface $request): bool => $request->getMethod() === 'GET',
            fn (RequestInterface $request): bool => $request->getMethod() === 'POST',
        )
    )
    ->respondWith(new Response(200));