1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPKitchen\CodeSpecsCore\Mixin; |
4
|
|
|
|
5
|
|
|
use PHPKitchen\CodeSpecsCore\Contract\TestGuy; |
6
|
|
|
use PHPKitchen\CodeSpecsCore\Directive\Wait; |
7
|
|
|
use PHPKitchen\CodeSpecsCore\Expectation\Dispatcher\DelayedDispatcher; |
8
|
|
|
use PHPKitchen\CodeSpecsCore\Expectation\Dispatcher\Dispatcher; |
9
|
|
|
use PHPKitchen\CodeSpecsCore\Expectation\Internal\StepsList; |
10
|
|
|
use PHPKitchen\CodeSpecsCore\Expectation\Matcher\ArrayMatcher; |
11
|
|
|
use PHPKitchen\CodeSpecsCore\Expectation\Matcher\BooleanMatcher; |
12
|
|
|
use PHPKitchen\CodeSpecsCore\Expectation\Matcher\ClassMatcher; |
13
|
|
|
use PHPKitchen\CodeSpecsCore\Expectation\Matcher\DirectoryMatcher; |
14
|
|
|
use PHPKitchen\CodeSpecsCore\Expectation\Matcher\FileMatcher; |
15
|
|
|
use PHPKitchen\CodeSpecsCore\Expectation\Matcher\NumberMatcher; |
16
|
|
|
use PHPKitchen\CodeSpecsCore\Expectation\Matcher\ObjectMatcher; |
17
|
|
|
use PHPKitchen\CodeSpecsCore\Expectation\Matcher\StringMatcher; |
18
|
|
|
use PHPKitchen\CodeSpecsCore\Expectation\Matcher\ValueMatcher; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Represents common expectation methods that can be used in test guy implementation. |
22
|
|
|
* |
23
|
|
|
* @codeCoverageIgnore |
24
|
|
|
* |
25
|
|
|
* @package PHPKitchen\CodeSpecsCore\Mixins |
26
|
|
|
* @author Dmitry Kolodko <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
trait TestGuyMethods { |
29
|
|
|
/** |
30
|
|
|
* @var StepsList |
31
|
|
|
*/ |
32
|
|
|
private $steps; |
33
|
|
|
/** |
34
|
|
|
* @var \PHPUnit\Framework\Test |
35
|
|
|
*/ |
36
|
|
|
protected $context; |
37
|
|
|
protected $variableName = ''; |
38
|
|
|
|
39
|
|
|
//region ----------------------- SPECIFICATION METHODS ----------------------- |
40
|
|
|
|
41
|
|
|
public function init() { |
42
|
|
|
$this->steps = StepsList::getInstance(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Specifies scenario test guy is working on. |
47
|
|
|
* |
48
|
|
|
* @param string $scenario scenario name. |
49
|
|
|
* Scenario should be a logical ending of "I describe ". For example: "process of user registration". |
50
|
|
|
* Such scenario would result in "I describe process of user registration" output in console. |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
|
|
public function describe(string $scenario): TestGuy { |
54
|
|
|
$this->steps->add('I describe ' . $scenario); |
55
|
|
|
return $this; |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Specifies what test guy expects from a set of matchers that would be defined next in the |
60
|
|
|
* specification. |
61
|
|
|
* |
62
|
|
|
* @param string $expectation expectation text. |
63
|
|
|
* Expectation should be a logical ending of "I expect that ". For example: "user is added to the DB". |
64
|
|
|
* Such scenario would result in "I expect that user is added to the DB" output in console. |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
|
|
public function expectThat(string $expectation): TestGuy { |
68
|
|
|
$this->steps->add('I expect that ' . $expectation); |
69
|
|
|
|
70
|
|
|
return $this; |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Specifies what test guy expects from a set of matchers that would be defined next in the |
75
|
|
|
* specification. |
76
|
|
|
* |
77
|
|
|
* @param string $expectation expectation text. |
78
|
|
|
* Expectation should be a logical ending of "I expect to ". For example: "see user in the DB". |
79
|
|
|
* Such scenario would result in "I expect to see user in the DB" output in console. |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
|
|
public function expectTo(string $expectation): TestGuy { |
83
|
|
|
$this->steps->add('I expect to ' . $expectation); |
84
|
|
|
return $this; |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Specifies what test guy expects from a set of matchers that would be defined next in the |
89
|
|
|
* specification. |
90
|
|
|
* |
91
|
|
|
* @param string $expectation expectation text. |
92
|
|
|
* Expectation should be a logical ending of "I expect to ". For example: "see user in the DB". |
93
|
|
|
* Such scenario would result in "I expect to see user in the DB" output in console. |
94
|
|
|
* @param callable $verificationSteps callable function with following definition "function (TestGuy $I) { ..." that contains a group of |
95
|
|
|
* expectations united by one verification topic. All of the expectations would be executed once they |
96
|
|
|
* are defined. |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
|
|
public function verifyThat(string $expectation, callable $verificationSteps = null): TestGuy { |
100
|
|
|
$this->steps->add('I verify that ' . $expectation); |
101
|
|
|
if ($verificationSteps) { |
102
|
|
|
$verificationSteps($this); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this; |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Specifies name of a variable test guy would check. |
110
|
|
|
* |
111
|
|
|
* @param string $variableName name of a variable to look at. |
112
|
|
|
* @return TestGuy |
113
|
|
|
*/ |
114
|
|
|
public function lookAt(string $variableName): TestGuy { |
115
|
|
|
$this->variableName = $variableName; |
116
|
|
|
return $this; |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Creates runtime matcher that you can use to perform typical asserts. |
122
|
|
|
* Runtime matcher is an object that represents a set of asserts from a typical matcher that |
123
|
|
|
* aren't executed at a time they were defined but would be executed every time runtime matcher object |
124
|
|
|
* would be called as a function with one argument - value to assert. |
125
|
|
|
* |
126
|
|
|
* For example: |
127
|
|
|
* <code> |
128
|
|
|
* $userHasName = $I->match('user')->isArray()->isNotEmpty()->hasKey('name'); |
129
|
|
|
* $userHasName($admin); |
130
|
|
|
* $userHasName($member); |
131
|
|
|
* </code> |
132
|
|
|
* |
133
|
|
|
* @param string $variableName name of a variable to look at. |
134
|
|
|
* @return \PHPKitchen\CodeSpecsCore\Expectation\Dispatcher\DelayedDispatcher |
135
|
|
|
*/ |
136
|
|
|
public function match(string $variableName): DelayedDispatcher { |
137
|
|
|
$this->variableName = $variableName; |
138
|
|
|
return $this->createDispatcher(DelayedDispatcher::class, null); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Stops execution for specified number of units of time. |
144
|
|
|
* |
145
|
|
|
* @param int $numberOfTimeUnits number of units of time. |
146
|
|
|
* {@link Wait} specifies what unit should be used. |
147
|
|
|
* |
148
|
|
|
* @return Wait |
149
|
|
|
*/ |
150
|
|
|
public function wait($numberOfTimeUnits): Wait { |
151
|
|
|
return new Wait($numberOfTimeUnits, $this->steps); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Starts a chain of asserts from {@link ValueMatcher}. |
156
|
|
|
* |
157
|
|
|
* @param mixed $variable variable to be tested |
158
|
|
|
* @return \PHPKitchen\CodeSpecsCore\Expectation\Matcher\ValueMatcher |
159
|
|
|
*/ |
160
|
|
|
public function see($variable): ValueMatcher { |
161
|
|
|
return $this->dispatch($variable)->isMixed(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Starts a chain of asserts from {@link StringMatcher}. |
166
|
|
|
* |
167
|
|
|
* @param string $variable variable to be tested |
168
|
|
|
* @return \PHPKitchen\CodeSpecsCore\Expectation\Matcher\StringMatcher |
169
|
|
|
*/ |
170
|
|
|
public function seeString($string): StringMatcher { |
171
|
|
|
return $this->dispatch($string)->isString(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Starts a chain of asserts from {@link ArrayMatcher}. |
176
|
|
|
* |
177
|
|
|
* @param array|\ArrayAccess $variable variable to be tested |
178
|
|
|
* @return \PHPKitchen\CodeSpecsCore\Expectation\Matcher\ArrayMatcher |
179
|
|
|
*/ |
180
|
|
|
public function seeArray($variable): ArrayMatcher { |
181
|
|
|
return $this->dispatch($variable)->isArray(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Starts a chain of asserts from {@link BooleanMatcher}. |
186
|
|
|
* |
187
|
|
|
* @param boolean $variable variable to be tested |
188
|
|
|
* @return \PHPKitchen\CodeSpecsCore\Expectation\Matcher\BooleanMatcher |
189
|
|
|
*/ |
190
|
|
|
public function seeBool($variable): BooleanMatcher { |
191
|
|
|
return $this->dispatch($variable)->isBoolean(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Starts a chain of asserts from {@link NumberMatcher}. |
196
|
|
|
* |
197
|
|
|
* @param int|float $variable variable to be tested |
198
|
|
|
* @return \PHPKitchen\CodeSpecsCore\Expectation\Matcher\NumberMatcher |
199
|
|
|
*/ |
200
|
|
|
public function seeNumber($variable): NumberMatcher { |
201
|
|
|
return $this->dispatch($variable)->isNumber(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Starts a chain of asserts from {@link ObjectMatcher}. |
206
|
|
|
* |
207
|
|
|
* @param object $variable variable to be tested |
208
|
|
|
* @return \PHPKitchen\CodeSpecsCore\Expectation\Matcher\ObjectMatcher |
209
|
|
|
*/ |
210
|
|
|
public function seeObject($variable): ObjectMatcher { |
211
|
|
|
return $this->dispatch($variable)->isObject(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Starts a chain of asserts from {@link ClassMatcher}. |
216
|
|
|
* |
217
|
|
|
* @param string $variable variable to be tested |
218
|
|
|
* @return \PHPKitchen\CodeSpecsCore\Expectation\Matcher\ClassMatcher |
219
|
|
|
*/ |
220
|
|
|
public function seeClass($variable): ClassMatcher { |
221
|
|
|
return $this->dispatch($variable)->isClass(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Starts a chain of asserts from {@link FileMatcher}. |
226
|
|
|
* |
227
|
|
|
* @param string $variable variable to be tested |
228
|
|
|
* @return \PHPKitchen\CodeSpecsCore\Expectation\Matcher\FileMatcher |
229
|
|
|
*/ |
230
|
|
|
public function seeFile($variable): FileMatcher { |
231
|
|
|
return $this->dispatch($variable)->isFile(); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Starts a chain of asserts from {@link DirectoryMatcher}. |
236
|
|
|
* |
237
|
|
|
* @param string $variable variable to be tested |
238
|
|
|
* @return \PHPKitchen\CodeSpecsCore\Expectation\Matcher\DirectoryMatcher |
239
|
|
|
*/ |
240
|
|
|
public function seeDirectory($variable): DirectoryMatcher { |
241
|
|
|
return $this->dispatch($variable)->isDirectory(); |
242
|
|
|
} |
243
|
|
|
//endregion |
244
|
|
|
|
245
|
|
|
//region ----------------------- UTIL METHODS ----------------------- |
246
|
|
|
|
247
|
|
|
public function clearSteps() { |
248
|
|
|
$this->steps->clear(); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
private function dispatch($actualValue): Dispatcher { |
252
|
|
|
return $this->createDispatcher(Dispatcher::class, $actualValue); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
private function createDispatcher($class, $actualValue): Dispatcher { |
256
|
|
|
$dispatcher = new $class($this->context, $actualValue, $this->variableName); |
257
|
|
|
$this->variableName = ''; |
258
|
|
|
return $dispatcher; |
259
|
|
|
} |
260
|
|
|
//endregion |
261
|
|
|
} |