PHP code example of webwhales / laravel-guzzle-conditional-mock-handler

1. Go to this page and download the library: Download webwhales/laravel-guzzle-conditional-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/ */

    

webwhales / laravel-guzzle-conditional-mock-handler example snippets


use Illuminate\Foundation\Testing\TestCase;
use WebWhales\LaravelGuzzleConditionalMockHandler\TestWithGuzzleMockHandler;

class ASimpleTest extends TestCase
{
    use TestWithGuzzleMockHandler;

    public function testSomething()
    {
        /**
         * @var \GuzzleHttp\Client $client
         */
        $client = $this->app->make(Client::class);

        // Add a mocked response
        $this->loadMockedResponseData('https://example.com', 'This is a test');

        // Get the response
        $response = $client->get('https://example.com');

		// Test the response content
        $this->assertEquals('This is a test', $response->getBody()->getContents());
    }

// Add a mocked response
$this->loadMockedResponse('https://example.com', new Response(400, ['X-Test' => 'Test'], 'This is a test'));

// Get the response
$response = $client->get('https://example.com');

// Test the response content
$this->assertEquals(400, $response->getStatusCode());
$this->assertEquals('This is a test', $response->getBody()->getContents());

// Add mocked responses
$this->loadMockedResponseData('^http(s)?://example\.', 'This is a test');

// Make a request to a matching URL
$response = $client->request('GET', 'https://example.com');

// Test the response content
$this->assertEquals('This is a test', $response->getBody()->getContents());


// Make a request to a non matching URL
// This will retrieve the actual content of https://www.example.com
$response = $client->request('GET', 'https://www.example.com');

// Test the response content
$this->assertNotEquals('This is a test', $response->getBody()->getContents());