Allow to mock your requests.
$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
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...)
Installation
composer require protomock/protomock
Enabling / disabling mocking for given protocol
$protomock->enable('http'); // will capture all http://... requests
// disabling
$protomock->disable('http');
Mocking a resource
$protomock
->with(<path>)
->will('wanted response');
// disabling
$mocked = $protomock->with(<path>)->will('wanted response');
$protomock->without($mocked)
Mocking a resource by regex
$protomock
->matching(<regex>)
->will('wanted response');
// example
$protomock
->matching('!.*\.txt!')
->will('wanted response');
Mocking a resource by path (case insensitive)
$protomock
->with($path, Mock::MATCHING_EXACT_CASE_INSENSITIVE)
->will('wanted response');
Using a function for response
// 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
// will trigger a WARNING
$protomock
->with('/my/file1.txt')
->willFail();
Expecting a failure as response (due to DNS resolution)
// will trigger a WARNING and wait for default_socket_timeout delay
$protomock
->with('/my/file1.txt')
->willFail();
Cancelling all
$protomock->reset();
_ "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