From 85a17474c7cb5efe39206bd8b51d732503be6a56 Mon Sep 17 00:00:00 2001 From: Yousaf Nabi Date: Wed, 7 Oct 2026 17:26:57 +0100 Subject: [PATCH 1/3] fix: upgrade pact-ffi to 0.5.9 without Windows logging hangs Preserve config-based logging on stdout through the existing logger singleton so PHPUnit process isolation cannot deadlock on a full stderr pipe. Keep DEBUG output and respect an already-applied logger.\n\nAdd logging regression coverage and document why the 0.5.10 invalid native boolean returns remain out of scope. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- composer.json | 4 +- docs/troubleshooting.md | 22 +++- .../pacts/binaryConsumer-binaryProvider.json | 8 +- src/PhpPact/FFI/Client.php | 8 +- tests/PhpPact/FFI/ClientLoggingTest.php | 123 ++++++++++++++++++ tests/PhpPact/FFI/ClientTest.php | 2 + 6 files changed, 158 insertions(+), 9 deletions(-) create mode 100644 tests/PhpPact/FFI/ClientLoggingTest.php diff --git a/composer.json b/composer.json index 504cd3a81..6d91209a8 100644 --- a/composer.json +++ b/composer.json @@ -244,12 +244,12 @@ "extra": { "downloads": { "pact-ffi-headers": { - "version": "0.5.8", + "version": "0.5.9", "url": "https://github.com/pact-foundation/pact-reference/releases/download/libpact_ffi-v{$version}/pact.h", "path": "bin/pact-ffi-headers/pact.h" }, "pact-ffi-lib": { - "version": "0.5.8", + "version": "0.5.9", "variables": { "{$prefix}": "PHP_OS_FAMILY === 'Windows' ? 'pact_ffi' : 'libpact_ffi'", "{$os}": "PHP_OS === 'Darwin' ? 'macos' : strtolower(PHP_OS_FAMILY)", diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index 774b415fb..16f1bb6dc 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -101,6 +101,26 @@ $config->setLogLevel('DEBUG'); * Pros * Simple + * Support plugins (e.g. csv, gRPC) * Cons - * Do not support plugins (e.g. csv, gRPC) * Only single sink (stdout) + +### Windows process isolation and Pact FFI 0.5.9 + +Pact FFI 0.5.9 changed `pactffi_init_with_log_level` to write to stderr instead of +stdout. PHPUnit's process-isolation runner reads the child process's stdout to +EOF before draining stderr. On Windows, DEBUG logs can fill the stderr pipe: +the child blocks writing logs while PHPUnit waits for the child to finish. + +Pact-PHP uses its existing logger singleton and an explicit stdout sink for +config-based logging. This preserves the previous logging destination and DEBUG +output without removing `--process-isolation`. An already-applied logger, such as +the file sink configured through the PHPUnit extension, is left unchanged. +The first applied logger determines the destination and level for the process. + +The upgrade intentionally stops at 0.5.9. The published 0.5.10 Linux x86_64 library +was observed returning byte `2` instead of `0` on false-result paths, including +`pactffi_mock_server_matched` for a nonexistent server and `pactffi_with_body` for +an invalid handle. Both PHP FFI and Python ctypes interpret this as true. +Addressing those invalid native boolean returns is a separate upstream task; +the PHP assertions must not be weakened to hide them. diff --git a/example/binary/pacts/binaryConsumer-binaryProvider.json b/example/binary/pacts/binaryConsumer-binaryProvider.json index 72fc73dd2..04a6e2681 100644 --- a/example/binary/pacts/binaryConsumer-binaryProvider.json +++ b/example/binary/pacts/binaryConsumer-binaryProvider.json @@ -32,10 +32,10 @@ "value": "image/jpeg" } ] - } - }, - "header": {}, - "status": {} + }, + "header": {}, + "status": {} + } }, "status": 200 } diff --git a/src/PhpPact/FFI/Client.php b/src/PhpPact/FFI/Client.php index 8d12dc5c2..a35ebacda 100644 --- a/src/PhpPact/FFI/Client.php +++ b/src/PhpPact/FFI/Client.php @@ -10,6 +10,9 @@ use PhpPact\FFI\Model\ArrayData; use PhpPact\FFI\Model\BinaryData; use PhpPact\FFI\Model\Result; +use PhpPact\Log\Enum\LogLevel; +use PhpPact\Log\Logger; +use PhpPact\Log\Model\Stdout; use PhpPact\Standalone\Installer\Model\Scripts; class Client implements ClientInterface @@ -224,8 +227,9 @@ public function withSpecification(int $pact, int $specification): bool public function initWithLogLevel(string $logLevel): void { - $method = 'pactffi_init_with_log_level'; - $this->call($method, $logLevel); + $logger = Logger::instance($this); + $logger->attach(new Stdout(LogLevel::tryFrom(\strtoupper($logLevel)) ?? LogLevel::INFO)); + $logger->apply(); } public function pactHandleWriteFile(int $pact, string $directory, bool $overwrite): int diff --git a/tests/PhpPact/FFI/ClientLoggingTest.php b/tests/PhpPact/FFI/ClientLoggingTest.php new file mode 100644 index 000000000..182d07344 --- /dev/null +++ b/tests/PhpPact/FFI/ClientLoggingTest.php @@ -0,0 +1,123 @@ +client = $this->getMockBuilder(Client::class) + ->disableOriginalConstructor() + ->onlyMethods([ + 'loggerInit', + 'loggerAttachSink', + 'loggerApply', + 'getLevelFilterTrace', + 'getLevelFilterDebug', + 'getLevelFilterInfo', + 'getLevelFilterWarn', + 'getLevelFilterError', + 'getLevelFilterOff', + ]) + ->getMock(); + } + + public function tearDown(): void + { + Logger::tearDown(); + } + + #[TestWith(['TRACE', 'getLevelFilterTrace'])] + #[TestWith(['debug', 'getLevelFilterDebug'])] + #[TestWith(['INFO', 'getLevelFilterInfo'])] + #[TestWith(['WARN', 'getLevelFilterWarn'])] + #[TestWith(['ERROR', 'getLevelFilterError'])] + #[TestWith(['OFF', 'getLevelFilterOff'])] + #[TestWith(['NONE', 'getLevelFilterOff'])] + #[TestWith(['unknown', 'getLevelFilterInfo'])] + public function testInitWithLogLevelUsesStdout(string $level, string $levelMethod): void + { + $this->client->expects($this->once())->method('loggerInit'); + $this->client->expects($this->once())->method($levelMethod)->willReturn(3); + $this->client->expects($this->once())->method('loggerAttachSink')->with('stdout', 3)->willReturn(0); + $this->client->expects($this->once())->method('loggerApply')->willReturn(0); + + $this->client->initWithLogLevel($level); + $this->client->initWithLogLevel($level); + } + + public function testInitWithLogLevelPreservesAppliedFileLogger(): void + { + $this->client->expects($this->once())->method('getLevelFilterDebug')->willReturn(4); + $this->client->expects($this->once())->method('loggerAttachSink')->with('file pact.log', 4)->willReturn(0); + $this->client->expects($this->once())->method('loggerApply')->willReturn(0); + $logger = Logger::instance($this->client); + $logger->attach(new File('pact.log', LogLevel::DEBUG)); + $logger->apply(); + + $this->client->initWithLogLevel('DEBUG'); + } + + public function testInitWithLogLevelReportsSinkFailure(): void + { + $this->client->method('getLevelFilterDebug')->willReturn(4); + $this->client->method('loggerAttachSink')->with('stdout', 4)->willReturn(-4); + $this->client->expects($this->never())->method('loggerApply'); + $this->expectException(LoggerAttachSinkException::class); + + $this->client->initWithLogLevel('DEBUG'); + } + + public function testInitWithLogLevelReportsApplyFailure(): void + { + $this->client->method('loggerAttachSink')->willReturn(0); + $this->client->method('loggerApply')->willReturn(-1); + $this->expectException(LoggerApplyException::class); + + $this->client->initWithLogLevel('DEBUG'); + } + + public function testNativeDebugLogsDoNotFillStderr(): void + { + $code = <<<'PHP' +require $argv[1]; +$client = new PhpPact\FFI\Client(); +$client->initWithLogLevel('DEBUG'); +$pact = $client->newPact('logging-consumer', 'logging-provider'); +$interaction = $client->newInteraction($pact, 'logging'); +for ($index = 0; $index < 64; $index++) { + if (!$client->withHeaderV2($interaction, $client->getInteractionPartRequest(), 'Accept', 0, 'image/jpeg')) { + throw new RuntimeException('Could not configure the logging interaction'); + } +} +$client->freePactHandle($pact); +PHP; + $process = new Process( + [PHP_BINARY, '-d', 'ffi.enable=true', '-r', $code, __DIR__ . '/../../../vendor/autoload.php'], + null, + ['PACT_DO_NOT_TRACK' => 'true'], + null, + 10 + ); + $process->run(); + + $this->assertSame(0, $process->getExitCode(), $process->getErrorOutput()); + $this->assertSame('', $process->getErrorOutput()); + $this->assertStringContainsString('DEBUG', $process->getOutput()); + $this->assertGreaterThan(4096, strlen($process->getOutput())); + } +} diff --git a/tests/PhpPact/FFI/ClientTest.php b/tests/PhpPact/FFI/ClientTest.php index a63507aaa..9d7c5850a 100644 --- a/tests/PhpPact/FFI/ClientTest.php +++ b/tests/PhpPact/FFI/ClientTest.php @@ -6,6 +6,7 @@ use PhpPact\FFI\Client; use PhpPact\FFI\ClientInterface; use PhpPact\FFI\Model\BinaryData; +use PhpPact\Log\Logger; use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; @@ -143,6 +144,7 @@ public function testWithSpecification(): void public function testInitWithLogLevel(): void { $this->client->initWithLogLevel('test'); + Logger::tearDown(); $this->expectNotToPerformAssertions(); } From 1a5debd50904cb608233608af3545e4e30ab9b15 Mon Sep 17 00:00:00 2001 From: Yousaf Nabi Date: Wed, 7 Oct 2026 17:27:09 +0100 Subject: [PATCH 2/3] chore: restore generated binary pact fixture Exclude test-generated pact changes from the FFI upgrade. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- example/binary/pacts/binaryConsumer-binaryProvider.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/example/binary/pacts/binaryConsumer-binaryProvider.json b/example/binary/pacts/binaryConsumer-binaryProvider.json index 04a6e2681..72fc73dd2 100644 --- a/example/binary/pacts/binaryConsumer-binaryProvider.json +++ b/example/binary/pacts/binaryConsumer-binaryProvider.json @@ -32,10 +32,10 @@ "value": "image/jpeg" } ] - }, - "header": {}, - "status": {} - } + } + }, + "header": {}, + "status": {} }, "status": 200 } From 1ee0c8c52481ca6f3822935fa5d6d36713c7827e Mon Sep 17 00:00:00 2001 From: Yousaf Nabi Date: Wed, 7 Oct 2026 18:33:42 +0100 Subject: [PATCH 3/3] chore: pact-ffi 0.5.10 --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 6d91209a8..1e00fb475 100644 --- a/composer.json +++ b/composer.json @@ -244,12 +244,12 @@ "extra": { "downloads": { "pact-ffi-headers": { - "version": "0.5.9", + "version": "0.5.10", "url": "https://github.com/pact-foundation/pact-reference/releases/download/libpact_ffi-v{$version}/pact.h", "path": "bin/pact-ffi-headers/pact.h" }, "pact-ffi-lib": { - "version": "0.5.9", + "version": "0.5.10", "variables": { "{$prefix}": "PHP_OS_FAMILY === 'Windows' ? 'pact_ffi' : 'libpact_ffi'", "{$os}": "PHP_OS === 'Darwin' ? 'macos' : strtolower(PHP_OS_FAMILY)",