-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathApiContext.php
More file actions
120 lines (102 loc) · 3.18 KB
/
ApiContext.php
File metadata and controls
120 lines (102 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
declare(strict_types=1);
namespace CodelyTv\Tests\Shared\Infrastructure\Behat;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Mink\Session;
use Behat\MinkExtension\Context\RawMinkContext;
use CodelyTv\Tests\Shared\Infrastructure\Mink\MinkHelper;
use CodelyTv\Tests\Shared\Infrastructure\Mink\MinkSessionRequestHelper;
use RuntimeException;
final class ApiContext extends RawMinkContext
{
private readonly MinkHelper $sessionHelper;
private readonly MinkSessionRequestHelper $request;
public function __construct(private readonly Session $minkSession)
{
$this->sessionHelper = new MinkHelper($this->minkSession);
$this->request = new MinkSessionRequestHelper(new MinkHelper($minkSession));
}
/**
* @Given I send a :method request to :url
*/
public function iSendARequestTo(string $method, string $url): void
{
$this->request->sendRequest($method, $this->locatePath($url));
}
/**
* @Given I send a :method request to :url with body:
*/
public function iSendARequestToWithBody(string $method, string $url, PyStringNode $body): void
{
$this->request->sendRequestWithPyStringNode($method, $this->locatePath($url), $body);
}
/**
* @Then the response content should be:
*/
public function theResponseContentShouldBe(PyStringNode $expectedResponse): void
{
$expected = $this->sanitizeOutput($expectedResponse->getRaw());
$actual = $this->sanitizeOutput($this->sessionHelper->getResponse());
if ($expected === false || $actual === false) {
throw new RuntimeException('The outputs could not be parsed as JSON');
}
if ($expected !== $actual) {
throw new RuntimeException(
sprintf("The outputs does not match!\n\n-- Expected:\n%s\n\n-- Actual:\n%s", $expected, $actual)
);
}
}
/**
* @Then the response should be empty
*/
public function theResponseShouldBeEmpty(): void
{
$actual = trim($this->sessionHelper->getResponse());
if (!empty($actual)) {
throw new RuntimeException(sprintf("The outputs is not empty, Actual:\n%s", $actual));
}
}
/**
* @Then the response should not be empty
*/
public function theResponseShouldNotBeEmpty(): void
{
$actual = trim($this->sessionHelper->getResponse());
if (empty($actual)) {
throw new RuntimeException(sprintf("The outputs is empty, Actual:\n%s", $actual));
}
}
/**
* @Then print last api response
*/
public function printApiResponse(): void
{
print_r($this->sessionHelper->getResponse());
}
/**
* @Then print response headers
*/
public function printResponseHeaders(): void
{
print_r($this->sessionHelper->getResponseHeaders());
}
/**
* @Then the response status code should be :expectedResponseCode
*/
public function theResponseStatusCodeShouldBe(mixed $expectedResponseCode): void
{
if ($this->minkSession->getStatusCode() !== (int) $expectedResponseCode) {
throw new RuntimeException(
sprintf(
'The status code <%s> does not match the expected <%s>',
$this->minkSession->getStatusCode(),
$expectedResponseCode
)
);
}
}
private function sanitizeOutput(string $output): false|string
{
return json_encode(json_decode(trim($output), true, 512, JSON_THROW_ON_ERROR), JSON_THROW_ON_ERROR);
}
}