Mocking native PHP requests (file_get_contents(), ...)

Fund package maintenance!

v1.0.3 2023-10-24 08:43 UTC

This package is auto-updated.

Last update: 2024-02-04 14:11:44 UTC


License Build Status

Allow to mock your requests.

$protomock = new ProtoMock();

    ->will('I am a mocked response');

// ...

echo file_get_contents('');
// I am a mocked response

Why ?

Because legacy code exists, and generally needs unit tests...

(and because my train is late (yes, I'm french), and I have one hour to kill...)



composer require protomock/protomock

Enabling / disabling mocking for given protocol

$protomock->enable('http'); // will capture all http://... requests

// disabling

Mocking a resource

    ->will('wanted response');

// disabling
$mocked = $protomock->with(<path>)->will('wanted response');

Mocking a resource by regex

    ->will('wanted response');

// example
    ->will('wanted response');

Mocking a resource by path (case insensitive)

    ->will('wanted response');

Using a function for response

// you can use any callable

    ->will(function($path) {
        return 'I a a mock. Current path is ' . $path;

Expecting a failure as response

// will trigger a WARNING

Expecting a failure as response (due to DNS resolution)

// will trigger a WARNING and wait for default_socket_timeout delay

Cancelling all



_ "That's look magic ! This project must be so complex !"

nope. It needs only 200 lines of code, including comments... I just use the stream_wrapper_register PHP function.

_ "Can I use it for my unit tests?"

Yes. ProtoMock is used by several companies for the Continuous Integration (CI) of their projects.


  • PHP >= 7


MIT. See the LICENSE file