1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Phiremock. |
4
|
|
|
* |
5
|
|
|
* Phiremock is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* Phiremock is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with Phiremock. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Mcustiel\Phiremock\Client\Utils; |
20
|
|
|
|
21
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\BinaryBodyCondition; |
22
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\BodyCondition; |
23
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\FormDataCondition; |
24
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\FormFieldCondition; |
25
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\HeaderCondition; |
26
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\HeaderConditionCollection; |
27
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\JsonPathCondition; |
28
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\JsonPathConditionCollection; |
29
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\MethodCondition; |
30
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\UrlCondition; |
31
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Matchers\Equals; |
32
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Matchers\Matcher; |
33
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Matchers\MatcherFactory; |
34
|
|
|
use Mcustiel\Phiremock\Domain\Conditions as RequestConditions; |
35
|
|
|
use Mcustiel\Phiremock\Domain\Http\FormFieldName; |
36
|
|
|
use Mcustiel\Phiremock\Domain\Http\HeaderName; |
37
|
|
|
use Mcustiel\Phiremock\Domain\Http\JsonPathName; |
38
|
|
|
use Mcustiel\Phiremock\Domain\Options\ScenarioName; |
39
|
|
|
use Mcustiel\Phiremock\Domain\Options\ScenarioState; |
40
|
|
|
|
41
|
|
|
class ConditionsBuilder |
42
|
|
|
{ |
43
|
|
|
/** @var MethodCondition */ |
44
|
|
|
private $methodCondition; |
45
|
|
|
/** @var UrlCondition */ |
46
|
|
|
private $urlCondition; |
47
|
|
|
/** @var BodyCondition */ |
48
|
|
|
private $bodyCondition; |
49
|
|
|
/** @var HeaderConditionCollection */ |
50
|
|
|
private $headersConditions; |
51
|
|
|
/** @var ScenarioName */ |
52
|
|
|
private $scenarioName; |
53
|
|
|
/** @var ScenarioState */ |
54
|
|
|
private $scenarioIs; |
55
|
|
|
/** @var FormDataCondition */ |
56
|
|
|
private $formData; |
57
|
|
|
/** @var JsonPathConditionCollection */ |
58
|
|
|
private $jsonPath; |
59
|
|
|
|
60
|
|
|
public function __construct(MethodCondition $methodCondition = null, UrlCondition $urlCondition = null) |
61
|
|
|
{ |
62
|
|
|
$this->headersConditions = new HeaderConditionCollection(); |
63
|
|
|
$this->formData = new FormDataCondition(); |
64
|
|
|
$this->jsonPath = new JsonPathConditionCollection(); |
65
|
|
|
$this->methodCondition = $methodCondition; |
66
|
|
|
$this->urlCondition = $urlCondition; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public static function create(?string $method = null, ?string $url = null): self |
70
|
|
|
{ |
71
|
|
|
return new static( |
72
|
|
|
$method === null ? null : new MethodCondition(MatcherFactory::sameString($method)), |
73
|
|
|
$url === null ? null : new UrlCondition(MatcherFactory::equalsTo($url)) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function andMethod(Matcher $matcher): self |
78
|
|
|
{ |
79
|
|
|
if ($matcher instanceof Equals) { |
80
|
|
|
$matcher = MatcherFactory::sameString($matcher->getCheckValue()->get()); |
81
|
|
|
} |
82
|
|
|
$this->methodCondition = new MethodCondition($matcher); |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function andBody(Matcher $matcher): self |
88
|
|
|
{ |
89
|
|
|
$this->bodyCondition = new BodyCondition($matcher); |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function andBinaryBody(Matcher $matcher): self |
95
|
|
|
{ |
96
|
|
|
$this->bodyCondition = new BinaryBodyCondition($matcher); |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function andHeader(string $header, Matcher $matcher): self |
102
|
|
|
{ |
103
|
|
|
$this->headersConditions->setHeaderCondition( |
104
|
|
|
new HeaderName($header), |
105
|
|
|
new HeaderCondition($matcher) |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function andFormField(string $fieldName, Matcher $matcher): self |
112
|
|
|
{ |
113
|
|
|
$this->formData->setFieldCondition( |
114
|
|
|
new FormFieldName($fieldName), |
115
|
|
|
new FormFieldCondition($matcher) |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function andUrl(Matcher $matcher): self |
122
|
|
|
{ |
123
|
|
|
$this->urlCondition = new UrlCondition($matcher); |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function andScenarioState(string $scenario, string $scenarioState): self |
129
|
|
|
{ |
130
|
|
|
$this->scenarioName = new ScenarioName($scenario); |
131
|
|
|
$this->scenarioIs = new ScenarioState($scenarioState); |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function andJsonPath(string $path, Matcher $matcher): self |
137
|
|
|
{ |
138
|
|
|
$this->jsonPath->setPathCondition( |
139
|
|
|
new JsonPathName($path), |
140
|
|
|
new JsonPathCondition($matcher) |
141
|
|
|
); |
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function build(): ConditionsBuilderResult |
146
|
|
|
{ |
147
|
|
|
return new ConditionsBuilderResult( |
148
|
|
|
new RequestConditions( |
149
|
|
|
$this->methodCondition, |
150
|
|
|
$this->urlCondition, |
151
|
|
|
$this->bodyCondition, |
152
|
|
|
$this->headersConditions->iterator(), |
153
|
|
|
$this->formData->iterator(), |
154
|
|
|
$this->scenarioIs, |
155
|
|
|
$this->jsonPath->iterator() |
156
|
|
|
), |
157
|
|
|
$this->scenarioName |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|