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.
101 lines
2.8 KiB
PHP
101 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Camelia\Controllers\Tests\Unit\Security;
|
|
|
|
use Camelia\Controllers\JsonRequest;
|
|
use Camelia\Controllers\Request;
|
|
use Camelia\Controllers\Response;
|
|
use Camelia\Controllers\Security\RequireRole;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class RequireRoleTest extends TestCase
|
|
{
|
|
private static function passThrough(Request $request): Response
|
|
{
|
|
return new Response(['ok' => true]);
|
|
}
|
|
|
|
private static function withUser(mixed $user): Request
|
|
{
|
|
return new JsonRequest()->withAttribute('user', $user);
|
|
}
|
|
|
|
public function testNoIdentityIs401(): void
|
|
{
|
|
$response = new RequireRole(['admin'])->process(new JsonRequest(), self::passThrough(...));
|
|
|
|
self::assertSame(401, $response->status());
|
|
self::assertStringContainsString(
|
|
'Authentication is required',
|
|
$response->body()['message'],
|
|
);
|
|
}
|
|
|
|
public function testMissingRoleIs401(): void
|
|
{
|
|
$middleware = new RequireRole(['admin']);
|
|
|
|
$response = $middleware->process(
|
|
self::withUser(['id' => 1, 'roles' => ['vendedor']]),
|
|
self::passThrough(...),
|
|
);
|
|
|
|
self::assertSame(401, $response->status());
|
|
self::assertStringContainsString('admin', $response->body()['message']);
|
|
}
|
|
|
|
public function testGrantedRolePasses(): void
|
|
{
|
|
$middleware = new RequireRole(['admin', 'gerente']);
|
|
|
|
$response = $middleware->process(
|
|
self::withUser(['id' => 1, 'roles' => ['gerente']]),
|
|
self::passThrough(...),
|
|
);
|
|
|
|
self::assertSame(200, $response->status());
|
|
}
|
|
|
|
public function testSingleRoleStringIsAccepted(): void
|
|
{
|
|
$middleware = new RequireRole(['admin']);
|
|
|
|
$response = $middleware->process(
|
|
self::withUser(['role' => 'admin']),
|
|
self::passThrough(...),
|
|
);
|
|
|
|
self::assertSame(200, $response->status());
|
|
}
|
|
|
|
public function testMalformedRolesFailClosed(): void
|
|
{
|
|
$middleware = new RequireRole(['admin']);
|
|
|
|
$response = $middleware->process(
|
|
self::withUser(['roles' => 'admin']),
|
|
self::passThrough(...),
|
|
);
|
|
|
|
// A string is a valid single role; the point is that no exception escapes.
|
|
self::assertSame(200, $response->status());
|
|
|
|
$broken = $middleware->process(
|
|
self::withUser(['roles' => [42, null]]),
|
|
self::passThrough(...),
|
|
);
|
|
self::assertSame(401, $broken->status());
|
|
}
|
|
|
|
public function testIdentityMustBeArray(): void
|
|
{
|
|
$middleware = new RequireRole(['admin']);
|
|
|
|
$response = $middleware->process(self::withUser('admin'), self::passThrough(...));
|
|
|
|
self::assertSame(401, $response->status());
|
|
}
|
|
}
|