Tests / PHP 8.4 (push) Failing after 1m26s
Drop support for PHP 8.3 and lower. The package now declares >=8.4, and CI runs only on 8.4. - composer.json: require php >=8.4. - mago.toml: php-version = "8.4", matching the minimum supported version. - phpstan.neon: phpVersion 80400. - Upgrade phpstan/phpstan to ^2.3: 1.x cannot parse `new Foo()->bar()`, which the 8.4 formatter now emits. - Narrow ArrayGateway::count() to int, matching what the fixture actually returns. - CI: single PHP 8.4 job using the php:8.4-cli container instead of setup-php, which failed with a TLS handshake timeout on the runner. BREAKING CHANGE: PHP 8.4 is now required.
114 lines
3.4 KiB
PHP
114 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Camelia\Controllers\Tests\Unit\Middleware;
|
|
|
|
use Camelia\Controllers\JsonRequest;
|
|
use Camelia\Controllers\Middleware\ContentNegotiation;
|
|
use Camelia\Controllers\Request;
|
|
use Camelia\Controllers\Response;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class ContentNegotiationTest extends TestCase
|
|
{
|
|
private static function ok(Request $request): Response
|
|
{
|
|
return new Response(['ok' => true]);
|
|
}
|
|
|
|
public function testAddsContentType(): void
|
|
{
|
|
$response = new ContentNegotiation()->process(new JsonRequest(), self::ok(...));
|
|
|
|
self::assertSame(200, $response->status());
|
|
self::assertSame('application/json; charset=utf-8', $response->headers()['Content-Type']);
|
|
}
|
|
|
|
public function testMissingAcceptHeaderIsAccepted(): void
|
|
{
|
|
$response = new ContentNegotiation()->process(new JsonRequest(), self::ok(...));
|
|
|
|
self::assertSame(200, $response->status());
|
|
}
|
|
|
|
public function testJsonAcceptIsAccepted(): void
|
|
{
|
|
$response = new ContentNegotiation()->process(
|
|
new JsonRequest(headers: ['accept' => 'application/json']),
|
|
self::ok(...),
|
|
);
|
|
|
|
self::assertSame(200, $response->status());
|
|
}
|
|
|
|
public function testBrowserAcceptIsAccepted(): void
|
|
{
|
|
$accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
|
|
|
|
$response = new ContentNegotiation()->process(
|
|
new JsonRequest(headers: ['accept' => $accept]),
|
|
self::ok(...),
|
|
);
|
|
|
|
self::assertSame(200, $response->status());
|
|
}
|
|
|
|
public function testIncompatibleAcceptIs406(): void
|
|
{
|
|
$response = new ContentNegotiation()->process(
|
|
new JsonRequest(headers: ['accept' => 'application/xml']),
|
|
static function (): Response {
|
|
self::fail('The handler must not run when the client rejects JSON.');
|
|
},
|
|
);
|
|
|
|
self::assertSame(406, $response->status());
|
|
self::assertSame('not_acceptable', $response->body()['code']);
|
|
}
|
|
|
|
public function testZeroQualityIsRejected(): void
|
|
{
|
|
$response = new ContentNegotiation()->process(
|
|
new JsonRequest(headers: ['accept' => 'application/json;q=0']),
|
|
static function (): Response {
|
|
self::fail('A q=0 entry means not acceptable.');
|
|
},
|
|
);
|
|
|
|
self::assertSame(406, $response->status());
|
|
}
|
|
|
|
public function testNonZeroQualityIsAccepted(): void
|
|
{
|
|
$response = new ContentNegotiation()->process(
|
|
new JsonRequest(headers: ['accept' => 'application/json;q=0.5']),
|
|
self::ok(...),
|
|
);
|
|
|
|
self::assertSame(200, $response->status());
|
|
}
|
|
|
|
public function testExistingContentTypeIsPreserved(): void
|
|
{
|
|
$response = new ContentNegotiation()->process(
|
|
new JsonRequest(),
|
|
static fn(): Response => new Response(['ok' => true], 200, [
|
|
'Content-Type' => 'application/problem+json',
|
|
]),
|
|
);
|
|
|
|
self::assertSame('application/problem+json', $response->headers()['Content-Type']);
|
|
}
|
|
|
|
public function testContentTypeCanBeDisabled(): void
|
|
{
|
|
$response = new ContentNegotiation(addContentType: false)->process(
|
|
new JsonRequest(),
|
|
self::ok(...),
|
|
);
|
|
|
|
self::assertFalse($response->hasHeader('Content-Type'));
|
|
}
|
|
}
|