Skip to content

Commit 795ca69

Browse files
committed
version 1.0.0
0 parents  commit 795ca69

22 files changed

+1426
-0
lines changed

.github/CODE_OF_CONDUCT.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6+
7+
## Our Standards
8+
9+
Examples of behavior that contributes to creating a positive environment include:
10+
11+
* Using welcoming and inclusive language
12+
* Being respectful of differing viewpoints and experiences
13+
* Gracefully accepting constructive criticism
14+
* Focusing on what is best for the community
15+
* Showing empathy towards other community members
16+
17+
Examples of unacceptable behavior by participants include:
18+
19+
* The use of sexualized language or imagery and unwelcome sexual attention or advances
20+
* Trolling, insulting/derogatory comments, and personal or political attacks
21+
* Public or private harassment
22+
* Publishing others' private information, such as a physical or electronic address, without explicit permission
23+
* Other conduct which could reasonably be considered inappropriate in a professional setting
24+
25+
## Our Responsibilities
26+
27+
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28+
29+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30+
31+
## Scope
32+
33+
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34+
35+
## Enforcement
36+
37+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at [email protected]. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38+
39+
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40+
41+
## Attribution
42+
43+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44+
45+
[homepage]: http://contributor-covenant.org
46+
[version]: http://contributor-covenant.org/version/1/4/

.github/CONTRIBUTING.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Contributing
2+
3+
## Report an issue
4+
5+
Please follow the guidelines below when creating an issue so that your issue can be more promptly resolved:
6+
* Provide information including: the version of PHP and HTTP Client Mock, the type of operating system and Web server;
7+
* Provide the complete error call stack if available;
8+
* Describe the steps for reproducing the issue. It would be even better if you could provide code to reproduce the issue;
9+
10+
Do not report an issue if you are asking how to use some HTTP Client Mock feature. You should use the `README.md` file.
11+
12+
Before you report an issue, please search through existing issues to see if your issue is already reported or fixed to make sure you are not reporting a duplicated issue. Also make sure you have the latest version of HTTP Client Mock and see if the issue still exists.

.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
custom: "https://www.paypal.me/ddrv"

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: 'Bug'
5+
labels: bug
6+
assignees: ddrv
7+
8+
---
9+
10+
### What steps will reproduce the problem?
11+
12+
### What is the expected result?
13+
14+
### What do you get instead?
15+
16+
### Additional info
17+
18+
| Q | A
19+
| ---------------- | ---
20+
| Library version | 1.?
21+
| PHP version | ?
22+
| Operating system | ?

.github/ISSUE_TEMPLATE/custom.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
name: Custom issue template
3+
about: Describe this issue template's purpose here.
4+
title: ''
5+
labels: ''
6+
assignees: ddrv
7+
8+
---
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ''
5+
labels: feature
6+
assignees: ddrv
7+
8+
---
9+
10+
**Is your feature request related to a problem? Please describe.**
11+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen.
15+
16+
**Describe alternatives you've considered**
17+
A clear and concise description of any alternative solutions or features you've considered.
18+
19+
**Additional context**
20+
Add any other context or screenshots about the feature request here.

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
| Q | A
2+
| ------------- | ---
3+
| Bug fix? | yes/no
4+
| New feature? | yes/no
5+
| Tickets | Fix #...

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
vendor/
2+
composer.lock
3+
phpcs.xml
4+
phpunit.xml

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2020 Ivan Dudarev, https://github.com/ddrv <[email protected]>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
[![Latest Stable Version](https://img.shields.io/packagist/v/webclient/fake-http-client.svg?style=flat-square)](https://packagist.org/packages/webclient/fake-http-client)
2+
[![Total Downloads](https://img.shields.io/packagist/dt/webclient/fake-http-client.svg?style=flat-square)](https://packagist.org/packages/webclient/fake-http-client/stats)
3+
[![License](https://img.shields.io/packagist/l/webclient/fake-http-client.svg?style=flat-square)](https://github.com/phpwebclient/fake-http-client/blob/master/LICENSE)
4+
[![PHP](https://img.shields.io/packagist/php-v/webclient/fake-http-client/v1.0.0.svg?style=flat-square)](https://php.net)
5+
6+
# webclient/fake-http-client
7+
8+
Mock for PSR-18 HTTP client
9+
10+
# Install
11+
12+
Add package to project
13+
14+
```bash
15+
composer require --dev webclient/fake-http-client:^1.0
16+
```
17+
18+
Set autoload
19+
20+
```php
21+
<?php
22+
23+
include 'vendor/autoload.php';
24+
```
25+
26+
# Using
27+
28+
```php
29+
<?php
30+
31+
use Webclient\Fake\Client;
32+
use Psr\Http\Message\RequestInterface;
33+
use Psr\Http\Server\RequestHandlerInterface;
34+
35+
/**
36+
* @var RequestHandlerInterface $handler your mock handler
37+
* @var RequestInterface $request your tested request
38+
*/
39+
$client = new Client($handler);
40+
41+
$response = $client->sendRequest($request);
42+
```
43+
44+
# Routing
45+
46+
This package provides simple routing.
47+
48+
```php
49+
<?php
50+
51+
use Webclient\Fake\Client;
52+
use Webclient\Fake\Handler\SimpleRoutingHandler;
53+
use Psr\Http\Message\RequestInterface;
54+
use Psr\Http\Server\RequestHandlerInterface;
55+
56+
/**
57+
* @var RequestHandlerInterface $notFoundHandler default handler, error 404
58+
* @var RequestHandlerInterface $entityCreatedHandler handler for 201 request (POST /entities)
59+
* @var RequestHandlerInterface $entityHandler handler for 200 request (GET /entities/1)
60+
* @var RequestHandlerInterface $entityDeletedHandler handler for 204 request (DELETE /entities/2)
61+
* @var RequestInterface $errorRequest request for unused uri (GET /users)
62+
* @var RequestInterface $entityCreatingRequest request for creating entity (POST /entities)
63+
* @var RequestInterface $entityRequest request for getting entity (GET /entities/1)
64+
* @var RequestInterface $entityDeletingRequest request for deleting entity (DELETE /entities/2)
65+
*/
66+
67+
$handler = new SimpleRoutingHandler($notFoundHandler);
68+
$handler
69+
->route(['GET', 'HEAD'], '/entities/1', $entityHandler)
70+
->route(['POST'], '/entities', $entityCreatedHandler)
71+
->route(['DELETED'], '/entities/2', $entityDeletedHandler)
72+
;
73+
$client = new Client($handler);
74+
75+
$response1 = $client->sendRequest($errorRequest); // returns error 404
76+
$response2 = $client->sendRequest($entityCreatingRequest); // returns success response 201
77+
$response3 = $client->sendRequest($entityRequest); // returns success response 200
78+
$response4 = $client->sendRequest($entityDeletingRequest); // returns success response 204
79+
```
80+
81+
# Nuance
82+
83+
If you pass the `\Psr\Http\Message\ServerRequestInterface` object to client and want the handler to receive it as is,
84+
add the attribute `\Webclient\Fake\Client::NO_REPLACE_ATTRIBUTE`:
85+
86+
```php
87+
<?php
88+
89+
use Webclient\Fake\Client;
90+
use Psr\Http\Message\ServerRequestInterface;
91+
use Psr\Http\Server\RequestHandlerInterface;
92+
93+
/**
94+
* @var RequestHandlerInterface $handler your mock handler
95+
* @var ServerRequestInterface $request your tested request
96+
*/
97+
$client = new Client($handler);
98+
99+
$response = $client->sendRequest($request->withAttribute(Client::NO_REPLACE_ATTRIBUTE, true));
100+
```

composer.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "webclient/fake-http-client",
3+
"type": "library",
4+
"description": "Mock PSR-18 HTTP client",
5+
"keywords": ["http", "client", "http-client", "mock", "fake"],
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Ivan Dudarev",
10+
"email": "[email protected]",
11+
"homepage": "https://ddrv.ru"
12+
}
13+
],
14+
"require": {
15+
"php": ">=7.0",
16+
"ext-json": "*",
17+
"psr/http-client": "^1.0",
18+
"psr/http-message": "^1.0",
19+
"psr/http-server-handler": "^1.0"
20+
},
21+
"require-dev": {
22+
"nyholm/psr7": "^1.3",
23+
"phpunit/phpunit": "^6.5",
24+
"squizlabs/php_codesniffer": "^3.5"
25+
},
26+
"provide": {
27+
"psr/http-client-implementation": "1.0"
28+
},
29+
"autoload": {
30+
"psr-4": {
31+
"Webclient\\Fake\\": "src/"
32+
}
33+
},
34+
"autoload-dev": {
35+
"psr-4": {
36+
"Webclient\\Stuff\\Fake\\": "stuff/",
37+
"Webclient\\Tests\\Fake\\": "tests/"
38+
}
39+
},
40+
"funding": [
41+
{
42+
"type": "other",
43+
"url": "https://www.paypal.me/ddrv"
44+
}
45+
]
46+
}

phpcs.xml.dist

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="Coding standard">
3+
<description>Coding standard</description>
4+
5+
<!-- display progress -->
6+
<arg value="p"/>
7+
<!-- use colors in output -->
8+
<arg name="colors"/>
9+
10+
<!-- inherit rules from: -->
11+
<rule ref="PSR12">
12+
<exclude name="PSR12.Properties.ConstantVisibility.NotFound"/>
13+
</rule>
14+
15+
<!-- Paths to check -->
16+
<file>src</file>
17+
<file>stuff</file>
18+
<file>tests</file>
19+
</ruleset>

phpunit.xml.dist

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php" colors="true">
3+
<testsuites>
4+
<testsuite name="all">
5+
<directory>tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
</phpunit>

src/Client.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Webclient\Fake;
6+
7+
use Webclient\Fake\Exception\NetworkError;
8+
use Webclient\Fake\Message\ServerRequest;
9+
use Psr\Http\Client\ClientExceptionInterface;
10+
use Psr\Http\Client\ClientInterface;
11+
use Psr\Http\Message\RequestInterface;
12+
use Psr\Http\Message\ResponseInterface;
13+
use Psr\Http\Message\ServerRequestInterface;
14+
use Psr\Http\Server\RequestHandlerInterface;
15+
use Throwable;
16+
17+
final class Client implements ClientInterface
18+
{
19+
20+
const NO_REPLACE_ATTRIBUTE = 'webclient-fake-http-client-request-no-replace';
21+
22+
/**
23+
* @var RequestHandlerInterface
24+
*/
25+
private $handler;
26+
27+
/**
28+
* @var array
29+
*/
30+
private $server;
31+
32+
public function __construct(RequestHandlerInterface $handler, array $server = [])
33+
{
34+
$this->handler = $handler;
35+
$this->server = $server;
36+
}
37+
38+
/**
39+
* @inheritDoc
40+
*/
41+
public function sendRequest(RequestInterface $request): ResponseInterface
42+
{
43+
if ($request instanceof ServerRequestInterface && $request->getAttribute(self::NO_REPLACE_ATTRIBUTE, false)) {
44+
$serverRequest = $request;
45+
} else {
46+
$serverRequest = new ServerRequest(
47+
$request->getUri(),
48+
$request->getBody(),
49+
$request->getProtocolVersion(),
50+
$request->getMethod(),
51+
$request->getRequestTarget(),
52+
$request->getHeaders(),
53+
$this->server
54+
);
55+
}
56+
$serverRequest->getBody()->rewind();
57+
$handler = $this->handler;
58+
try {
59+
return $handler->handle($serverRequest);
60+
} /** @noinspection PhpRedundantCatchClauseInspection */ catch (ClientExceptionInterface $exception) {
61+
throw $exception;
62+
} catch (Throwable $exception) {
63+
throw new NetworkError($request, $exception);
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)