|
26 | 26 | require __DIR__ . '/../../vendor/autoload.php'; |
27 | 27 |
|
28 | 28 | use GuzzleHttp\Client; |
| 29 | +use GuzzleHttp\Exception\GuzzleException; |
29 | 30 | use GuzzleHttp\Message\ResponseInterface; |
30 | 31 |
|
31 | 32 | class CardDavContext implements \Behat\Behat\Context\Context { |
@@ -311,4 +312,64 @@ public function theFollowingHttpHeadersShouldBeSet(\Behat\Gherkin\Node\TableNode |
311 | 312 | } |
312 | 313 | } |
313 | 314 | } |
| 315 | + |
| 316 | + /** |
| 317 | + * @When :user sends a create addressbook request to :addressbook on the endpoint :endpoint |
| 318 | + */ |
| 319 | + public function sendsCreateAddressbookRequest(string $user, string $addressbook, string $endpoint) { |
| 320 | + $davUrl = $this->baseUrl . $endpoint . $addressbook; |
| 321 | + $password = ($user === 'admin') ? 'admin' : '123456'; |
| 322 | + |
| 323 | + try { |
| 324 | + $this->response = $this->client->request( |
| 325 | + 'MKCOL', |
| 326 | + $davUrl, |
| 327 | + [ |
| 328 | + 'body' => '<d:mkcol xmlns:card="urn:ietf:params:xml:ns:carddav" |
| 329 | + xmlns:d="DAV:"> |
| 330 | + <d:set> |
| 331 | + <d:prop> |
| 332 | + <d:resourcetype> |
| 333 | + <d:collection />,<card:addressbook /> |
| 334 | + </d:resourcetype>,<d:displayname>' . $addressbook . '</d:displayname> |
| 335 | + </d:prop> |
| 336 | + </d:set> |
| 337 | + </d:mkcol>', |
| 338 | + 'auth' => [ |
| 339 | + $user, |
| 340 | + $password, |
| 341 | + ], |
| 342 | + 'headers' => [ |
| 343 | + 'Content-Type' => 'application/xml;charset=UTF-8', |
| 344 | + ], |
| 345 | + ] |
| 346 | + ); |
| 347 | + } catch (GuzzleException $e) { |
| 348 | + $this->response = $e->getResponse(); |
| 349 | + } |
| 350 | + } |
| 351 | + |
| 352 | + /** |
| 353 | + * @Then The CardDAV HTTP status code should be :code |
| 354 | + * @param int $code |
| 355 | + * @throws \Exception |
| 356 | + */ |
| 357 | + public function theCarddavHttpStatusCodeShouldBe($code) { |
| 358 | + if ((int)$code !== $this->response->getStatusCode()) { |
| 359 | + throw new \Exception( |
| 360 | + sprintf( |
| 361 | + 'Expected %s got %s', |
| 362 | + (int)$code, |
| 363 | + $this->response->getStatusCode() |
| 364 | + ) |
| 365 | + ); |
| 366 | + } |
| 367 | + |
| 368 | + $body = $this->response->getBody()->getContents(); |
| 369 | + if ($body && substr($body, 0, 1) === '<') { |
| 370 | + $reader = new Sabre\Xml\Reader(); |
| 371 | + $reader->xml($body); |
| 372 | + $this->responseXml = $reader->parse(); |
| 373 | + } |
| 374 | + } |
314 | 375 | } |
0 commit comments