Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
"prefer-stable": true,
"require": {
"php": "^7.4 || ^8.0",
"ext-json": "*",
"ext-dom": "*",
"ext-json": "*",
"ibexa/test-core": "~4.6.x-dev",
"justinrainbow/json-schema": "^5.2",
"symfony/browser-kit": "^5.4",
"symfony/http-foundation": "^5.4",
"symfony/mime": "^5.4",
"symfony/proxy-manager-bridge": "^5.4",
"symfony/translation": "^5.4",
Expand Down
12 changes: 11 additions & 1 deletion src/contracts/BaseRestWebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,17 @@ public function testEndpoint(EndpointRequestDefinition $endpointDefinition): voi
{
$response = $this->performRequest($endpointDefinition);

self::assertResponseIsSuccessful();
$expectedStatusCode = $endpointDefinition->getExpectedStatusCode();
if (null === $expectedStatusCode) {
self::assertResponseIsSuccessful();
} else {
$actualStatusCode = $response->getStatusCode();
self::assertSame(
$actualStatusCode,
$expectedStatusCode,
"Expected HTTP $expectedStatusCode, got HTTP $actualStatusCode status code"
);
}

$content = (string)$response->getContent();
$this->assertResponseIsValid(
Expand Down
20 changes: 19 additions & 1 deletion src/contracts/Request/Value/EndpointRequestDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ final class EndpointRequestDefinition implements Stringable
*/
private ?string $snapshotName;

private ?int $expectedStatusCode;

/**
* @param array<string, string> $headers input headers
* @param string|null $name unique name
* @param int|null $expectedStatusCode expected HTTP status code. If none is given, any successful status code is accepted (>=200 <300)
*/
public function __construct(
string $method,
Expand All @@ -50,7 +53,8 @@ public function __construct(
array $headers = [],
?InputPayload $payload = null,
?string $name = null,
?string $snapshotName = null
?string $snapshotName = null,
?int $expectedStatusCode = null
) {
$this->method = $method;
$this->uri = $uri;
Expand All @@ -60,6 +64,7 @@ public function __construct(
$this->payload = $payload;
$this->name = $name;
$this->snapshotName = $snapshotName;
$this->expectedStatusCode = $expectedStatusCode;
}

public function getMethod(): string
Expand Down Expand Up @@ -149,6 +154,11 @@ public function getSnapshotName(): ?string
return $this->snapshotName;
}

public function getExpectedStatusCode(): ?int
{
return $this->expectedStatusCode;
}

public function withAcceptHeader(?string $acceptHeader): self
{
$endpointDefinition = clone $this;
Expand All @@ -173,6 +183,14 @@ public function withSnapshotName(?string $snapshotName): self
return $endpointDefinition;
}

public function withExpectedStatusCode(?int $expectedStatusCode): self
{
$endpointDefinition = clone $this;
$endpointDefinition->expectedStatusCode = $expectedStatusCode;

return $endpointDefinition;
}

public function __clone()
{
if (null !== $this->payload) {
Expand Down
23 changes: 23 additions & 0 deletions tests/lib/Request/Value/EndpointRequestDefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Ibexa\Contracts\Test\Rest\Request\Value\EndpointRequestDefinition;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Symfony\Component\HttpFoundation\Response;

/**
* @covers \Ibexa\Contracts\Test\Rest\Request\Value\EndpointRequestDefinition
Expand Down Expand Up @@ -74,4 +75,26 @@ public function testWithSnapshotName(): void
self::assertNull($endpointRequestDefinition->getSnapshotName());
self::assertSame($snapshotName, $clonedEndpointRequestDefinition->getSnapshotName());
}

public function testWithExpectedStatusCode(): void
{
$endpointRequestDefinition = new EndpointRequestDefinition('GET', '/foo', null, 'application/xml');
self::assertNull($endpointRequestDefinition->getExpectedStatusCode());

$endpointRequestDefinition = $endpointRequestDefinition->withExpectedStatusCode(Response::HTTP_CREATED);
self::assertSame(Response::HTTP_CREATED, $endpointRequestDefinition->getExpectedStatusCode());

$endpointRequestDefinition = new EndpointRequestDefinition(
'GET',
'/foo',
null,
'application/xml',
[],
null,
null,
null,
Response::HTTP_NO_CONTENT
);
self::assertSame(Response::HTTP_NO_CONTENT, $endpointRequestDefinition->getExpectedStatusCode());
}
}
Loading