Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
88 changes: 59 additions & 29 deletions src/Http/Message/Handler/CompactProgressHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
use Symfony\Component\Console;
use Throwable;

use function min;
use function round;
use function sprintf;
use function str_pad;
use function str_repeat;
use function strlen;

/**
* CompactProgressHandler.
*
Expand All @@ -35,63 +42,86 @@
*/
final class CompactProgressHandler implements ResponseHandler
{
private const PROGRESS_BAR_FORMAT = ' %current%/%max% [%bar%] %percent:3s%% -- <%fail_tag%>%fail_count% %failures%</>';
private const MAX_LINE_LENGTH = 80;

private readonly Console\Helper\ProgressBar $progressBar;
private int $failures = 0;
private readonly int $maxColumns;
private int $columns = 0;
private int $current = 0;

public function __construct(
private readonly Console\Output\OutputInterface $output,
int $max,
private readonly int $max,
) {
$this->progressBar = $this->createProgressBar($output, $max);
$this->maxColumns = $this->calculateMaxColumns();
}

public function startProgressBar(): void
{
$this->progressBar->setMessage('info', 'fail_tag');
$this->progressBar->setMessage('no', 'fail_count');
$this->progressBar->setMessage('failures', 'failures');
$this->progressBar->start();
$this->reset();
}

public function finishProgressBar(): void
{
$this->progressBar->finish();
$this->output->writeln('');
$this->reset();
}

public function onSuccess(Message\ResponseInterface $response, Message\UriInterface $uri): void
{
$this->progressBar->advance();
$this->progressBar->display();
$this->advance('.');
}

public function onFailure(Throwable $exception, Message\UriInterface $uri): void
{
$this->progressBar->setMessage((string) ++$this->failures, 'fail_count');
$this->advance('<error>F</error>');
}

if ($this->failures > 0) {
$this->progressBar->setMessage('error', 'fail_tag');
}
if (1 === $this->failures) {
$this->progressBar->setMessage('failure', 'failures');
private function advance(string $output): void
{
++$this->columns;
++$this->current;

if ($this->columns >= $this->maxColumns || $this->current === $this->max) {
$this->output->writeln($output.$this->renderCurrentState());
$this->columns = 0;
} else {
$this->output->write($output);
}
if (2 === $this->failures) {
$this->progressBar->setMessage('failures', 'failures');
}

private function renderCurrentState(): string
{
$percent = round(($this->current / $this->max) * 100);

if ($this->max === $this->current) {
$fill = str_repeat(' ', $this->maxColumns - $this->columns);
} else {
$fill = '';
}

$this->progressBar->advance();
$this->progressBar->display();
return sprintf(
'%s %s / %d (%s%%)',
$fill,
str_pad((string) $this->current, strlen((string) $this->max), ' ', STR_PAD_LEFT),
$this->max,
str_pad((string) $percent, 3, ' ', STR_PAD_LEFT),
);
}

private function createProgressBar(Console\Output\OutputInterface $output, int $max): Console\Helper\ProgressBar
private function calculateMaxColumns(): int
{
Console\Helper\ProgressBar::setFormatDefinition('cache-warmup', self::PROGRESS_BAR_FORMAT);

$progressBar = new Console\Helper\ProgressBar($output, $max);
$progressBar->setFormat('cache-warmup');
// current / max (...%)
$stateLength = 2 * strlen((string) $this->max) + 11;
$lineLength = min(
(new Console\Terminal())->getWidth(),
self::MAX_LINE_LENGTH,
);

return $lineLength - $stateLength;
}

return $progressBar;
private function reset(): void
{
$this->columns = 0;
$this->current = 0;
}
}
10 changes: 5 additions & 5 deletions tests/unit/Command/CacheWarmupCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function initializeResolvesRelativeConfigFilePath(): void

$this->commandTester->execute(['--config' => 'tests/unit/Fixtures/ConfigFiles/valid_config.php']);

self::assertStringContainsString('3/3', $this->commandTester->getDisplay());
self::assertStringContainsString('3 / 3', $this->commandTester->getDisplay());
}

#[Framework\Attributes\Test]
Expand All @@ -100,7 +100,7 @@ public function initializeLoadsConfigFromGivenFile(string $configFile): void

$this->commandTester->execute(['--config' => $configFile, '--progress' => true]);

self::assertStringContainsString('3/3', $this->commandTester->getDisplay());
self::assertStringContainsString('3 / 3', $this->commandTester->getDisplay());
}

#[Framework\Attributes\Test]
Expand All @@ -113,7 +113,7 @@ public function initializeOverwritesConfigFromGivenFileWithCommandOptions(): voi
'--limit' => 1,
]);

self::assertStringContainsString('1/1', $this->commandTester->getDisplay());
self::assertStringContainsString('1 / 1', $this->commandTester->getDisplay());
}

#[Framework\Attributes\Test]
Expand All @@ -129,7 +129,7 @@ public function initializeOverwritesConfigFromGivenFileDefinedAsEnvironmentVaria

putenv('CACHE_WARMUP_CONFIG');

self::assertStringContainsString('1/1', $this->commandTester->getDisplay());
self::assertStringContainsString('1 / 1', $this->commandTester->getDisplay());
}

#[Framework\Attributes\Test]
Expand Down Expand Up @@ -181,7 +181,7 @@ public function initializeOverwritesConfigFromGivenFileAndCommandOptionsWithEnvi

putenv('CACHE_WARMUP_LIMIT');

self::assertStringContainsString('2/2', $this->commandTester->getDisplay());
self::assertStringContainsString('2 / 2', $this->commandTester->getDisplay());
}

#[Framework\Attributes\Test]
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/Crawler/OutputtingCrawlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,7 @@ public function crawlWritesCrawlingStateAsProgressBarToOutput(): void
$output = $this->output->fetch();

self::assertNotEmpty($output);
self::assertMatchesRegularExpression('#^\s*1/2 \S+\s+\d+% -- no failures#m', $output);
self::assertMatchesRegularExpression('#^\s*2/2 \S+\s+\d+% -- 1 failure#m', $output);
self::assertStringContainsString('2 / 2', $output);
}

#[Framework\Attributes\Test]
Expand Down
45 changes: 22 additions & 23 deletions tests/unit/Http/Message/Handler/CompactProgressHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,50 +44,53 @@ final class CompactProgressHandlerTest extends Framework\TestCase
protected function setUp(): void
{
$this->output = new Console\Output\BufferedOutput();
$this->subject = new Src\Http\Message\Handler\CompactProgressHandler($this->output, 10);
$this->subject = new Src\Http\Message\Handler\CompactProgressHandler($this->output, 200);
}

#[Framework\Attributes\Test]
public function startProgressBarStartsProgressBar(): void
public function onSuccessAdvancesProgressByOneStep(): void
{
$this->subject->startProgressBar();
$response = new Psr7\Response();
$uri = new Psr7\Uri('https://www.example.com');

$output = $this->output->fetch();
$this->subject->startProgressBar();
$this->subject->onSuccess($response, $uri);

self::assertNotEmpty($output);
self::assertMatchesRegularExpression('#^\s*0/10 \S+\s+0% -- no failures#m', $output);
self::assertSame('.', $this->output->fetch());
}

#[Framework\Attributes\Test]
public function finishProgressBarFinishesProgressBar(): void
public function onSuccessPrintsCurrentStateOnLineBreak(): void
{
$response = new Psr7\Response();
$uri = new Psr7\Uri('https://www.example.com');

$this->subject->startProgressBar();
$this->subject->finishProgressBar();

$output = $this->output->fetch();
for ($i = 0; $i < 100; ++$i) {
$this->subject->onSuccess($response, $uri);
}

self::assertNotEmpty($output);
self::assertMatchesRegularExpression('#^\s*0/10 \S+\s+0% -- no failures#m', $output);
self::assertMatchesRegularExpression('#^\s*10/10 \S+\s+100% -- no failures#m', $output);
self::assertStringContainsString('63 / 200 ( 32%)', $this->output->fetch());
}

#[Framework\Attributes\Test]
public function onSuccessPrintsSuccessfulUrlAndAdvancesProgressBarByOneStep(): void
public function onSuccessPrintsFinalStateOnFinishedCrawling(): void
{
$response = new Psr7\Response();
$uri = new Psr7\Uri('https://www.example.com');

$this->subject->startProgressBar();
$this->subject->onSuccess($response, $uri);

$output = $this->output->fetch();
for ($i = 0; $i < 200; ++$i) {
$this->subject->onSuccess($response, $uri);
}

self::assertNotEmpty($output);
self::assertMatchesRegularExpression('#^\s*1/10 \S+\s+10% -- no failures#m', $output);
self::assertStringContainsString('200 / 200 (100%)', $this->output->fetch());
}

#[Framework\Attributes\Test]
public function onFailurePrintsFailedUrlAndAdvancesProgressBarByOneStep(): void
public function onFailureAdvancesProgressByOneStep(): void
{
$exception = new Exception('foo');
$uri = new Psr7\Uri('https://www.example.com');
Expand All @@ -96,10 +99,6 @@ public function onFailurePrintsFailedUrlAndAdvancesProgressBarByOneStep(): void
$this->subject->onFailure($exception, $uri);
$this->subject->onFailure($exception, $uri);

$output = $this->output->fetch();

self::assertNotEmpty($output);
self::assertMatchesRegularExpression('#^\s*1/10 \S+\s+10% -- 1 failure#m', $output);
self::assertMatchesRegularExpression('#^\s*2/10 \S+\s+20% -- 2 failures#m', $output);
self::assertSame('FF', $this->output->fetch());
}
}
Loading