2021-05-15 21:42:34 +03:00
![Build Status ](https://img.shields.io/github/workflow/status/Neur0toxine/pock/Tests?style=flat-square )
[![Coverage ](https://img.shields.io/codecov/c/gh/Neur0toxine/pock/master.svg?logo=codecov&logoColor=white&style=flat-square )](https://codecov.io/gh/Neur0toxine/pock)
[![Latest stable ](https://img.shields.io/packagist/v/neur0toxine/pock.svg?style=flat-square )](https://packagist.org/packages/neur0toxine/pock)
[![PHP from Packagist ](https://img.shields.io/packagist/php-v/neur0toxine/pock.svg?logo=php&logoColor=white&style=flat-square )](https://packagist.org/packages/neur0toxine/pock)
![License ](https://img.shields.io/github/license/Neur0toxine/pock?style=flat-square )
2021-05-15 13:14:21 +03:00
2021-05-13 21:08:55 +03:00
# pock
2021-05-15 19:41:56 +03:00
Easy to use HTTP mocking solution, compatible with PSR-18 and HTTPlug.
2021-05-13 21:08:55 +03:00
2021-05-15 19:41:56 +03:00
Project is still in its early development stage. API can change over time, but I'll try to not introduce breaking changes.
2021-05-15 21:46:21 +03:00
You can find autogenerated documentation [here ](https://neur0toxine.github.io/pock/ ) or look at the examples. API for the mock building can be found
[here ](https://neur0toxine.github.io/pock/classes/Pock-PockBuilder.html ) and API for the response building (returned from `PockBuilder::reply` call)
can be found [here ](https://neur0toxine.github.io/pock/classes/Pock-PockResponseBuilder.html ).
2021-05-15 19:00:42 +03:00
2021-05-15 19:41:56 +03:00
# Examples
Mock JSON API route with Basic authorization, reply with JSON.
```php
use Pock\Enum\RequestMethod;
use Pock\Enum\RequestScheme;
use Pock\PockBuilder;
$builder = new PockBuilder();
$builder->matchMethod(RequestMethod::GET)
->matchScheme(RequestScheme::HTTPS)
->matchHost('example.com')
->matchPath('/api/v1/users')
->matchHeaders([
'Content-Type' => 'application/json',
'Authorization' => 'Basic YWxhZGRpbjpvcGVuc2VzYW1l'
])
->reply(200)
->withHeader('Content-Type', 'application/json')
->withJson([
[
'name' => 'John Doe',
'username' => 'john',
'email' => 'john@example.com'
],
[
'name' => 'Jane Doe',
'username' => 'jane',
'email' => 'jane@example.com'
],
]);
// Pass PSR-18 compatible client to the API client.
$client = new MysteriousApiClient($builder->getClient());
$client->setCredentials('username', 'password');
// Receive mock response.
$response = $client->getUsers();
```
Same mock, but with models! Also, the code itself is slightly shorter.
```php
use Pock\Enum\RequestMethod;
use Pock\PockBuilder;
$builder = new PockBuilder();
$builder->matchMethod(RequestMethod::GET)
->matchUri('https://example.com/api/v1/users')
->matchHeaders([
'Content-Type' => 'application/json',
'Authorization' => 'Basic YWxhZGRpbjpvcGVuc2VzYW1l'
])
->reply(200)
->withHeader('Content-Type', 'application/json')
->withJson([
// We're assuming here that MysteriousUser's constructor can receive an initial values.
new MysteriousUser('John Doe', 'john', 'john@example.com'),
new MysteriousUser('Jane Doe', 'jane', 'jane@example.com'),
]);
// Pass PSR-18 compatible client to the API client.
$client = new MysteriousApiClient($builder->getClient());
$client->setCredentials('username', 'password');
// Receive mock response.
$response = $client->getUsers();
```
It is possible to mock a response using DTO's because pock can use third-party serializers under the hood.
# Serializer support
pock supports JMS serializer and Symfony serializer out of the box. Available serializer will be instantiated automatically.
It will be used to serialize requests and responses in mocks which means you actually can pass an entire DTO
into the corresponding methods (for example, `matchJsonBody` as an assertion or `withJsonBody` to generate a response body).
2021-05-15 20:28:41 +03:00
By default, JMS serializer has more priority than the Symfony serializer. You can use methods below before running tests (`bootstrap.php`)
2021-05-15 19:41:56 +03:00
if you want to override default behavior.
```php
use Pock\Factory\JsonSerializerFactory;
use Pock\Factory\XmlSerializerFactory;
use Pock\Serializer\SymfonySerializerDecorator;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
$encoders = [new XmlEncoder(), new JsonEncoder()];
$normalizers = [new ObjectNormalizer()];
$serializer = new SymfonySerializerDecorator(new Serializer($normalizers, $encoders));
JsonSerializerFactory::setSerializer($serializer);
XmlSerializerFactory::setSerializer($serializer);
```
In order to use unsupported serializer you should create a decorator which implements `Pock\Serializer\SerializerInterface` .
2021-05-20 19:30:31 +03:00
# Roadmap to stable
2021-05-21 20:39:37 +03:00
- [x] `at(N)` - execute mock only at Nth call.
2021-05-20 20:56:45 +03:00
- [x] `always()` - always execute this mock (removes mock expiration).
- [x] Separate `UniversalMockException` into several exceptions (`PockClientException`, `PockNetworkException` , etc).
- [x] Add methods for easier throwing of exceptions listed in previous entry.
2021-05-21 09:38:15 +03:00
- [x] `replyWithCallback` - reply using specified callback.
- [x] `replyWithFactory` - reply using specified response factory (provide corresponding interface).
2021-05-21 19:54:17 +03:00
- [x] Compare XML bodies using `DOMDocument` , fallback to text comparison in case of problems.
2021-09-23 19:41:02 +03:00
- [x] Regexp matchers for body, query, URI and path.
- [x] Form Data body matcher (partial & exact)
- [x] Multipart form body matcher (just like callback matcher but parses the body as a multipart form data)
2021-05-21 20:39:37 +03:00
- [ ] `symfony/http-client` support.
2021-09-16 15:36:17 +03:00
- [ ] Real network response for mocked & unmatched requests.
2021-05-20 19:30:31 +03:00
- [ ] Document everything (with examples if it’ s feasible).