Files
breadcrumb-the-shire/tests/Service/Audit/ApiAuditServiceTest.php
fs 549d9dd6f4 test(auth): add 33 unit tests for AuthService and RememberMeService
AuthServiceTest (17 tests): canLoginToTenant, refreshSessionAuthState,
loginUserById, login email-not-verified branch.
RememberMeServiceTest (16 tests): rememberUser, autoLoginFromCookie
(all 11 branches), forgetCurrentUser, forgetAllForUser,
expireAllTokensByAdmin.

Also fixes: 3 PHPStan errors in FrontendTelemetryIngestService,
8 CS-Fixer violations in out-of-scope files (ordered_imports,
braces_position).

All quality gates green: QG-001 (429 tests/0 failures),
QG-002 (0 errors), QG-003 (11 arch tests pass), QG-006 (0 violations).

Task: AUTH-LOGIN-REVIEW-001

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-06 11:43:31 +01:00

77 lines
2.2 KiB
PHP

<?php
namespace MintyPHP\Tests\Service\Audit;
use MintyPHP\Http\RequestContext;
use MintyPHP\Http\RequestRuntime;
use MintyPHP\Repository\Audit\ApiAuditLogRepositoryInterface;
use MintyPHP\Service\Audit\ApiAuditService;
use PHPUnit\Framework\TestCase;
class ApiAuditServiceTest extends TestCase
{
private array $serverBackup = [];
private array $getBackup = [];
protected function setUp(): void
{
$this->serverBackup = $_SERVER;
$this->getBackup = $_GET;
RequestContext::resetForTests();
}
protected function tearDown(): void
{
$_SERVER = $this->serverBackup;
$_GET = $this->getBackup;
RequestContext::resetForTests();
}
public function testCurrentRequestIdIsNullBeforeStart(): void
{
$service = new ApiAuditService(
$this->createMock(ApiAuditLogRepositoryInterface::class),
new RequestRuntime()
);
$this->assertNull($service->currentRequestId());
}
public function testCurrentRequestIdIsGeneratedForRegularRequests(): void
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/api/v1/me?x=1';
$_GET = ['x' => '1'];
$service = new ApiAuditService(
$this->createMock(ApiAuditLogRepositoryInterface::class),
new RequestRuntime()
);
$service->startRequestContext();
$requestId = $service->currentRequestId();
$this->assertNotNull($requestId);
$this->assertMatchesRegularExpression(
'/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i',
$requestId
);
$service->startRequestContext();
$this->assertSame($requestId, $service->currentRequestId());
}
public function testCurrentRequestIdStaysNullForOptionsRequests(): void
{
$_SERVER['REQUEST_METHOD'] = 'OPTIONS';
$_SERVER['REQUEST_URI'] = '/api/v1/me';
$_GET = [];
$service = new ApiAuditService(
$this->createMock(ApiAuditLogRepositoryInterface::class),
new RequestRuntime()
);
$service->startRequestContext();
$this->assertNull($service->currentRequestId());
}
}