147 lines
4.1 KiB
PHP
147 lines
4.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Service;
|
|
|
|
use App\Service\ErrorCodeGenerator;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
|
|
class ErrorCodeGeneratorTest extends TestCase
|
|
{
|
|
private RequestStack $requestStack;
|
|
private ErrorCodeGenerator $service;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->requestStack = new RequestStack();
|
|
$this->service = new ErrorCodeGenerator($this->requestStack);
|
|
}
|
|
|
|
public function testReturnsNullWithoutRequest(): void
|
|
{
|
|
$result = $this->service->getErrorCode();
|
|
|
|
$this->assertNull($result);
|
|
}
|
|
|
|
public function testReturnsNullWithoutRequestId(): void
|
|
{
|
|
$request = new Request();
|
|
$this->requestStack->push($request);
|
|
|
|
$result = $this->service->getErrorCode();
|
|
|
|
$this->assertNull($result);
|
|
}
|
|
|
|
public function testGeneratesCodeWithRequestId(): void
|
|
{
|
|
$request = new Request();
|
|
$request->attributes->set('request_id', 'abc123');
|
|
$this->requestStack->push($request);
|
|
|
|
$result = $this->service->getErrorCode();
|
|
|
|
$this->assertNotNull($result);
|
|
$this->assertMatchesRegularExpression('/^E-[A-Z0-9]{8}$/', $result);
|
|
}
|
|
|
|
public function testGeneratesConsistentCodeForSameRequestId(): void
|
|
{
|
|
$request = new Request();
|
|
$request->attributes->set('request_id', 'abc123');
|
|
$this->requestStack->push($request);
|
|
|
|
$result1 = $this->service->getErrorCode();
|
|
$result2 = $this->service->getErrorCode();
|
|
|
|
$this->assertSame($result1, $result2);
|
|
}
|
|
|
|
public function testGeneratesDifferentCodesForDifferentRequestIds(): void
|
|
{
|
|
$request1 = new Request();
|
|
$request1->attributes->set('request_id', 'abc123');
|
|
$this->requestStack->push($request1);
|
|
|
|
$service1 = new ErrorCodeGenerator($this->requestStack);
|
|
$result1 = $service1->getErrorCode();
|
|
|
|
$this->requestStack->pop();
|
|
|
|
$request2 = new Request();
|
|
$request2->attributes->set('request_id', 'xyz789');
|
|
$this->requestStack->push($request2);
|
|
|
|
$service2 = new ErrorCodeGenerator($this->requestStack);
|
|
$result2 = $service2->getErrorCode();
|
|
|
|
$this->assertNotSame($result1, $result2);
|
|
}
|
|
|
|
public function testSequenceSuffixesProduceSameCode(): void
|
|
{
|
|
$request1 = new Request();
|
|
$request1->attributes->set('request_id', 'abc123_1');
|
|
$this->requestStack->push($request1);
|
|
|
|
$service1 = new ErrorCodeGenerator($this->requestStack);
|
|
$result1 = $service1->getErrorCode();
|
|
|
|
$this->requestStack->pop();
|
|
|
|
$request2 = new Request();
|
|
$request2->attributes->set('request_id', 'abc123_2');
|
|
$this->requestStack->push($request2);
|
|
|
|
$service2 = new ErrorCodeGenerator($this->requestStack);
|
|
$result2 = $service2->getErrorCode();
|
|
|
|
$this->assertSame($result1, $result2);
|
|
}
|
|
|
|
public function testSequenceSuffixMatchesBaseId(): void
|
|
{
|
|
$request1 = new Request();
|
|
$request1->attributes->set('request_id', 'abc123');
|
|
$this->requestStack->push($request1);
|
|
|
|
$service1 = new ErrorCodeGenerator($this->requestStack);
|
|
$result1 = $service1->getErrorCode();
|
|
|
|
$this->requestStack->pop();
|
|
|
|
$request2 = new Request();
|
|
$request2->attributes->set('request_id', 'abc123_5');
|
|
$this->requestStack->push($request2);
|
|
|
|
$service2 = new ErrorCodeGenerator($this->requestStack);
|
|
$result2 = $service2->getErrorCode();
|
|
|
|
$this->assertSame($result1, $result2);
|
|
}
|
|
|
|
public function testHasErrorReturnsFalseByDefault(): void
|
|
{
|
|
$this->assertFalse($this->service->hasError());
|
|
}
|
|
|
|
public function testMarkErrorOccurredSetsFlag(): void
|
|
{
|
|
$this->service->markErrorOccurred();
|
|
|
|
$this->assertTrue($this->service->hasError());
|
|
}
|
|
|
|
public function testHasErrorRemainsTrueAfterMultipleCalls(): void
|
|
{
|
|
$this->service->markErrorOccurred();
|
|
$this->service->markErrorOccurred();
|
|
|
|
$this->assertTrue($this->service->hasError());
|
|
}
|
|
}
|