Skip to content

Commit 3bd6e1d

Browse files
authored
Release/1.0.1 (#2)
* fix: Implement LogStream fallback mechanism and add unit tests.
1 parent c83096a commit 3bd6e1d

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/Internal/Stream/LogStream.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ private function __construct(mixed $resource)
1616

1717
public static function from(mixed $resource = null): LogStream
1818
{
19-
return new LogStream(resource: $resource ?? STDERR);
19+
if ($resource !== null) {
20+
return new LogStream(resource: $resource);
21+
}
22+
23+
$fallback = fopen('php://stderr', 'wb');
24+
25+
return new LogStream(resource: $fallback);
2026
}
2127

2228
public function write(string $content): void
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Test\TinyBlocks\Logger\Internal\Stream;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use TinyBlocks\Logger\Internal\Stream\LogStream;
9+
10+
final class LogStreamTest extends TestCase
11+
{
12+
/** @noinspection PhpConditionAlreadyCheckedInspection */
13+
public function testFromWithoutResourceFallsBackToStderr(): void
14+
{
15+
/** @Given no resource is provided */
16+
/** @When creating a LogStream without a resource */
17+
$logStream = LogStream::from();
18+
19+
/** @Then a LogStream instance should be returned (using stderr as fallback) */
20+
self::assertInstanceOf(LogStream::class, $logStream);
21+
}
22+
}

0 commit comments

Comments
 (0)