1
0
Files
breadcrumb-the-shire/modules/audit/tests/Module/Audit/Service/ApiAuditServiceTest.php

77 lines
2.2 KiB
PHP
Raw Normal View History

2026-03-04 15:56:58 +01:00
<?php
namespace MintyPHP\Tests\Module\Audit\Service;
2026-03-04 15:56:58 +01:00
2026-03-05 11:17:42 +01:00
use MintyPHP\Http\RequestContext;
use MintyPHP\Http\RequestRuntime;
use MintyPHP\Module\Audit\Repository\ApiAuditLogRepository;
use MintyPHP\Module\Audit\Service\ApiAuditService;
2026-03-04 15:56:58 +01:00
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
{
2026-03-06 00:44:52 +01:00
$service = new ApiAuditService(
$this->createMock(ApiAuditLogRepository::class),
2026-03-06 00:44:52 +01:00
new RequestRuntime()
);
2026-03-04 15:56:58 +01:00
$this->assertNull($service->currentRequestId());
}
public function testCurrentRequestIdIsGeneratedForRegularRequests(): void
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/api/v1/me?x=1';
$_GET = ['x' => '1'];
2026-03-06 00:44:52 +01:00
$service = new ApiAuditService(
$this->createMock(ApiAuditLogRepository::class),
2026-03-06 00:44:52 +01:00
new RequestRuntime()
);
2026-03-04 15:56:58 +01:00
$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 = [];
2026-03-06 00:44:52 +01:00
$service = new ApiAuditService(
$this->createMock(ApiAuditLogRepository::class),
2026-03-06 00:44:52 +01:00
new RequestRuntime()
);
2026-03-04 15:56:58 +01:00
$service->startRequestContext();
$this->assertNull($service->currentRequestId());
}
}