1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Meare\Juggler\Imposter; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Meare\Juggler\Exception\Client\NotFoundException; |
7
|
|
|
use function Meare\Juggler\is_subarray_assoc; |
8
|
|
|
|
9
|
|
|
abstract class Imposter implements \JsonSerializable |
10
|
|
|
{ |
11
|
|
|
const PROTOCOL_HTTPS = 'https'; |
12
|
|
|
const PROTOCOL_HTTP = 'http'; |
13
|
|
|
const PROTOCOL_TCP = 'tcp'; |
14
|
|
|
const PROTOCOL_SMTP = 'smtp'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $protocol; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $requests; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int|null |
28
|
|
|
*/ |
29
|
|
|
private $port; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $name; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param int|null $port |
38
|
|
|
* @param array $requests |
39
|
|
|
*/ |
40
|
|
|
public function __construct(int $port = null, array $requests = []) |
41
|
|
|
{ |
42
|
|
|
if (null !== $port) { |
43
|
|
|
$this->setPort($port); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->setRequests($requests); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param array $requests |
51
|
|
|
*/ |
52
|
|
|
private function setRequests(array $requests) |
53
|
|
|
{ |
54
|
|
|
$this->requests = $requests; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
public function hasRequests() : bool |
61
|
|
|
{ |
62
|
|
|
return sizeof($this->requests) > 0; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function getRequests() : array |
69
|
|
|
{ |
70
|
|
|
return $this->requests; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param array|callable $criteria |
75
|
|
|
* @param int $exactly Expect exactly n occurrences |
76
|
|
|
* @return bool |
77
|
|
|
* @throws NotFoundException |
78
|
|
|
*/ |
79
|
|
|
public function hasRequestsByCriteria($criteria, int $exactly = null) : bool |
80
|
|
|
{ |
81
|
|
|
$num = $this->countRequestsByCriteria($criteria); |
82
|
|
|
if (null === $exactly) { |
83
|
|
|
return $num > 0; |
84
|
|
|
} else { |
85
|
|
|
return $num === $exactly; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param array|callable $criteria |
91
|
|
|
* @return int |
92
|
|
|
*/ |
93
|
|
|
public function countRequestsByCriteria($criteria) : int |
94
|
|
|
{ |
95
|
|
|
try { |
96
|
|
|
return sizeof($this->findRequests($criteria)); |
97
|
|
|
} catch (NotFoundException $e) { |
98
|
|
|
return 0; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param array|callable $criteria |
104
|
|
|
* @return array[] |
105
|
|
|
* @throws NotFoundException |
106
|
|
|
* @throws \InvalidArgumentException |
107
|
|
|
*/ |
108
|
|
|
public function findRequests($criteria) : array |
109
|
|
|
{ |
110
|
|
|
switch (true) { |
111
|
|
|
case is_array($criteria): |
112
|
|
|
$matched_requests = $this->findRequestsWithSubarray($criteria); |
113
|
|
|
break; |
114
|
|
|
|
115
|
|
|
case is_callable($criteria): |
116
|
|
|
$matched_requests = $this->findRequestsWithCallable($criteria); |
117
|
|
|
break; |
118
|
|
|
|
119
|
|
|
default: |
120
|
|
|
throw new \InvalidArgumentException('Criteria could only be array or callable'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (0 === sizeof($matched_requests)) { |
124
|
|
|
throw new NotFoundException('Unable to find any requests per criteria'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $matched_requests; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param array $criteria |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
private function findRequestsWithSubarray($criteria) |
135
|
|
|
{ |
136
|
|
|
$matched_requests = []; |
137
|
|
|
foreach ($this->requests as $request) { |
138
|
|
|
if (is_subarray_assoc($criteria, $request)) { |
139
|
|
|
$matched_requests[] = $request; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $matched_requests; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param callback $callback |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
|
|
private function findRequestsWithCallable($callback) |
151
|
|
|
{ |
152
|
|
|
$matched_requests = []; |
153
|
|
|
foreach ($this->requests as $request) { |
154
|
|
|
if (true === $callback($request)) { |
155
|
|
|
$matched_requests[] = $request; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $matched_requests; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return int|null |
164
|
|
|
*/ |
165
|
|
|
public function getPort() |
166
|
|
|
{ |
167
|
|
|
return $this->port; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param int $port |
172
|
|
|
* @return self |
173
|
|
|
*/ |
174
|
|
|
public function setPort(int $port) |
175
|
|
|
{ |
176
|
|
|
$this->port = $port; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public function getProtocol() : string |
183
|
|
|
{ |
184
|
|
|
return $this->protocol; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return bool |
189
|
|
|
*/ |
190
|
|
|
public function hasName() : bool |
191
|
|
|
{ |
192
|
|
|
return null !== $this->getName(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return string|null |
197
|
|
|
*/ |
198
|
|
|
public function getName() |
199
|
|
|
{ |
200
|
|
|
return $this->name; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param string $name |
205
|
|
|
*/ |
206
|
|
|
public function setName(string $name) |
207
|
|
|
{ |
208
|
|
|
$this->name = $name; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return bool |
213
|
|
|
*/ |
214
|
|
|
public function hasPort() : bool |
215
|
|
|
{ |
216
|
|
|
return null !== $this->port; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|