1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EasyHttp\MockBuilder; |
4
|
|
|
|
5
|
|
|
use EasyHttp\MockBuilder\Expectations\BodyExpectation; |
6
|
|
|
use EasyHttp\MockBuilder\Expectations\HeaderExistsExpectation; |
7
|
|
|
use EasyHttp\MockBuilder\Expectations\HeaderIsExpectation; |
8
|
|
|
use EasyHttp\MockBuilder\Expectations\HeaderIsNotExpectation; |
9
|
|
|
use EasyHttp\MockBuilder\Expectations\HeaderNotExistsExpectation; |
10
|
|
|
use EasyHttp\MockBuilder\Expectations\MethodIsExpectation; |
11
|
|
|
use EasyHttp\MockBuilder\Expectations\ParamExistsExpectation; |
12
|
|
|
use EasyHttp\MockBuilder\Expectations\ParamIsExpectation; |
13
|
|
|
use EasyHttp\MockBuilder\Expectations\ParamNotExistsExpectation; |
14
|
|
|
use EasyHttp\MockBuilder\Expectations\PathIsExpectation; |
15
|
|
|
use EasyHttp\MockBuilder\Expectations\PathMatchExpectation; |
16
|
|
|
use EasyHttp\MockBuilder\Expectations\RequestExpectation; |
17
|
|
|
use GuzzleHttp\Promise\FulfilledPromise; |
18
|
|
|
use GuzzleHttp\Promise\Promise; |
19
|
|
|
use GuzzleHttp\Psr7\Response; |
20
|
|
|
use Psr\Http\Message\RequestInterface; |
21
|
|
|
|
22
|
|
|
class HttpMock |
23
|
|
|
{ |
24
|
|
|
private MockBuilder $builder; |
25
|
|
|
|
26
|
121 |
|
public function __construct(MockBuilder $builder) |
27
|
|
|
{ |
28
|
121 |
|
$this->builder = $builder; |
29
|
|
|
} |
30
|
|
|
|
31
|
121 |
|
public function __invoke(RequestInterface $request): FulfilledPromise |
32
|
|
|
{ |
33
|
121 |
|
$rejectionReason = ''; |
34
|
|
|
|
35
|
121 |
|
foreach ($this->builder->getExpectations() as $expectation) { |
36
|
121 |
|
$matches = true; |
37
|
|
|
|
38
|
121 |
|
$promise = new Promise( |
39
|
121 |
|
function () use (&$promise, $request) { |
40
|
121 |
|
$promise->resolve($request); |
41
|
121 |
|
} |
42
|
121 |
|
); |
43
|
121 |
|
$promise |
44
|
121 |
|
->then(MethodIsExpectation::from($expectation)) |
45
|
121 |
|
->then(PathIsExpectation::from($expectation)) |
46
|
121 |
|
->then(PathMatchExpectation::from($expectation)) |
47
|
121 |
|
->then(ParamIsExpectation::from($expectation)) |
48
|
121 |
|
->then(ParamExistsExpectation::from($expectation)) |
49
|
121 |
|
->then(ParamNotExistsExpectation::from($expectation)) |
50
|
121 |
|
->then(HeaderIsExpectation::from($expectation)) |
51
|
121 |
|
->then(HeaderIsNotExpectation::from($expectation)) |
52
|
121 |
|
->then(HeaderExistsExpectation::from($expectation)) |
53
|
121 |
|
->then(HeaderNotExistsExpectation::from($expectation)) |
54
|
121 |
|
->then(BodyExpectation::from($expectation)) |
55
|
121 |
|
->then(RequestExpectation::from($expectation)) |
56
|
121 |
|
->otherwise( |
57
|
121 |
|
function ($reason) use (&$matches, &$rejectionReason) { |
58
|
67 |
|
$rejectionReason = $reason; |
59
|
67 |
|
$matches = false; |
60
|
121 |
|
} |
61
|
121 |
|
); |
62
|
|
|
|
63
|
121 |
|
$promise->wait(); |
64
|
|
|
|
65
|
121 |
|
if ($matches) { |
66
|
56 |
|
return $expectation->responseBuilder($request)->response(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
66 |
|
return $this->fallbackResponse($rejectionReason); |
71
|
|
|
} |
72
|
|
|
|
73
|
66 |
|
private function fallbackResponse($rejectionReason): FulfilledPromise |
74
|
|
|
{ |
75
|
66 |
|
return new FulfilledPromise( |
76
|
66 |
|
new Response(404, [], $rejectionReason) |
77
|
66 |
|
); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|