Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Internal/Stream/LogStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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+');
Comment thread
gustavofreze marked this conversation as resolved.
Outdated
Comment thread
gustavofreze marked this conversation as resolved.
Outdated

return new LogStream(resource: $fallback);
Comment thread
gustavofreze marked this conversation as resolved.
Outdated
}

public function write(string $content): void
Expand Down
42 changes: 42 additions & 0 deletions tests/Internal/Stream/LogStreamTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Test\TinyBlocks\Logger\Internal\Stream;

use PHPUnit\Framework\TestCase;
use TinyBlocks\Logger\Internal\Stream\LogStream;

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
Comment thread
gustavofreze marked this conversation as resolved.
Outdated
{
/** @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);
Comment thread
gustavofreze marked this conversation as resolved.
Outdated
Comment thread
gustavofreze marked this conversation as resolved.
Outdated
}
}