PHP code example of jshayes / fake-requests

1. Go to this page and download the library: Download jshayes/fake-requests 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/ */

    

jshayes / fake-requests example snippets


use JSHayes\FakeRequests\MockHandler;
use JSHayes\FakeRequests\ClientFactory;

public function test()
{
    $factory = new ClientFactory();
    $factory->setHandler($mockHandler = new MockHandler());

    $mockHandler->get('/get-request');
    $mockHandler->post('/post-request');

    $factory->make()->get('/get-request');
    $factory->make()->post('/post-request');
}

use JSHayes\FakeRequests\MockHandler;
use JSHayes\FakeRequests\ClientFactory;

public function test()
{
    $factory = new ClientFactory();
    $factory->setHandler($mockHandler = new MockHandler());

    $mockHandler->expects('get', '/get-request');
    $mockHandler->expects('post', '/post-request');

    $factory->make()->get('/get-request');
    $factory->make()->post('/post-request');
}

use JSHayes\FakeRequests\MockHandler;
use JSHayes\FakeRequests\ClientFactory;

public function test()
{
    $factory = new ClientFactory();
    $factory->setHandler($mockHandler = new MockHandler());

    $mockHandler->expects('get', 'https://test.dev/get-request');

    $factory->make()->get('https://test.dev/get-request');
}

$mockHandler->get('/get-request')->inspectRequest(function (RequestInterface $request, array $options) {
    // Make assertions on the request or options here
});

$expectation = $mockHandler->get('/get-request');
$factory->make()->get('/get-request');
$request = $expectation->getRequest();
$request->assertBodyEquals('');

use JSHayes\FakeRequests\Request;

class ExtendedRequest extends Request
{
    /**
     * This is an example method in an extended request. These extended requests
     * can be used to add assertion helpers to make testing request flows easier
     *
     * @return void
     */
    public function assertExample(): void
    {
        $this->assertTrue(true);
    }
}

// Register the extended request with the mock handler
$mockHandler->extendRequest(ExtendedRequest::class);

$expectation = $mockHandler->get('/get-request');
$factory->make()->get('/get-request');

// Getting the request from the expectation will return the extended request
$request = $expectation->getRequest();
$request->assertExample();

$expectation1 = $mockHandler->get('/get-request1')->extendRequest(ExtendedRequest::class);
$expectation2 = $mockHandler->get('/get-request2');
$factory->make()->get('/get-request1');
$factory->make()->get('/get-request2');

// Getting the request from the expectation will return the extended request
$request = $expectation1->getRequest();
$request->assertExample();

// Getting the request for the second expectation will not return the extended request, since
// we only extended the request on the first expectation. So, the assertExample method will
// not be available here.
$request->expectation2->getRequest();

$mockHandler->get('/test')->respondWith(200, 'body', ['header' => 'value]);

$mockHandler->get('/test')->respondWith(new Response(200, ['header' => 'value'], 'body'));

$mockHandler->get('/test')->respondWith(function (ResponseBuilder $builder) {
    $builder->status(200);
    $builder->body('body');
    $builder->headers(['header' => 'values']);
});

$mockHandler->get('/test')->when(function (RequestInterface $request, array $options) {
    return true;
});

$mockHandler->allowUnexpectedCalls();
$client->get('/test');

use JSHayes\FakeRequests\Traits\Laravel\FakeRequests;

class SomeTest extends TestCase
{
    use FakeRequests;

    /**
     * @test
     */
    public function some_test()
    {
        $handler = $this->fakeRequests();
        // Add expectations to the handler
    }
}