Skip to content

Commit 1c7c255

Browse files
bug symfony#63368 [Console] Fix ProgressBar %remaining% and %estimated% placeholder guards (yoeunes)
This PR was merged into the 7.4 branch. Discussion ---------- [Console] Fix ProgressBar `%remaining%` and `%estimated%` placeholder guards | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT PR symfony#52605 changed `getMaxSteps()` to return `$this->max ?? 0`, which made the null checks guards for `%remaining%` and `%estimated%` placeholders ineffective. This makes `getRemaining()` and `getEstimated()` produce negative values instead of throwing LogicException as intended. Commits ------- 244ba08 [Console] Fix ProgressBar %remaining% and %estimated% placeholder guards
2 parents 596e51f + 244ba08 commit 1c7c255

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

src/Symfony/Component/Console/Helper/ProgressBar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,14 +578,14 @@ private static function initPlaceholderFormatters(): array
578578
},
579579
'elapsed' => fn (self $bar) => Helper::formatTime(time() - $bar->getStartTime(), 2),
580580
'remaining' => function (self $bar) {
581-
if (null === $bar->getMaxSteps()) {
581+
if (null === $bar->max) {
582582
throw new LogicException('Unable to display the remaining time if the maximum number of steps is not set.');
583583
}
584584

585585
return Helper::formatTime($bar->getRemaining(), 2);
586586
},
587587
'estimated' => function (self $bar) {
588-
if (null === $bar->getMaxSteps()) {
588+
if (null === $bar->max) {
589589
throw new LogicException('Unable to display the estimated time if the maximum number of steps is not set.');
590590
}
591591

src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\Attributes\DataProvider;
1515
use PHPUnit\Framework\Attributes\Group;
1616
use PHPUnit\Framework\TestCase;
17+
use Symfony\Component\Console\Exception\LogicException;
1718
use Symfony\Component\Console\Formatter\OutputFormatter;
1819
use Symfony\Component\Console\Helper\Helper;
1920
use Symfony\Component\Console\Helper\ProgressBar;
@@ -1371,4 +1372,24 @@ public function testGetNotSetMessage()
13711372

13721373
$this->assertNull($progressBar->getMessage());
13731374
}
1375+
1376+
public function testRemainingWithoutMaxThrowsLogicException()
1377+
{
1378+
$this->expectException(LogicException::class);
1379+
1380+
$bar = new ProgressBar($this->getOutputStream());
1381+
$bar->setFormat('%remaining%');
1382+
$bar->start();
1383+
$bar->advance();
1384+
}
1385+
1386+
public function testEstimatedWithoutMaxThrowsLogicException()
1387+
{
1388+
$this->expectException(LogicException::class);
1389+
1390+
$bar = new ProgressBar($this->getOutputStream());
1391+
$bar->setFormat('%estimated%');
1392+
$bar->start();
1393+
$bar->advance();
1394+
}
13741395
}

src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ public function testBlockWithWindowsLineEndings()
100100
$code = static function (InputInterface $input, OutputInterface $output) {
101101
$io = new SymfonyStyle($input, $output);
102102
$io->block("First line.\r\nSecond line.", 'INFO', 'fg=white;bg=blue', ' ', true);
103+
104+
return Command::SUCCESS;
103105
};
104106

105107
$this->command->setCode($code);

0 commit comments

Comments
 (0)