-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeatureContext.php
More file actions
103 lines (90 loc) · 3.18 KB
/
FeatureContext.php
File metadata and controls
103 lines (90 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\Step\Given;
use donatj\MockWebServer\MockWebServer;
use donatj\MockWebServer\RequestInfo;
use donatj\MockWebServer\Response as MockWebServerResponse;
use GuzzleHttp\Psr7\Response;
use Libresign\NextcloudBehat\NextcloudApiContext;
use PHPUnit\Framework\Assert;
require __DIR__ . '/../../vendor/autoload.php';
class FeatureContext extends NextcloudApiContext {
protected MockWebServer $mockServer;
public function __construct(?array $parameters = []) {
parent::__construct($parameters);
$this->mockServer = new MockWebServer();
$this->mockServer->start();
$this->baseUrl = $this->mockServer->getServerRoot() . '/';
}
/**
* @inheritDoc
*/
public function setCurrentUser(string $user): void {
parent::setCurrentUser($user);
Assert::assertEquals($this->currentUser, $user);
}
/**
* @inheritDoc
*/
public function assureUserExists(string $user): void {
parent::assureUserExists($user);
$lastRequest = $this->getLastREquest();
$headers = $lastRequest->getHeaders();
Assert::assertEquals('/ocs/v2.php/cloud/users/test', $lastRequest->getRequestUri());
Assert::assertArrayHasKey('OCS-ApiRequest', $headers);
Assert::assertEquals('true', $headers['OCS-ApiRequest']);
Assert::assertArrayHasKey('Authorization', $headers);
Assert::assertArrayHasKey('Accept', $headers);
Assert::assertEquals('application/json', $headers['Accept']);
}
private function getLastRequest(): RequestInfo {
$lastRequest = $this->mockServer->getLastRequest();
if (!$lastRequest instanceof RequestInfo) {
throw new Exception('Invalid response');
}
return $lastRequest;
}
/**
* @inheritDoc
*/
public function sendRequest(string $verb, string $url, $body = null, array $headers = [], array $options = []): void {
parent::sendRequest($verb, $url, $body, $headers, $options);
$lastRequest = $this->getLastRequest();
// Verb
Assert::assertEquals($verb, $lastRequest->getRequestMethod());
// Url
$actual = preg_replace('/^\/index.php/', '', $lastRequest->getRequestUri());
$url = $this->parseText($url);
Assert::assertEquals($url, $actual);
// Headers
Assert::assertCount(
count($this->requestOptions['headers']),
array_intersect_assoc($lastRequest->getHeaders(), $this->requestOptions['headers'])
);
// Form params
if (array_key_exists('form_params', $this->requestOptions)) {
Assert::assertEquals($this->requestOptions['form_params'], $lastRequest->getParsedInput());
}
}
#[Given('set the response to:')]
public function setTheResponseTo(PyStringNode $response): void {
// Mock response to be equal to body of request
$this->mockServer->setDefaultResponse(new MockWebServerResponse(
(string) $response
));
}
/**
* @inheritDoc
*/
public function theResponseShouldBeAJsonArrayWithTheFollowingMandatoryValues(TableNode $table): void {
$lastRequest = $this->getLastRequest();
$body = json_encode($lastRequest->getParsedInput());
Assert::assertIsString($body);
// Mock response to be equal to body of request
$this->mockServer->setDefaultResponse(new MockWebServerResponse(
$body
));
parent::theResponseShouldBeAJsonArrayWithTheFollowingMandatoryValues($table);
}
}