Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/halleck45/protomock
Allows to mock native requests in PHP
https://github.com/halleck45/protomock
Last synced: about 2 months ago
JSON representation
Allows to mock native requests in PHP
- Host: GitHub
- URL: https://github.com/halleck45/protomock
- Owner: Halleck45
- License: mit
- Created: 2018-03-27T18:52:17.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-11-04T13:41:54.000Z (about 1 year ago)
- Last Synced: 2024-10-31T17:51:56.973Z (about 2 months ago)
- Language: PHP
- Size: 594 KB
- Stars: 20
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
ProtoMock
=====![License](https://poser.pugx.org/protomock/protomock/license.svg)
![Build Status](https://github.com/Halleck45/ProtoMock/actions/workflows/ci.yml/badge.svg)Allow to mock your requests.
```php
$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...
(and because my train is late (yes, I'm french), and I have one hour to kill...)
Usage
-----**Installation**
```bash
composer require protomock/protomock
```**Enabling / disabling mocking for given protocol**
```php
$protomock->enable('http'); // will capture all http://... requests// disabling
$protomock->disable('http');
```
**Mocking a resource**```php
$protomock
->with()
->will('wanted response');// disabling
$mocked = $protomock->with()->will('wanted response');
$protomock->without($mocked)
```**Mocking a resource by regex**
```php
$protomock
->matching()
->will('wanted response');// example
$protomock
->matching('!.*\.txt!')
->will('wanted response');
```**Mocking a resource by path (case insensitive)**
```php
$protomock
->with($path, Mock::MATCHING_EXACT_CASE_INSENSITIVE)
->will('wanted response');
```**Using a function for response**
```php
// 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**
```php
// will trigger a WARNING
$protomock
->with('/my/file1.txt')
->willFail();
```**Expecting a failure as response (due to DNS resolution)**
```php
// will trigger a WARNING and wait for default_socket_timeout delay
$protomock
->with('/my/file1.txt')
->willFail();
```**Cancelling all**
```php
$protomock->reset();
```FAQ
-----_ *"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.
Requirements
------ `PHP >= 7`
License
-----MIT. See the LICENSE file