|
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\MethodCondition; |
|
28
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\UrlCondition; |
|
29
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Matchers\Equals; |
|
30
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Matchers\Matcher; |
|
31
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Matchers\MatcherFactory; |
|
32
|
|
|
use Mcustiel\Phiremock\Domain\Conditions as RequestConditions; |
|
33
|
|
|
use Mcustiel\Phiremock\Domain\Http\FormFieldName; |
|
34
|
|
|
use Mcustiel\Phiremock\Domain\Http\HeaderName; |
|
35
|
|
|
use Mcustiel\Phiremock\Domain\Options\ScenarioName; |
|
36
|
|
|
use Mcustiel\Phiremock\Domain\Options\ScenarioState; |
|
37
|
|
|
|
|
38
|
|
|
class ConditionsBuilder |
|
39
|
|
|
{ |
|
40
|
|
|
/** @var MethodCondition */ |
|
41
|
|
|
private $methodCondition; |
|
42
|
|
|
/** @var UrlCondition */ |
|
43
|
|
|
private $urlCondition; |
|
44
|
|
|
/** @var BodyCondition */ |
|
45
|
|
|
private $bodyCondition; |
|
46
|
|
|
/** @var HeaderConditionCollection */ |
|
47
|
|
|
private $headersConditions; |
|
48
|
|
|
/** @var ScenarioName */ |
|
49
|
|
|
private $scenarioName; |
|
50
|
|
|
/** @var ScenarioState */ |
|
51
|
|
|
private $scenarioIs; |
|
52
|
|
|
/** @var FormDataCondition */ |
|
53
|
|
|
private $formData; |
|
54
|
|
|
|
|
55
|
|
|
public function __construct(MethodCondition $methodCondition = null, UrlCondition $urlCondition = null) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->headersConditions = new HeaderConditionCollection(); |
|
58
|
|
|
$this->formData = new FormDataCondition(); |
|
59
|
|
|
$this->methodCondition = $methodCondition; |
|
60
|
|
|
$this->urlCondition = $urlCondition; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public static function create(?string $method = null, ?string $url = null): self |
|
64
|
|
|
{ |
|
65
|
|
|
return new static( |
|
66
|
|
|
$method === null ? null : new MethodCondition(MatcherFactory::sameString($method)), |
|
67
|
|
|
$url === null ? null : new UrlCondition(MatcherFactory::equalsTo($url)) |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function andMethod(Matcher $matcher): self |
|
72
|
|
|
{ |
|
73
|
|
|
if ($matcher instanceof Equals) { |
|
74
|
|
|
$matcher = MatcherFactory::sameString($matcher->getCheckValue()->get()); |
|
75
|
|
|
} |
|
76
|
|
|
$this->methodCondition = new MethodCondition($matcher); |
|
77
|
|
|
|
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function andBody(Matcher $matcher): self |
|
82
|
|
|
{ |
|
83
|
|
|
$this->bodyCondition = new BodyCondition($matcher); |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function andBinaryBody(Matcher $matcher): self |
|
89
|
|
|
{ |
|
90
|
|
|
$this->bodyCondition = new BinaryBodyCondition($matcher); |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function andHeader(string $header, Matcher $matcher): self |
|
96
|
|
|
{ |
|
97
|
|
|
$this->headersConditions->setHeaderCondition( |
|
98
|
|
|
new HeaderName($header), |
|
99
|
|
|
new HeaderCondition($matcher) |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function andFormField(string $fieldName, Matcher $matcher): self |
|
106
|
|
|
{ |
|
107
|
|
|
$this->formData->setFieldCondition( |
|
108
|
|
|
new FormFieldName($fieldName), |
|
109
|
|
|
new FormFieldCondition($matcher) |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function andUrl(Matcher $matcher): self |
|
116
|
|
|
{ |
|
117
|
|
|
$this->urlCondition = new UrlCondition($matcher); |
|
118
|
|
|
|
|
119
|
|
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function andScenarioState(string $scenario, string $scenarioState): self |
|
123
|
|
|
{ |
|
124
|
|
|
$this->scenarioName = new ScenarioName($scenario); |
|
125
|
|
|
$this->scenarioIs = new ScenarioState($scenarioState); |
|
126
|
|
|
|
|
127
|
|
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function build(): ConditionsBuilderResult |
|
131
|
|
|
{ |
|
132
|
|
|
return new ConditionsBuilderResult( |
|
133
|
|
|
new RequestConditions( |
|
134
|
|
|
$this->methodCondition, |
|
135
|
|
|
$this->urlCondition, |
|
136
|
|
|
$this->bodyCondition, |
|
137
|
|
|
$this->headersConditions->iterator(), |
|
138
|
|
|
$this->formData->iterator(), |
|
139
|
|
|
$this->scenarioIs |
|
140
|
|
|
), |
|
141
|
|
|
$this->scenarioName |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|