1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Mock; |
4
|
|
|
|
5
|
|
|
use Http\Client\Common\HttpAsyncClientEmulator; |
6
|
|
|
use Http\Client\Common\VersionBridgeClient; |
7
|
|
|
use Http\Client\Exception; |
8
|
|
|
use Http\Client\HttpAsyncClient; |
9
|
|
|
use Http\Client\HttpClient; |
10
|
|
|
use Http\Message\RequestMatcher; |
11
|
|
|
use Http\Mock\Exception\RequestMismatchException; |
12
|
|
|
use Http\Mock\Exception\UnexpectedRequestException; |
13
|
|
|
use Psr\Http\Client\ClientExceptionInterface; |
14
|
|
|
use Psr\Http\Message\RequestInterface; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* An implementation of the HTTP client that is useful for automated tests. |
19
|
|
|
* |
20
|
|
|
* This mock expects requests to be sent in a sequence and returns responses or throws exceptions as configured. |
21
|
|
|
* |
22
|
|
|
* @author Andreas Möller <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class StrictClient implements HttpClient, HttpAsyncClient |
25
|
|
|
{ |
26
|
|
|
use HttpAsyncClientEmulator; |
27
|
|
|
use VersionBridgeClient; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $configuredSequence = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
* |
37
|
|
|
* @throws UnexpectedRequestException |
38
|
|
|
*/ |
39
|
|
|
public function doSendRequest(RequestInterface $request) |
40
|
|
|
{ |
41
|
|
|
$next = array_shift($this->configuredSequence); |
42
|
|
|
|
43
|
|
|
if (null === $next) { |
44
|
|
|
throw UnexpectedRequestException::fromRequest($request); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** @var RequestMatcher $matcher */ |
48
|
|
|
$matcher = $next['matcher']; |
49
|
|
|
|
50
|
|
|
try { |
51
|
|
|
$isMatch = $matcher->matches($request); |
52
|
|
|
} catch (\Exception $exception) { |
53
|
|
|
throw RequestMismatchException::fromMatcherException($exception); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (false === $isMatch) { |
57
|
|
|
throw RequestMismatchException::create(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** @var callable $callable */ |
61
|
|
|
$callable = $next['callable']; |
62
|
|
|
|
63
|
|
|
return $callable($request); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Adds an exception to be thrown or response to be returned for the next request |
68
|
|
|
* expected to be sent in a sequence of requests. |
69
|
|
|
* |
70
|
|
|
* For more complex logic, pass a callable as $result. The method is given |
71
|
|
|
* the request and MUST either return a ResponseInterface or throw an |
72
|
|
|
* exception that implements the PSR-18 / HTTPlug exception interface. |
73
|
|
|
* |
74
|
|
|
* @param ResponseInterface|Exception|ClientExceptionInterface|callable $result |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public function on(RequestMatcher $requestMatcher, $result) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$callable = null; |
|
|
|
|
79
|
|
|
|
80
|
|
|
switch (true) { |
81
|
|
|
case is_callable($result): |
82
|
|
|
$callable = $result; |
83
|
|
|
|
84
|
|
|
break; |
85
|
|
|
case $result instanceof ResponseInterface: |
86
|
|
|
$callable = function () use ($result) { |
87
|
|
|
return $result; |
88
|
|
|
}; |
89
|
|
|
|
90
|
|
|
break; |
91
|
|
|
case $result instanceof \Exception: |
92
|
|
|
$callable = function () use ($result) { |
93
|
|
|
throw $result; |
94
|
|
|
}; |
95
|
|
|
|
96
|
|
|
break; |
97
|
|
|
default: |
98
|
|
|
throw new \InvalidArgumentException('Result must be either a response, an exception, or a callable'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->configuredSequence[] = [ |
102
|
|
|
'matcher' => $requestMatcher, |
103
|
|
|
'callable' => $callable, |
104
|
|
|
]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns true when the configured sequence of requests and responses (or exceptions) |
109
|
|
|
* has been completed, i.e., the queue of configured results has been exhausted. |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public function hasCompletedSequence() |
114
|
|
|
{ |
115
|
|
|
return 0 === count($this->configuredSequence); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.