1 | <?php |
||
10 | class ApiContext extends RawPageContext |
||
11 | { |
||
12 | protected $response; |
||
13 | |||
14 | public function getResponse() |
||
18 | |||
19 | /** |
||
20 | * @Given /^I prepare a (?<method>[A-Za-z]+) request on "(?<page>[^"].*)?"$/ |
||
21 | * @Given /^I prepare a (?<method>[A-Za-z]+) request on the (.*) (?<hasPage>page|resource)$/ |
||
22 | * @Given /^I prepare a (?<method>[A-Za-z]+) request on the (.*) (?<hasPage>page|resource) with:?$/ |
||
23 | */ |
||
24 | public function iPrepareRequest($method, $page, $hasPage = false, TableNode $table = null) |
||
39 | |||
40 | /** |
||
41 | * @Given /^I specified the following request http (?<scheme>[a-z]+) credentials:?$/ |
||
42 | */ |
||
43 | public function iSpecifiedTheFollowingHttpAuthentication(TableNode $credentialsTable, $scheme) |
||
44 | { |
||
45 | $this |
||
46 | ->getRequestBuilder() |
||
47 | ->setCredentials($credentialsTable->getRowsHash()) |
||
48 | ->addSecurityExtension(new HttpExtension($scheme)) |
||
49 | ; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @Given /^I specified the following request oauth credentials:?$/ |
||
54 | */ |
||
55 | public function iSpecifiedTheFollowingOauthCredentials(TableNode $credentialsTable) |
||
56 | { |
||
57 | $this |
||
58 | ->getRequestBuilder() |
||
59 | ->setCredentials($credentialsTable->getRowsHash()) |
||
60 | ->addSecurityExtension(new OauthExtension) |
||
61 | ; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @Given /^I specified the following request headers:?$/ |
||
66 | */ |
||
67 | public function iSpecifiedHeaders(TableNode $table) |
||
71 | |||
72 | /** |
||
73 | * @Given /^I specified the following request queries:?$/ |
||
74 | */ |
||
75 | public function iSpecifiedQueries(TableNode $table) |
||
79 | |||
80 | /** |
||
81 | * @Given /^I specified the following request body:?$/ |
||
82 | */ |
||
83 | public function iSpecifiedTheBody($data) |
||
95 | |||
96 | /** |
||
97 | * @Given /^I specified the following request data:?$/ |
||
98 | */ |
||
99 | public function iSpecifiedData(TableNode $dataTable) |
||
109 | |||
110 | /** |
||
111 | * @Given /^I specified the following request files:?$/ |
||
112 | */ |
||
113 | public function iSpecifiedFiles(TableNode $fileTable) |
||
114 | { |
||
115 | $requestBuilder = $this->getRequestBuilder(); |
||
116 | |||
117 | if ('POST' !== $requestBuilder->getMethod()) { |
||
118 | throw new \RuntimeException('You can\'t send files with a non POST method'); |
||
119 | } |
||
120 | |||
121 | foreach ($fileTable->getRowsHash() as $name => $path) { |
||
122 | $requestBuilder->addFile($name, $path); |
||
123 | } |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @Given /^I specified the following request cookies:?$/ |
||
128 | */ |
||
129 | public function iSpecifiedCookies(TableNode $cookiesTable) |
||
130 | { |
||
131 | $this->getRequestBuilder()->setCookies($cookiesTable->getRowsHash()); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @Given /^I specified the following request options:?$/ |
||
136 | */ |
||
137 | public function iSpecifiedOptions(TableNode $optionsTable) |
||
141 | |||
142 | /** |
||
143 | * @When /^I send the request$/ |
||
144 | */ |
||
145 | public function iSendTheRequest() |
||
153 | |||
154 | /** |
||
155 | * @Then /^I should receive a (?<httpCode>[0-9]+) response$/ |
||
156 | * @Then /^I should receive a (?<httpCode>[0-9]+) (?<shortType>[a-zA-Z]+) response$/ |
||
157 | */ |
||
158 | public function iShouldReceiveResponse($httpCode, $shortType = null) |
||
201 | |||
202 | /** |
||
203 | * @Then /^the response should contains? the following headers:?$/ |
||
204 | */ |
||
205 | public function theResponseShouldContainsHeaders(TableNode $headerTable) |
||
217 | |||
218 | /** |
||
219 | * @Then /^the response should contains? the following json:?$/ |
||
220 | */ |
||
221 | public function theResponsShouldContainsJson($jsonData) |
||
222 | { |
||
223 | if (!is_object($jsonData)) { |
||
224 | throw new \InvalidArgumentException('Invalid json data'); |
||
225 | } |
||
226 | |||
227 | $json = false; |
||
228 | |||
229 | if ($jsonData instanceof PyStringNode) { |
||
230 | $json = json_decode($jsonData->getRaw(), true); |
||
231 | } elseif ($jsonData instanceof TableNode) { |
||
232 | $json = $jsonData->getRowsHash(); |
||
233 | } elseif ($jsonData instanceof \stdClass || true === is_array($jsonData)) { |
||
234 | $json = $jsonData; |
||
235 | } |
||
236 | |||
237 | if (false === $json) { |
||
238 | throw new \InvalidArgumentException(sprintf( |
||
239 | 'Invalid json data class ("%s")', |
||
240 | get_class($jsonData) |
||
241 | )); |
||
242 | } |
||
243 | |||
244 | $expected = json_encode($json); |
||
245 | $real = json_encode($this->response->json()); |
||
246 | |||
247 | $this->getAsserter()->assertEquals( |
||
248 | $expected, |
||
249 | $real, |
||
250 | sprintf("The given json\r\n\r\n%s\r\nis not equal to the expected\r\n\r\n%s", |
||
251 | $real, |
||
252 | $expected |
||
253 | ) |
||
254 | ); |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @Then /^the response should contains?:?$/ |
||
259 | */ |
||
260 | public function theResponseShouldContains(PyStringNode $bodyNode) |
||
264 | } |
||
265 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.