Skip to content

Commit 955cd76

Browse files
authored
Pass progress symbol as a parameter instead of part of the constructor (#97)
This offers more flexibility as it makes the instantiation of the standard logger no longer dependent of the progress symbol. This means if we remove the progress symbol from being passed to the parallel executor (for example to use a default value), this will not be a problem (currently that default value _needs_ to be in Parallelization).
1 parent f84da93 commit 955cd76

8 files changed

+33
-15
lines changed

src/Logger/Logger.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function advance(int $steps = 1): void;
3333

3434
public function finish(string $itemName): void;
3535

36-
public function logUnexpectedOutput(string $buffer): void;
36+
public function logUnexpectedOutput(string $buffer, string $progressSymbol): void;
3737

3838
public function logCommandStarted(string $commandName): void;
3939

src/Logger/StandardLogger.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,18 @@
2626
final class StandardLogger implements Logger
2727
{
2828
private OutputInterface $output;
29-
private string $advancementChar;
3029
private int $terminalWidth;
3130
private ProgressBar $progressBar;
3231
private ProgressBarFactory $progressBarFactory;
3332
private LoggerInterface $logger;
3433

3534
public function __construct(
3635
OutputInterface $output,
37-
string $advancementCharacter,
3836
int $terminalWidth,
3937
ProgressBarFactory $progressBarFactory,
4038
LoggerInterface $logger
4139
) {
4240
$this->output = $output;
43-
$this->advancementChar = $advancementCharacter;
4441
$this->terminalWidth = $terminalWidth;
4542
$this->progressBarFactory = $progressBarFactory;
4643
$this->logger = $logger;
@@ -115,7 +112,7 @@ public function finish(string $itemName): void
115112
unset($this->progressBar);
116113
}
117114

118-
public function logUnexpectedOutput(string $buffer): void
115+
public function logUnexpectedOutput(string $buffer, string $progressSymbol): void
119116
{
120117
$this->output->writeln('');
121118
$this->output->writeln(sprintf(
@@ -127,7 +124,7 @@ public function logUnexpectedOutput(string $buffer): void
127124
STR_PAD_BOTH,
128125
),
129126
));
130-
$this->output->writeln(str_replace($this->advancementChar, '', $buffer));
127+
$this->output->writeln(str_replace($progressSymbol, '', $buffer));
131128
$this->output->writeln('');
132129
}
133130

src/ParallelExecutor.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,12 @@ private function processChildOutput(
330330
string $buffer,
331331
Logger $logger
332332
): void {
333-
$advancementChar = $this->progressSymbol;
334-
$chars = mb_substr_count($buffer, $advancementChar);
333+
$progressSymbol = $this->progressSymbol;
334+
$chars = mb_substr_count($buffer, $progressSymbol);
335335

336336
// Display unexpected output
337337
if ($chars !== mb_strlen($buffer)) {
338-
$logger->logUnexpectedOutput($buffer);
338+
$logger->logUnexpectedOutput($buffer, $progressSymbol);
339339
}
340340

341341
$logger->advance($chars);

src/Parallelization.php

-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ protected function createLogger(OutputInterface $output): Logger
259259
{
260260
return new StandardLogger(
261261
$output,
262-
self::getProgressSymbol(),
263262
(new Terminal())->getWidth(),
264263
new DebugProgressBarFactory(),
265264
new ConsoleLogger($output),

tests/Fixtures/Command/ImportMoviesCommand.php

-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ protected function createLogger(OutputInterface $output): Logger
115115
{
116116
return new StandardLogger(
117117
$output,
118-
self::getProgressSymbol(),
119118
(new Terminal())->getWidth(),
120119
new TestDebugProgressBarFactory(),
121120
new ConsoleLogger($output),

tests/Fixtures/Command/NoSubProcessCommand.php

-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ protected function createLogger(OutputInterface $output): Logger
7878
{
7979
return new StandardLogger(
8080
$output,
81-
self::getProgressSymbol(),
8281
(new Terminal())->getWidth(),
8382
new TestDebugProgressBarFactory(),
8483
new ConsoleLogger($output),

tests/Logger/FakeLogger.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function finish(string $itemName): void
4545
throw new DomainException('Unexpected call.');
4646
}
4747

48-
public function logUnexpectedOutput(string $buffer): void
48+
public function logUnexpectedOutput(string $buffer, string $progressSymbol): void
4949
{
5050
throw new DomainException('Unexpected call.');
5151
}

tests/Logger/StandardLoggerTest.php

+26-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ protected function setUp(): void
3636

3737
$this->logger = new StandardLogger(
3838
$this->output,
39-
self::ADVANCEMENT_CHARACTER,
4039
self::TERMINAL_WIDTH,
4140
new TestProgressBarFactory(),
4241
new ConsoleLogger($this->output),
@@ -129,7 +128,32 @@ public function test_it_can_log_the_unexpected_output_of_a_child_process(): void
129128
$this->logger->advance(4);
130129
$this->output->fetch();
131130

132-
$this->logger->logUnexpectedOutput('An error occurred.');
131+
$this->logger->logUnexpectedOutput(
132+
'An error occurred.',
133+
self::ADVANCEMENT_CHARACTER,
134+
);
135+
136+
$expected = <<<'TXT'
137+
138+
================= Process Output =================
139+
An error occurred.
140+
141+
142+
TXT;
143+
144+
self::assertSame($expected, $this->output->fetch());
145+
}
146+
147+
public function test_it_removes_the_progress_character_of_the_unexpected_output_of_a_child_process(): void
148+
{
149+
$this->logger->startProgress(10);
150+
$this->logger->advance(4);
151+
$this->output->fetch();
152+
153+
$this->logger->logUnexpectedOutput(
154+
'An error'.self::ADVANCEMENT_CHARACTER.' occurred.',
155+
self::ADVANCEMENT_CHARACTER,
156+
);
133157

134158
$expected = <<<'TXT'
135159

0 commit comments

Comments
 (0)