From efa9fb12f7db1f823b182e67ae5ba98db3f68dab Mon Sep 17 00:00:00 2001 From: Gustavo Freze Date: Tue, 24 Feb 2026 02:04:23 -0300 Subject: [PATCH 1/2] fix: Implement LogStream fallback mechanism and add unit tests. --- src/Internal/Stream/LogStream.php | 8 ++++- tests/Internal/Stream/LogStreamTest.php | 42 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 tests/Internal/Stream/LogStreamTest.php diff --git a/src/Internal/Stream/LogStream.php b/src/Internal/Stream/LogStream.php index 144dd27..dafb875 100644 --- a/src/Internal/Stream/LogStream.php +++ b/src/Internal/Stream/LogStream.php @@ -16,7 +16,13 @@ private function __construct(mixed $resource) public static function from(mixed $resource = null): LogStream { - return new LogStream(resource: $resource ?? STDERR); + if ($resource !== null) { + return new LogStream(resource: $resource); + } + + $fallback = fopen('php://memory', 'wb+'); + + return new LogStream(resource: $fallback); } public function write(string $content): void diff --git a/tests/Internal/Stream/LogStreamTest.php b/tests/Internal/Stream/LogStreamTest.php new file mode 100644 index 0000000..4e447cc --- /dev/null +++ b/tests/Internal/Stream/LogStreamTest.php @@ -0,0 +1,42 @@ +write('custom resource test'); + + rewind($stream); + $output = stream_get_contents($stream); + + self::assertSame('custom resource test', $output); + + fclose($stream); + } + + public function testFromWithNullFallsBackToStderr(): void + { + /** @Given no resource is provided */ + /** @When creating a LogStream without arguments */ + $logStream = LogStream::from(); + + /** @Then it should be created successfully and be writable without errors */ + $logStream->write('fallback test'); + + self::assertTrue(true); + } +} From d12d51ccca85c42bb221f6517e929464785d7a4f Mon Sep 17 00:00:00 2001 From: Gustavo Freze Date: Tue, 24 Feb 2026 02:14:56 -0300 Subject: [PATCH 2/2] fix: Implement LogStream fallback mechanism and add unit tests. --- src/Internal/Stream/LogStream.php | 2 +- tests/Internal/Stream/LogStreamTest.php | 30 +++++-------------------- 2 files changed, 6 insertions(+), 26 deletions(-) diff --git a/src/Internal/Stream/LogStream.php b/src/Internal/Stream/LogStream.php index dafb875..2537757 100644 --- a/src/Internal/Stream/LogStream.php +++ b/src/Internal/Stream/LogStream.php @@ -20,7 +20,7 @@ public static function from(mixed $resource = null): LogStream return new LogStream(resource: $resource); } - $fallback = fopen('php://memory', 'wb+'); + $fallback = fopen('php://stderr', 'wb'); return new LogStream(resource: $fallback); } diff --git a/tests/Internal/Stream/LogStreamTest.php b/tests/Internal/Stream/LogStreamTest.php index 4e447cc..f6d8e05 100644 --- a/tests/Internal/Stream/LogStreamTest.php +++ b/tests/Internal/Stream/LogStreamTest.php @@ -9,34 +9,14 @@ final class LogStreamTest extends TestCase { - public function testFromWithCustomResource(): void - { - /** @Given a custom writable stream */ - $stream = fopen('php://memory', 'r+'); - - /** @When creating a LogStream with the custom resource */ - $logStream = LogStream::from(resource: $stream); - - /** @Then it should write content to the provided stream */ - $logStream->write('custom resource test'); - - rewind($stream); - $output = stream_get_contents($stream); - - self::assertSame('custom resource test', $output); - - fclose($stream); - } - - public function testFromWithNullFallsBackToStderr(): void + /** @noinspection PhpConditionAlreadyCheckedInspection */ + public function testFromWithoutResourceFallsBackToStderr(): void { /** @Given no resource is provided */ - /** @When creating a LogStream without arguments */ + /** @When creating a LogStream without a resource */ $logStream = LogStream::from(); - /** @Then it should be created successfully and be writable without errors */ - $logStream->write('fallback test'); - - self::assertTrue(true); + /** @Then a LogStream instance should be returned (using stderr as fallback) */ + self::assertInstanceOf(LogStream::class, $logStream); } }