fix: make bpn response failures diagnosable

This commit is contained in:
2026-09-19 11:51:15 +02:00
parent ec83ad598f
commit fbb3ebb8c1
5 changed files with 240 additions and 29 deletions
+52 -2
View File
@@ -6,6 +6,7 @@ namespace App\Tests\BusProNet;
use App\BusProNet\ApiClient;
use App\BusProNet\DataProcessor\BookingDataProcessor;
use App\BusProNet\Exception\ApiClientException;
use App\BusProNet\Exception\ImmediateConnectionCloseException;
use App\BusProNet\XmlParser\ApiResponseParser;
use App\Service\RequestIdGenerator;
@@ -70,7 +71,7 @@ class ApiClientReceiveTest extends TestCase
$this->invokeReceive($stream);
}
public function testNonEmptyResponseReturnsData(): void
public function testNonEmptyResponseReturnsPayloadWithoutTheLengthHeader(): void
{
$stream = fopen('php://memory', 'r+');
fwrite($stream, '0000000005Hello');
@@ -80,7 +81,56 @@ class ApiClientReceiveTest extends TestCase
$result = $this->invokeReceive($stream);
self::assertSame('0000000005Hello', $result);
self::assertSame('Hello', $result);
}
/**
* A peer closing mid-stream used to yield a silently shortened payload, which only
* surfaced later as an unexplained parse failure. The announced length catches it here.
*/
public function testTruncatedResponseThrows(): void
{
$stream = fopen('php://memory', 'r+');
fwrite($stream, '0000000050Hello');
rewind($stream);
$this->setOperationStartTime();
$this->setSelectedPort(9000);
$this->expectException(ApiClientException::class);
$this->expectExceptionMessage('Truncated response, got 5 of 50 announced bytes');
$this->invokeReceive($stream);
}
public function testResponseShorterThanTheLengthHeaderThrows(): void
{
$stream = fopen('php://memory', 'r+');
fwrite($stream, '00000');
rewind($stream);
$this->setOperationStartTime();
$this->setSelectedPort(9000);
$this->expectException(ApiClientException::class);
$this->expectExceptionMessage('Incomplete response header, got 5 of 10 bytes');
$this->invokeReceive($stream);
}
public function testHeaderOnlyResponseThrows(): void
{
$stream = fopen('php://memory', 'r+');
fwrite($stream, '0000000000');
rewind($stream);
$this->setOperationStartTime();
$this->setSelectedPort(9000);
$this->expectException(ApiClientException::class);
$this->expectExceptionMessage('Response announced an empty body (header "0000000000")');
$this->invokeReceive($stream);
}
private function invokeReceive($socket): string