|
1
|
|
|
<?php |
|
2
|
|
|
/******************************************************************************* |
|
3
|
|
|
* This file is part of the GraphQL Bundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) YnloUltratech <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
******************************************************************************/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Behat\Context; |
|
12
|
|
|
|
|
13
|
|
|
use Behat\Behat\Context\Context; |
|
14
|
|
|
use PHPUnit\Framework\Assert; |
|
15
|
|
|
use Ynlo\GraphQLBundle\Behat\Gherkin\YamlStringNode; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* A set of assertion methods. |
|
19
|
|
|
*/ |
|
20
|
|
|
final class AssertContext implements Context |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Compare equality of two values. |
|
24
|
|
|
* |
|
25
|
|
|
* Example: Then "{response.data.title}" should be equal to "Welcome" |
|
26
|
|
|
* Example: Then "{response.data.title}" should be equal to "{@post1.getTitle()}" |
|
27
|
|
|
* Example: Then "{response.data.id}" should be equal to "#post1" |
|
28
|
|
|
* |
|
29
|
|
|
* @Then /^"([^"]*)" should be equal to "([^"]*)"$/ |
|
30
|
|
|
*/ |
|
31
|
|
|
public function shouldBeEqualTo($actual, $expected) |
|
32
|
|
|
{ |
|
33
|
|
|
Assert::assertEquals($expected, $actual); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Compare NOT equality of two values. |
|
38
|
|
|
* |
|
39
|
|
|
* Example: Then "{response.data.title}" should not be equal to "Welcome" |
|
40
|
|
|
* Example: Then "{response.data.title}" should not be equal to "{@post1.getTitle()}" |
|
41
|
|
|
* Example: Then "{response.data.id}" should not be equal to "#post1" |
|
42
|
|
|
* |
|
43
|
|
|
* @Then /^"([^"]*)" should not be equal to "([^"]*)"$/ |
|
44
|
|
|
*/ |
|
45
|
|
|
public function shouldNotBeEqualTo($actual, $expected) |
|
46
|
|
|
{ |
|
47
|
|
|
Assert::assertNotEquals($expected, $actual); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Asserts that a value is greater than another value. |
|
52
|
|
|
* |
|
53
|
|
|
* Example: Then "{response.data.amount}" should be greater than 20 |
|
54
|
|
|
* |
|
55
|
|
|
* @Then /^"([^"]*)" should be greater than ([^"]*)$/ |
|
56
|
|
|
*/ |
|
57
|
|
|
public function shouldBeGreaterThan($actual, $expected) |
|
58
|
|
|
{ |
|
59
|
|
|
Assert::assertGreaterThan($expected, $actual); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
*Asserts that a value is greater than or equal to another value. |
|
64
|
|
|
* |
|
65
|
|
|
* Example: Then "{response.data.amount}" should be greater or equal to 20 |
|
66
|
|
|
* |
|
67
|
|
|
* @Then /^"([^"]*)" should be greater or equal to ([^"]*)$/ |
|
68
|
|
|
*/ |
|
69
|
|
|
public function shouldBeGreaterOrEqual($actual, $expected) |
|
70
|
|
|
{ |
|
71
|
|
|
Assert::assertGreaterThanOrEqual($expected, $actual); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Asserts that a value is less than another value. |
|
76
|
|
|
* |
|
77
|
|
|
* Example: Then "{response.data.amount}" should be less than 20 |
|
78
|
|
|
* |
|
79
|
|
|
* @Then /^"([^"]*)" should be less than ([^"]*)$/ |
|
80
|
|
|
*/ |
|
81
|
|
|
public function shouldBeLessThan($actual, $expected) |
|
82
|
|
|
{ |
|
83
|
|
|
Assert::assertLessThan($expected, $actual); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Asserts that a value is smaller than or equal to another value. |
|
88
|
|
|
* |
|
89
|
|
|
* Example: Then "{response.data.amount}" should be greater or equal to 20 |
|
90
|
|
|
* |
|
91
|
|
|
* @Then /^"([^"]*)" should be less or equal to ([^"]*)$/ |
|
92
|
|
|
*/ |
|
93
|
|
|
public function shouldBeLessOrEqual($actual, $expected) |
|
94
|
|
|
{ |
|
95
|
|
|
Assert::assertLessThanOrEqual($expected, $actual); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Asserts that a condition is true. |
|
100
|
|
|
* |
|
101
|
|
|
* Example: Then "{response.data.enabled}" should be true |
|
102
|
|
|
* |
|
103
|
|
|
* @Then /^"([^"]*)" should be true$/ |
|
104
|
|
|
*/ |
|
105
|
|
|
public function shouldBeTrue($value) |
|
106
|
|
|
{ |
|
107
|
|
|
Assert::assertTrue($value); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Asserts that a condition is false. |
|
112
|
|
|
* |
|
113
|
|
|
* Example: Then "{response.data.enabled}" should be false |
|
114
|
|
|
* |
|
115
|
|
|
* @Then /^"([^"]*)" should be false$/ |
|
116
|
|
|
*/ |
|
117
|
|
|
public function shouldBeFalse($actual) |
|
118
|
|
|
{ |
|
119
|
|
|
Assert::assertFalse($actual); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Asserts that a value is empty |
|
124
|
|
|
* |
|
125
|
|
|
* Example: Then "{response.data.body}" should be empty |
|
126
|
|
|
* |
|
127
|
|
|
* @Then /^"([^"]*)" should be empty$/ |
|
128
|
|
|
*/ |
|
129
|
|
|
public function shouldBeEmpty($value) |
|
130
|
|
|
{ |
|
131
|
|
|
Assert::assertEmpty($value); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Asserts that a value is NOT empty |
|
136
|
|
|
* |
|
137
|
|
|
* Example: Then "{response.data.title}" should not be empty |
|
138
|
|
|
* |
|
139
|
|
|
* @Then /^"([^"]*)" should not be empty$/ |
|
140
|
|
|
*/ |
|
141
|
|
|
public function shouldNotBeEmpty($value) |
|
142
|
|
|
{ |
|
143
|
|
|
Assert::assertNotEmpty($value); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Asserts that a value is null |
|
148
|
|
|
* |
|
149
|
|
|
* Example: Then "{response.data.title}" should be null |
|
150
|
|
|
* |
|
151
|
|
|
* @Then /^"([^"]*)" should be null$/ |
|
152
|
|
|
*/ |
|
153
|
|
|
public function shouldBeNull($value) |
|
154
|
|
|
{ |
|
155
|
|
|
Assert::assertNull($value); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Asserts that a value is NOT null |
|
160
|
|
|
* |
|
161
|
|
|
* Example: Then "{response.data.title}" should not be null |
|
162
|
|
|
* |
|
163
|
|
|
* @Then /^"([^"]*)" should not be null$/ |
|
164
|
|
|
*/ |
|
165
|
|
|
public function shouldNotBeNull($value) |
|
166
|
|
|
{ |
|
167
|
|
|
Assert::assertNotNull($value); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Asserts that a condition match Then expression |
|
172
|
|
|
* |
|
173
|
|
|
* Example: Then "{response.data.number}" should match /^\d+$/ |
|
174
|
|
|
* |
|
175
|
|
|
* @Then /^"([^"]*)" should match "([^"]*)"$/ |
|
176
|
|
|
*/ |
|
177
|
|
|
public function shouldMatchRegExp($value, $exp) |
|
178
|
|
|
{ |
|
179
|
|
|
Assert::assertRegExp($exp, $value); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Asserts that a condition NOT match Then expression |
|
184
|
|
|
* |
|
185
|
|
|
* Example: Then "{response.data.number}" should not match /^\w+$/ |
|
186
|
|
|
* |
|
187
|
|
|
* @Then /^"([^"]*)" should not match "([^"]*)"$/ |
|
188
|
|
|
*/ |
|
189
|
|
|
public function shouldNotMatchRegExp($value, $exp) |
|
190
|
|
|
{ |
|
191
|
|
|
Assert::assertNotRegExp($exp, $value); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Asserts that a haystack contains a needle. |
|
196
|
|
|
* |
|
197
|
|
|
* Example: Then "{response.data.tags}" should contains "php" |
|
198
|
|
|
* |
|
199
|
|
|
* @Then /^"([^"]*)" should contains "([^"]*)"$/ |
|
200
|
|
|
*/ |
|
201
|
|
|
public function shouldContains($haystack, $needle) |
|
202
|
|
|
{ |
|
203
|
|
|
Assert::assertContains($needle, $haystack); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Asserts that a haystack NOT contains a needle. |
|
208
|
|
|
* |
|
209
|
|
|
* Example: Then "{response.data.tags}" should not contains "javascript" |
|
210
|
|
|
* |
|
211
|
|
|
* @Then /^"([^"]*)" should not contains "([^"]*)"$/ |
|
212
|
|
|
*/ |
|
213
|
|
|
public function shouldNotContains($haystack, $needle) |
|
214
|
|
|
{ |
|
215
|
|
|
Assert::assertNotContains($needle, $haystack); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Asserts the number of elements of an array, Countable or Traversable. |
|
220
|
|
|
* |
|
221
|
|
|
* Example: Then "{response.data.tags}" should have "3" items |
|
222
|
|
|
* |
|
223
|
|
|
* @Then /^"([^"]*)" should have ([^"]*) items$/ |
|
224
|
|
|
*/ |
|
225
|
|
|
public function shouldCountElements($haystack, $count) |
|
226
|
|
|
{ |
|
227
|
|
|
Assert::assertCount($count, $haystack); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Asserts NOT the number of elements of an array, Countable or Traversable. |
|
232
|
|
|
* |
|
233
|
|
|
* Example: Then "{response.data.tags}" should don't have "3" items |
|
234
|
|
|
* |
|
235
|
|
|
* @Then /^"([^"]*)" should don't have ([^"]*) items$/ |
|
236
|
|
|
*/ |
|
237
|
|
|
public function shouldNotCountElements($haystack, $count) |
|
238
|
|
|
{ |
|
239
|
|
|
Assert::assertNotCount($count, $haystack); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Asserts that an array has a specified subset. |
|
244
|
|
|
* |
|
245
|
|
|
* Example: And "{search('data.posts[*].title', response)}" should contains this subset: |
|
246
|
|
|
* """ |
|
247
|
|
|
* - Welcome |
|
248
|
|
|
* - "{@post1.getTitle()}" |
|
249
|
|
|
* """ |
|
250
|
|
|
* |
|
251
|
|
|
* @Then /^"([^"]*)" should contains this subset:$/ |
|
252
|
|
|
*/ |
|
253
|
|
|
public function shouldContainsASubset($actual, YamlStringNode $subset) |
|
254
|
|
|
{ |
|
255
|
|
|
Assert::assertArraySubset($subset->toArray(), $actual); |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|