Skip to content

Commit b814750

Browse files
committed
version 3
1 parent f975ce1 commit b814750

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2307
-190
lines changed

.gitattributes

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44
*.yml text eol=lf
55
*.dist text eol=lf
66
LICENCE text eol=lf
7-
stuff/ export-ignore
87
tests/ export-ignore
98
*.dist export-ignore

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ assignees: ddrv
1515

1616
### Additional info
1717

18-
| Q | A
19-
| ---------------- | ---
20-
| Library version | 1.?
21-
| PHP version | ?
22-
| Operating system | ?
18+
| Q | A |
19+
|------------------|-----|
20+
| Library version | 3.? |
21+
| PHP version | ? |
22+
| Operating system | ? |

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,8 @@ jobs:
3939
- name: Check code style
4040
run: vendor/bin/phpcs
4141

42+
- name: Stat Analise
43+
run: vendor/bin/psalm
44+
4245
- name: Run test suite
4346
run: vendor/bin/phpunit

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ vendor/
22
composer.lock
33
phpcs.xml
44
phpunit.xml
5+
psalm.xml

README.md

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Mock for PSR-18 HTTP client
1212
Add package to project
1313

1414
```bash
15-
composer require --dev webclient/fake-http-client:^2.0
15+
composer require --dev webclient/fake-http-client:^3.0
1616
```
1717

1818
Set autoload
@@ -40,16 +40,56 @@ $client = new FakeHttpClient($handler);
4040

4141
$response = $client->sendRequest($request);
4242
```
43+
# Handlers
4344

44-
# Routing
45+
## SpecHandler
46+
47+
This package provides universal handler `\Webclient\Fake\Handler\SpecHandler\SpecHandler`
48+
and builder `\Webclient\Fake\Handler\SpecHandler\SpecHandlerBuilder`.
49+
With it, you can customize the client for almost any need.
50+
51+
```php
52+
<?php
53+
54+
use Psr\Http\Message\ResponseInterface;
55+
use Webclient\Fake\FakeHttpClient;
56+
use Webclient\Fake\Handler\SpecHandler\SpecHandlerBuilder;
57+
use Webclient\Fake\Handler\SpecHandler\Rule;
58+
59+
$builder = SpecHandlerBuilder::create();
60+
61+
$builder
62+
->route(function (Rule $rule) {
63+
$rule->notEqual('header.authorization', 'bearer xxx');
64+
$rule->oneOf(function (Rule $rule) {
65+
$rule->allOf(function (Rule $rule) {
66+
$rule->equal('uri.path', '/api/v1/posts');
67+
$rule->equal('method', 'POST');
68+
});
69+
$rule->allOf(function (Rule $rule) {
70+
$rule->match('uri.path', '^/api/v1/posts/([a-zA-Z0-9\-]+)$');
71+
$rule->match('method', '^(PUT|DELETE)$');
72+
});
73+
});
74+
})
75+
->response(function (ResponseInterface $response): ResponseInterface {
76+
return $response->withStatus(403);
77+
});
78+
79+
$handler = $builder->build();
80+
81+
$client = new FakeHttpClient($handler);
82+
```
83+
84+
## SimpleRoutingHandler
4585

4686
This package provides simple routing.
4787

4888
```php
4989
<?php
5090

5191
use Webclient\Fake\FakeHttpClient;
52-
use Webclient\Fake\Handler\SimpleRoutingHandler;
92+
use Webclient\Fake\Handler\SimpleRoutingHandler\SimpleRoutingHandler;
5393
use Psr\Http\Message\RequestInterface;
5494
use Psr\Http\Server\RequestHandlerInterface;
5595

composer.json

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
"ext-mbstring": "*",
1818
"psr/http-client": "^1.0",
1919
"psr/http-message": "^1.0",
20+
"psr/http-factory": "^1.0",
2021
"psr/http-server-handler": "^1.0"
2122
},
2223
"require-dev": {
23-
"psr/http-factory": "^1.0",
2424
"nyholm/psr7": "^1.5",
2525
"phpunit/phpunit": "^6.5 || ^7.5 || 8.5 || 9.5",
26-
"squizlabs/php_codesniffer": "^3.5"
26+
"squizlabs/php_codesniffer": "^3.5",
27+
"vimeo/psalm": "^4.30"
2728
},
2829
"provide": {
2930
"psr/http-client-implementation": "1.0"
@@ -35,14 +36,19 @@
3536
},
3637
"autoload-dev": {
3738
"psr-4": {
38-
"Webclient\\Stuff\\Fake\\": "stuff/",
39-
"Webclient\\Tests\\Fake\\": "tests/"
39+
"Tests\\Webclient\\Fake\\Tools\\": "tests/src",
40+
"Tests\\Webclient\\Fake\\Unit\\": "tests/unit"
4041
}
4142
},
4243
"funding": [
4344
{
4445
"type": "other",
4546
"url": "https://www.paypal.me/ddrv"
4647
}
47-
]
48+
],
49+
"config": {
50+
"allow-plugins": {
51+
"composer/package-versions-deprecated": true
52+
}
53+
}
4854
}

phpcs.xml.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@
1212

1313
<!-- Paths to check -->
1414
<file>src</file>
15-
<file>stuff</file>
1615
<file>tests</file>
1716
</ruleset>

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<phpunit bootstrap="vendor/autoload.php" colors="true" cacheResult="false">
33
<testsuites>
44
<testsuite name="all">
5-
<directory>tests</directory>
5+
<directory>tests/unit</directory>
66
</testsuite>
77
</testsuites>
88
</phpunit>

psalm.xml.dist

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
errorLevel="1"
4+
resolveFromConfigFile="true"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xmlns="https://getpsalm.org/schema/config"
7+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
8+
>
9+
<projectFiles>
10+
<directory name="src" />
11+
<ignoreFiles>
12+
<directory name="vendor" />
13+
</ignoreFiles>
14+
</projectFiles>
15+
</psalm>

src/Exception/NetworkError.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Psr\Http\Message\RequestInterface;
1010
use Throwable;
1111

12-
class NetworkError extends Exception implements NetworkExceptionInterface
12+
final class NetworkError extends Exception implements NetworkExceptionInterface
1313
{
1414
private RequestInterface $request;
1515

src/FakeHttpClient.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final class FakeHttpClient implements ClientInterface
1919
public const NO_REPLACE_ATTRIBUTE = 'webclient-fake-http-client-request-no-replace';
2020

2121
private RequestHandlerInterface $handler;
22-
private array $server;
22+
private array $serverParams;
2323

24-
public function __construct(RequestHandlerInterface $handler, array $server = [])
24+
public function __construct(RequestHandlerInterface $handler, array $serverParams = [])
2525
{
2626
$this->handler = $handler;
27-
$this->server = $server;
27+
$this->serverParams = $serverParams;
2828
}
2929

3030
/**
@@ -36,13 +36,8 @@ public function sendRequest(RequestInterface $request): ResponseInterface
3636
$serverRequest = $request;
3737
} else {
3838
$serverRequest = new ServerRequest(
39-
$request->getUri(),
40-
$request->getBody(),
41-
$request->getProtocolVersion(),
42-
$request->getMethod(),
43-
$request->getRequestTarget(),
44-
$request->getHeaders(),
45-
$this->server
39+
$request,
40+
$this->serverParams
4641
);
4742
}
4843
$serverRequest->getBody()->rewind();

src/Handler/SimpleRoutingHandler.php renamed to src/Handler/SimpleRoutingHandler/SimpleRoutingHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
declare(strict_types=1);
44

5-
namespace Webclient\Fake\Handler;
5+
namespace Webclient\Fake\Handler\SimpleRoutingHandler;
66

77
use Psr\Http\Message\ResponseInterface;
88
use Psr\Http\Message\ServerRequestInterface;
99
use Psr\Http\Server\RequestHandlerInterface;
1010

11-
class SimpleRoutingHandler implements RequestHandlerInterface
11+
final class SimpleRoutingHandler implements RequestHandlerInterface
1212
{
1313
/** @var RequestHandlerInterface[] */
1414
private array $handlers = [];
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Webclient\Fake\Handler\SpecHandler\Comparer;
6+
7+
interface ComparerInterface
8+
{
9+
public function compare(?string $pattern, ?string $actual): bool;
10+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Webclient\Fake\Handler\SpecHandler\Comparer;
6+
7+
final class ContainsComparer implements ComparerInterface
8+
{
9+
public function compare(?string $pattern, ?string $actual): bool
10+
{
11+
if (is_null($pattern) && is_null($actual)) {
12+
return true;
13+
}
14+
if (is_null($pattern) || is_null($actual)) {
15+
return false;
16+
}
17+
return strpos($actual, $pattern) !== false;
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Webclient\Fake\Handler\SpecHandler\Comparer;
6+
7+
final class EqualComparer implements ComparerInterface
8+
{
9+
public function compare(?string $pattern, ?string $actual): bool
10+
{
11+
return $pattern === $actual;
12+
}
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Webclient\Fake\Handler\SpecHandler\Comparer;
6+
7+
final class MatchComparer implements ComparerInterface
8+
{
9+
public function compare(?string $pattern, ?string $actual): bool
10+
{
11+
if (is_null($pattern)) {
12+
return true;
13+
}
14+
$pattern = trim($pattern);
15+
if ($pattern === '') {
16+
return true;
17+
}
18+
preg_match('#' . $pattern . '#u', (string)$actual, $matches);
19+
return !empty($matches);
20+
}
21+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Webclient\Fake\Handler\SpecHandler\Condition;
6+
7+
use Psr\Http\Message\ServerRequestInterface;
8+
9+
final class AndCondition implements ConditionInterface
10+
{
11+
/**
12+
* @var ConditionInterface[]
13+
*/
14+
private array $conditions;
15+
16+
public function __construct(ConditionInterface ...$conditions)
17+
{
18+
$this->conditions = $conditions;
19+
}
20+
21+
public function check(ServerRequestInterface $request): bool
22+
{
23+
foreach ($this->conditions as $condition) {
24+
if (!$condition->check($request)) {
25+
return false;
26+
}
27+
}
28+
return true;
29+
}
30+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Webclient\Fake\Handler\SpecHandler\Condition;
4+
5+
use Psr\Http\Message\ServerRequestInterface;
6+
7+
interface ConditionInterface
8+
{
9+
public function check(ServerRequestInterface $request): bool;
10+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Webclient\Fake\Handler\SpecHandler\Condition;
6+
7+
use Psr\Http\Message\ServerRequestInterface;
8+
9+
final class NotCondition implements ConditionInterface
10+
{
11+
private ConditionInterface $condition;
12+
13+
public function __construct(ConditionInterface $condition)
14+
{
15+
$this->condition = $condition;
16+
}
17+
18+
public function check(ServerRequestInterface $request): bool
19+
{
20+
return !$this->condition->check($request);
21+
}
22+
}

0 commit comments

Comments
 (0)