diff --git a/src/Stream.php b/src/Stream.php index 4c17007..36da18c 100644 --- a/src/Stream.php +++ b/src/Stream.php @@ -7,12 +7,16 @@ class Stream { const IN = "in"; const OUT = "out"; const ERROR = "error"; + const REPEAT_CHAR = "⟲"; protected SplFileObject $error; protected SplFileObject $out; protected SplFileObject $in; protected SplFileObject $currentStream; + protected string $lastLineBuffer; + private bool $lastLineRepeats; + public function __construct( string $in = null, string $out = null, @@ -29,6 +33,8 @@ public function __construct( } $this->setStream($in, $out, $error); + $this->lastLineBuffer = ""; + $this->lastLineRepeats = false; } public function setStream(string $in, string $out, string $error):void { @@ -81,7 +87,22 @@ public function writeLine( string $message = "", string $streamName = self::OUT ):void { - $this->write($message . PHP_EOL, $streamName); + $message .= PHP_EOL; + + if($message === $this->lastLineBuffer) { + $this->write(self::REPEAT_CHAR, $streamName); + $this->lastLineRepeats = true; + } + else { + if($this->lastLineRepeats) { + $this->write(PHP_EOL, $streamName); + } + + $this->write($message, $streamName); + $this->lastLineRepeats = false; + } + + $this->lastLineBuffer = $message; } protected function getNamedStream(string $streamName):SplFileObject { diff --git a/test/phpunit/StreamTest.php b/test/phpunit/StreamTest.php index 32e3657..177c1f6 100644 --- a/test/phpunit/StreamTest.php +++ b/test/phpunit/StreamTest.php @@ -117,4 +117,32 @@ public function testInvalidStreamName() { $this->expectException(InvalidStreamNameException::class); $stream->write("this does not exist", "nothing"); } -} \ No newline at end of file + + public function testRepeatingLineSuppressed():void { + $stream = new Stream( + "php://memory", + "php://memory", + "php://memory", + ); + $out = $stream->getOutStream(); + + for($i = 1; $i <= 10; $i++) { + $stream->writeLine("This is message $i, and should appear individually."); + } + + for($i = 1; $i <= 5; $i++) { + $stream->writeLine("This message is sent 5 times but should only appear once."); + } + + for($i = 11; $i <= 20; $i++) { + $stream->writeLine("This is message $i, and should appear after the repeating message individually."); + } + + $out->rewind(); + $fullStreamContents = $out->fread(1024); + + self::assertSame(10, substr_count($fullStreamContents, "should appear individually"), "Unique messages should appear individually"); + self::assertSame(1, substr_count($fullStreamContents, "should only appear once"), "Similar messages should only appear once"); + self::assertSame(4, substr_count($fullStreamContents, Stream::REPEAT_CHAR)); + } +}