Skip to content

Commit 641529f

Browse files
authored
Allow the logger to be overwritten (#82)
Allow the logger to be overwritten which opens the door for using a different logger for testing providing the means to finally have stable tests.
1 parent 42e5ea7 commit 641529f

9 files changed

+116
-16
lines changed

src/Logger/Logger.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function finish(string $itemName): void;
3333

3434
public function logUnexpectedOutput(string $buffer): void;
3535

36-
public function logCommandStarted(string $string): void;
36+
public function logCommandStarted(string $commandName): void;
3737

38-
public function logCommandFinished(string $string): void;
38+
public function logCommandFinished(): void;
3939
}

src/Logger/StandardLogger.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ public function logCommandStarted(string $commandName): void
135135
$this->logger->debug('Command started: '.$commandName);
136136
}
137137

138-
public function logCommandFinished(string $commandName): void
138+
public function logCommandFinished(): void
139139
{
140-
$this->logger->debug('Command finished: '.$commandName);
140+
$this->logger->debug('Command finished');
141141
}
142142
}

src/Parallelization.php

+12-7
Original file line numberDiff line numberDiff line change
@@ -280,13 +280,7 @@ protected function executeMasterProcess(
280280
$numberOfBatches = $config->getNumberOfBatches();
281281
$itemName = $this->getItemName($numberOfItems);
282282

283-
$logger = new StandardLogger(
284-
$output,
285-
self::getProgressSymbol(),
286-
(new Terminal())->getWidth(),
287-
new DebugProgressBarFactory(),
288-
new ConsoleLogger($output),
289-
);
283+
$logger = $this->createLogger($output);
290284

291285
$logger->logConfiguration(
292286
$segmentSize,
@@ -410,6 +404,17 @@ protected function executeChildProcess(
410404
}
411405
}
412406

407+
protected function createLogger(OutputInterface $output): Logger
408+
{
409+
return new StandardLogger(
410+
$output,
411+
self::getProgressSymbol(),
412+
(new Terminal())->getWidth(),
413+
new DebugProgressBarFactory(),
414+
new ConsoleLogger($output),
415+
);
416+
}
417+
413418
/**
414419
* @internal
415420
* @return positive-int

src/ProcessLauncher.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private function startProcess(InputStream $inputStream): void
149149
}
150150
$process->start($this->callback);
151151

152-
$this->logger->logCommandStarted('Command started: '.$this->command);
152+
$this->logger->logCommandStarted($this->command);
153153

154154
$this->runningProcesses[] = $process;
155155
}
@@ -162,7 +162,7 @@ private function freeTerminatedProcesses(): void
162162
{
163163
foreach ($this->runningProcesses as $key => $process) {
164164
if (!$process->isRunning()) {
165-
$this->logger->logCommandFinished('Command finished');
165+
$this->logger->logCommandFinished();
166166

167167
unset($this->runningProcesses[$key]);
168168
}

tests/Fixtures/Command/ImportMoviesCommand.php

+16
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@
1717
use function json_decode;
1818
use const JSON_THROW_ON_ERROR;
1919
use Symfony\Component\Console\Input\InputInterface;
20+
use Symfony\Component\Console\Logger\ConsoleLogger;
2021
use Symfony\Component\Console\Output\OutputInterface;
22+
use Symfony\Component\Console\Terminal;
2123
use Webmozarts\Console\Parallelization\ContainerAwareCommand;
24+
use Webmozarts\Console\Parallelization\Integration\TestDebugProgressBarFactory;
2225
use Webmozarts\Console\Parallelization\Integration\TestLogger;
26+
use Webmozarts\Console\Parallelization\Logger\Logger;
27+
use Webmozarts\Console\Parallelization\Logger\StandardLogger;
2328
use Webmozarts\Console\Parallelization\Parallelization;
2429

2530
final class ImportMoviesCommand extends ContainerAwareCommand
@@ -106,6 +111,17 @@ protected function getItemName(int $count): string
106111
return 1 === $count ? 'movie' : 'movies';
107112
}
108113

114+
protected function createLogger(OutputInterface $output): Logger
115+
{
116+
return new StandardLogger(
117+
$output,
118+
self::getProgressSymbol(),
119+
(new Terminal())->getWidth(),
120+
new TestDebugProgressBarFactory(),
121+
new ConsoleLogger($output),
122+
);
123+
}
124+
109125
/**
110126
* @param list<string> $movieFileNames
111127
*

tests/Fixtures/Command/NoSubProcessCommand.php

+16
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@
1515

1616
use DomainException;
1717
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Logger\ConsoleLogger;
1819
use Symfony\Component\Console\Output\OutputInterface;
20+
use Symfony\Component\Console\Terminal;
1921
use Webmozarts\Console\Parallelization\ContainerAwareCommand;
22+
use Webmozarts\Console\Parallelization\Integration\TestDebugProgressBarFactory;
23+
use Webmozarts\Console\Parallelization\Logger\Logger;
24+
use Webmozarts\Console\Parallelization\Logger\StandardLogger;
2025
use Webmozarts\Console\Parallelization\Parallelization;
2126

2227
final class NoSubProcessCommand extends ContainerAwareCommand
@@ -67,4 +72,15 @@ protected function getItemName(int $count): string
6772
{
6873
return 0 === $count ? 'item' : 'items';
6974
}
75+
76+
protected function createLogger(OutputInterface $output): Logger
77+
{
78+
return new StandardLogger(
79+
$output,
80+
self::getProgressSymbol(),
81+
(new Terminal())->getWidth(),
82+
new TestDebugProgressBarFactory(),
83+
new ConsoleLogger($output),
84+
);
85+
}
7086
}

tests/Integration/ParallelizationIntegrationTest.php

+29-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ public function test_it_can_run_the_command_without_sub_processes(): void
6464
Processing 5 items in segments of 2, batches of 2, 1 round, 1 batch in 1 process
6565
6666
0/5 [>---------------------------] 0% 10 secs/10 secs 10.0 MiB
67+
1/5 [=====>----------------------] 20% 10 secs/10 secs 10.0 MiB
68+
2/5 [===========>----------------] 40% 10 secs/10 secs 10.0 MiB
69+
3/5 [================>-----------] 60% 10 secs/10 secs 10.0 MiB
70+
4/5 [======================>-----] 80% 10 secs/10 secs 10.0 MiB
6771
5/5 [============================] 100% 10 secs/10 secs 10.0 MiB
6872
6973
Processed 5 items.
@@ -136,6 +140,18 @@ public function test_it_can_run_the_command_with_multiple_processes_in_debug_mod
136140
],
137141
);
138142

143+
$expectedWithNoDebugMode = <<<'EOF'
144+
Processing 5 movies in segments of 2, batches of 2, 3 rounds, 3 batches in 2 processes
145+
146+
0/5 [>---------------------------] 0% 10 secs/10 secs 10.0 MiB
147+
2/5 [===========>----------------] 40% 10 secs/10 secs 10.0 MiB
148+
4/5 [======================>-----] 80% 10 secs/10 secs 10.0 MiB
149+
5/5 [============================] 100% 10 secs/10 secs 10.0 MiB
150+
151+
Processed 5 movies.
152+
153+
EOF;
154+
139155
$expected = <<<'EOF'
140156
Processing 5 movies in segments of 2, batches of 2, 3 rounds, 3 batches in 2 processes
141157
@@ -156,7 +172,19 @@ public function test_it_can_run_the_command_with_multiple_processes_in_debug_mod
156172

157173
$actual = $this->getOutput($commandTester);
158174

159-
self::assertSame($expected, $actual, $actual);
175+
$expectedChildProcessesCount = 3;
176+
$expectedCommandStartedLine = "[debug] Command started: '/path/to/php' '/path/to/work-dir/bin/console' 'import:movies' '--child'\n";
177+
$expectedCommandFinishedLine = "[debug] Command finished\n";
178+
179+
$outputWithoutExtraDebugInfo = str_replace(
180+
[$expectedCommandStartedLine, $expectedCommandFinishedLine],
181+
['', ''],
182+
$actual,
183+
);
184+
185+
self::assertSame($expectedWithNoDebugMode, $outputWithoutExtraDebugInfo, $outputWithoutExtraDebugInfo);
186+
self::assertSame($expectedChildProcessesCount, mb_substr_count($actual, $expectedCommandStartedLine));
187+
self::assertSame($expectedChildProcessesCount, mb_substr_count($actual, $expectedCommandFinishedLine));
160188
}
161189

162190
private function getOutput(CommandTester $commandTester): string
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Webmozarts Console Parallelization package.
5+
*
6+
* (c) Webmozarts GmbH <office@webmozarts.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Webmozarts\Console\Parallelization\Integration;
15+
16+
use const PHP_FLOAT_MIN;
17+
use Symfony\Component\Console\Helper\ProgressBar;
18+
use Symfony\Component\Console\Output\OutputInterface;
19+
use Webmozarts\Console\Parallelization\Logger\ProgressBarFactory;
20+
21+
final class TestDebugProgressBarFactory implements ProgressBarFactory
22+
{
23+
public function create(
24+
OutputInterface $output,
25+
int $numberOfItems
26+
): ProgressBar {
27+
// Put the lowest time between redraws to ensure they we see all elements
28+
// of progress.
29+
$progressBar = new ProgressBar($output, $numberOfItems, PHP_FLOAT_MIN);
30+
$progressBar->setFormat(ProgressBar::FORMAT_DEBUG);
31+
$progressBar->start();
32+
33+
return $progressBar;
34+
}
35+
}

tests/Logger/StandardLoggerTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,10 @@ public function test_it_can_log_the_end_of_a_command(): void
159159
{
160160
$this->output->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
161161

162-
$this->logger->logCommandFinished('/path/to/bin/console foo:bar --child');
162+
$this->logger->logCommandFinished();
163163

164164
$expected = <<<'TXT'
165-
[debug] Command finished: /path/to/bin/console foo:bar --child
165+
[debug] Command finished
166166

167167
TXT;
168168

0 commit comments

Comments
 (0)