2017 © Pedro PelĂĄez
 

library protomock

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

image

protomock/protomock

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

  • Tuesday, March 27, 2018
  • by Halleck
  • Repository
  • 1 Watchers
  • 14 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

ProtoMock

License Build Status, (*1)

Allow to mock your requests., (*2)

$protomock = new ProtoMock();
$protomock->enable('file');
$protomock->enable('http');

$protomock
    ->with('http://my-website.fr/hello')
    ->will('I am a mocked response');

// ...

echo file_get_contents('http://my-website.fr/hello');
// I am a mocked response

Why ?

Because legacy code exists, and generally needs unit tests..., (*3)

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

Usage

Installation, (*5)

composer require protomock/protomock

Enabling / disabling mocking for given protocol, (*6)

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

// disabling
$protomock->disable('http');

Mocking a resource, (*7)

$protomock
    ->with(<path>)
    ->will('wanted response');

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

Mocking a resource by regex, (*8)

$protomock
    ->matching(<regex>)
    ->will('wanted response');

// example
$protomock
    ->matching('!.*\.txt!')
    ->will('wanted response');

Mocking a resource by path (case insensitive), (*9)

$protomock
    ->with($path, Mock::MATCHING_EXACT_CASE_INSENSITIVE)
    ->will('wanted response');

Using a function for response, (*10)

// you can use any callable

$protomock
    ->with('/my/file1.txt')
    ->will(function($path) {
        return 'I a a mock. Current path is ' . $path;
    });

Expecting a failure as response, (*11)

// will trigger a WARNING
$protomock
    ->with('/my/file1.txt')
    ->willFail();

Expecting a failure as response (due to DNS resolution), (*12)

// will trigger a WARNING and wait for default_socket_timeout delay
$protomock
    ->with('/my/file1.txt')
    ->willFail();

Cancelling all, (*13)

$protomock->reset();

FAQ

_ "That's look magic ! This project must be so complex !", (*14)

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

_ "Can I use it for my unit tests?", (*16)

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

Requirements

  • PHP >= 7

License

MIT. See the LICENSE file, (*18)

The Versions

27/03 2018

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

  • php ~7

 

The Development Requires

test mock http unit