requestStack = new RequestStack(); $this->service = new ErrorCodeService($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 ErrorCodeService($this->requestStack); $result1 = $service1->getErrorCode(); $this->requestStack->pop(); $request2 = new Request(); $request2->attributes->set('request_id', 'xyz789'); $this->requestStack->push($request2); $service2 = new ErrorCodeService($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 ErrorCodeService($this->requestStack); $result1 = $service1->getErrorCode(); $this->requestStack->pop(); $request2 = new Request(); $request2->attributes->set('request_id', 'abc123_2'); $this->requestStack->push($request2); $service2 = new ErrorCodeService($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 ErrorCodeService($this->requestStack); $result1 = $service1->getErrorCode(); $this->requestStack->pop(); $request2 = new Request(); $request2->attributes->set('request_id', 'abc123_5'); $this->requestStack->push($request2); $service2 = new ErrorCodeService($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()); } }