1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Meare\Juggler\Imposter; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Meare\Juggler\Exception\Client\NotFoundException; |
7
|
|
|
use Meare\Juggler\Imposter\Stub\Predicate\IPredicate; |
8
|
|
|
use Meare\Juggler\Imposter\Stub\Response\IResponse; |
9
|
|
|
|
10
|
|
|
class HttpImposter extends Imposter |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $protocol = self::PROTOCOL_HTTP; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Stub\Stub[] |
19
|
|
|
*/ |
20
|
|
|
private $stubs = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return array |
24
|
|
|
*/ |
25
|
8 |
|
public function jsonSerialize() |
26
|
|
|
{ |
27
|
8 |
|
return \Meare\Juggler\array_filter_null([ |
28
|
8 |
|
'port' => $this->getPort(), |
29
|
8 |
|
'protocol' => $this->getProtocol(), |
30
|
8 |
|
'name' => $this->getName(), |
31
|
8 |
|
'requests' => $this->hasRequests() ? $this->serializeRequests() : null, |
32
|
8 |
|
'stubs' => $this->jsonSerializeStubs(), |
33
|
8 |
|
]); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
3 |
|
private function serializeRequests() |
40
|
|
|
{ |
41
|
3 |
|
$requests = $this->requests; |
42
|
3 |
|
foreach ($requests as $key => $request) { |
43
|
3 |
View Code Duplication |
if (isset($request['query'])) { |
|
|
|
|
44
|
1 |
|
$requests[$key]['query'] = \Meare\Juggler\json_object($request['query']); |
45
|
1 |
|
} |
46
|
3 |
View Code Duplication |
if (isset($request['headers'])) { |
|
|
|
|
47
|
1 |
|
$requests[$key]['headers'] = \Meare\Juggler\json_object($request['headers']); |
48
|
1 |
|
} |
49
|
3 |
|
} |
50
|
|
|
|
51
|
3 |
|
return $requests; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
8 |
|
private function jsonSerializeStubs() |
58
|
|
|
{ |
59
|
8 |
|
$stubs = []; |
60
|
8 |
|
foreach ($this->stubs as $stub) { |
61
|
1 |
|
$stubs[] = $stub->jsonSerialize(); |
62
|
8 |
|
} |
63
|
|
|
|
64
|
8 |
|
return $stubs; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param IResponse[]|IResponse $responses |
69
|
|
|
* @param IPredicate[]|IPredicate $predicates |
70
|
|
|
* @return Stub\Stub |
71
|
|
|
*/ |
72
|
1 |
|
public function createStub($responses = null, $predicates = null) |
73
|
|
|
{ |
74
|
1 |
|
$stub = new Stub\Stub($responses, $predicates); |
|
|
|
|
75
|
1 |
|
$this->addStub($stub); |
76
|
|
|
|
77
|
1 |
|
return $stub; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param Stub\Stub $stub |
82
|
|
|
*/ |
83
|
7 |
|
public function addStub(Stub\Stub $stub) |
84
|
|
|
{ |
85
|
7 |
|
$this->stubs[] = $stub; |
86
|
7 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return Stub\Stub[] |
90
|
|
|
*/ |
91
|
3 |
|
public function getStubs() |
92
|
|
|
{ |
93
|
3 |
|
return $this->stubs; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param Stub\Stub[] $stubs |
98
|
|
|
*/ |
99
|
2 |
|
public function setStubs(array $stubs) |
100
|
|
|
{ |
101
|
2 |
|
$this->clearStubs(); |
102
|
2 |
|
$this->addStubs($stubs); |
103
|
2 |
|
} |
104
|
|
|
|
105
|
2 |
|
public function clearStubs() |
106
|
|
|
{ |
107
|
2 |
|
$this->stubs = []; |
108
|
2 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param Stub\Stub[] $stubs |
112
|
|
|
*/ |
113
|
2 |
|
public function addStubs(array $stubs) |
114
|
|
|
{ |
115
|
2 |
|
foreach ($stubs as $stub) { |
116
|
2 |
|
$this->addStub($stub); |
117
|
2 |
|
} |
118
|
2 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param Stub\Stub $stub |
122
|
|
|
* @throws NotFoundException |
123
|
|
|
*/ |
124
|
2 |
|
public function removeStub(Stub\Stub $stub) |
125
|
|
|
{ |
126
|
2 |
|
foreach ($this->stubs as $key => $s) { |
127
|
1 |
|
if ($s === $stub) { |
128
|
1 |
|
unset($this->stubs[$key]); |
129
|
|
|
|
130
|
1 |
|
return; |
131
|
|
|
} |
132
|
1 |
|
} |
133
|
1 |
|
throw new NotFoundException('Unable to find stub'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param array $criteria |
138
|
|
|
* @return Stub\Stub |
139
|
|
|
* @throws NotFoundException |
140
|
|
|
*/ |
141
|
2 |
|
public function findStubByPredicates($criteria) |
142
|
|
|
{ |
143
|
2 |
|
foreach ($this->stubs as $stub) { |
144
|
1 |
|
if ($stub->isPredicatesMatch($criteria)) { |
145
|
1 |
|
return $stub; |
146
|
|
|
} |
147
|
2 |
|
} |
148
|
1 |
|
throw new NotFoundException('Unable to find stub'); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
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.