dev-master
9999999-devMocking native PHP requests (file_get_contents(), ...)
MIT
The Requires
- php ~7
The Development Requires
test mock http unit
Wallogit.com
2017 © Pedro PelĂĄez
Mocking native PHP requests (file_get_contents(), ...)
, (*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
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)
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();
_ "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)
PHP >= 7MIT. See the LICENSE file, (*18)
Mocking native PHP requests (file_get_contents(), ...)
MIT
test mock http unit