PHP code example of kshabazz / interception

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

    

kshabazz / interception example snippets


use \Kshabazz\Interception\StreamWrappers\Http;

class HttpTest extends \PHPUnit_Framework_TestCase
{
    static public function setUpBeforeClass()
    {
        // Unregister the built-in PHP HTTP protocol stream wrapper.
        \stream_wrapper_unregister( 'http' );

        // Pick a place where we want to save request for playback.
        Http::setSaveDir( './fixtures/' );

        // Register the Interception stream wrapper for the HTTP protocol.
        \stream_register_wrapper(
            'http',
            '\\Kshabazz\\Interception\\StreamWrappers\\Http',
            \STREAM_IS_URL
        );
    }

    /**
     * Make sure we restore the original HTTP stream wrapper for the test environment.
     */
    static public function tearDownAfterClass()
    {
        \stream_wrapper_restore( 'http' );
    }

    /**
     * Example test case.
     */
    public function test_setSaveFile()
    {
        // You can also specify the filename for the local cache.
        Http::setSaveFilename( 'test-example' );

        // Will generate a file:  ./fixtures/test-example.rsd
        \file_get_contents( 'http://www.example.com' );

        $file = FIXTURES_PATH . DIRECTORY_SEPARATOR . $fileName . '.rsd';
        $this->assertTrue( \file_exists($file) );
        \unlink( $file );
    }
}


...
// Define a FIXTURES_PATH constant in your bootstrap file, set to whatever path you like.
$fixturesPath = \realpath( __DIR__ . DIRECTORY_SEPARATOR . 'fixtures' );
\define( 'FIXTURES_PATH', $fixturesPath );
...

/**
 * Now the HTTP request will be stored in the file "example-dot-com.rsd"
 *
 * @interception example-dot-com
 */
public function test_interception_annotation()
{
    $responseBody = file_get_content( 'http://www.example.com/', 'r' );
    $filename = FIXTURES_PATH . DIRECTORY_SEPARATOR . 'example-dot-com.rsd';
    $this->assertFileExists( \file_exists($filename) );
}

/**
* Now the HTTP request will be stored in the file "example-dot-com.rsd"
*
* @interceptionPersist example-dot-com
*/
public function test_interception_annotation()
{
    $responseBody = file_get_content( 'http://www.example.com/', 'r' );
    $responseBody = file_get_content( 'http://www.example.com/', 'r' );

    $filename1 = FIXTURES_PATH . DIRECTORY_SEPARATOR . 'example-dot-com-1.rsd';
    $filename2 = FIXTURES_PATH . DIRECTORY_SEPARATOR . 'example-dot-com-2.rsd';

    $this->assertFileExists( \file_exists($filename1) );
    $this->assertFileExists( \file_exists($filename2) );
}

use \GuzzleHttp\Client,
    \Kshabazz\Interception\StreamWrappers\Http,
    \Kshabazz\Interception\GuzzleHandler;

// Set the file to save the response to, in your unit test, you could set this with the "@interception" annotation also.
Http::setSaveFilename( 'google-dot-come' );

// Interception Guzzle compatible stream handler
$streamHandler = new GuzzleHandler();

// Have Guzzle use the Interception stream handler, so request can be intercepted.
$httpClient = new Client([
	'handler' => $streamHandler
]);

// Make the request.
$httpClient->get( 'http://www.google.com/ );