Skip to content

Commit b5e2bf8

Browse files
committed
test: progress bar functionality
1 parent 733e21d commit b5e2bf8

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

src/Output/ProgressBar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ protected function getBarStrLen(): int
280280
{
281281
if (!$this->barStrLen) {
282282
// Subtract 10 because of the '> 100%' plus some padding, max 100
283-
$this->barStrLen = min($this->terminal->width() - 10, 100);
283+
$this->barStrLen = max(50, min($this->terminal->width() - 10, 100));
284284
}
285285

286286
return $this->barStrLen;

tests/Output/ProgressBarTest.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP-CLI package.
5+
*
6+
* (c) Jitendra Adhikari <[email protected]>
7+
* <https://github.com/adhocore>
8+
*
9+
* Licensed under MIT license.
10+
*/
11+
12+
namespace Ahc\Cli\Test\Output;
13+
14+
use Ahc\Cli\Helper\Terminal;
15+
use Ahc\Cli\Output\ProgressBar;
16+
use Ahc\Cli\Output\Writer;
17+
use Ahc\Cli\Test\CliTestCase;
18+
use ArrayIterator;
19+
use IteratorAggregate;
20+
use UnexpectedValueException;
21+
22+
class ProgressBarTest extends CliTestCase
23+
{
24+
public function test_progress_bar()
25+
{
26+
$progress = new ProgressBar(10, new Writer(static::$ou));
27+
for ($i = 1; $i <= 10; $i++) {
28+
$progress->advance(1, "$i x label");
29+
if ($i === 2) {
30+
$progress->forceRedraw(true);
31+
$progress->current(2, "2 x label");
32+
$progress->forceRedraw(false);
33+
}
34+
}
35+
36+
$this->assertBufferContains('===========================================> 100%');
37+
$this->assertBufferContains('10 x label');
38+
39+
$this->expectException(UnexpectedValueException::class);
40+
$progress->current(11);
41+
}
42+
43+
public function test_progress_bar_option()
44+
{
45+
$progress = new ProgressBar(10, new Writer(static::$ou));
46+
$progress->option('loader', '#');
47+
$progress->option(['pointer' => '~']);
48+
for ($i = 1; $i <= 10; $i++) {
49+
$label = $i === 5 ? '' : '#' . ($i % 3) . ' label';
50+
$progress->advance(1, $label);
51+
}
52+
53+
$this->assertBufferContains('###########################################~ 100%');
54+
$this->assertBufferContains('#1 label');
55+
56+
$this->expectException(UnexpectedValueException::class);
57+
$progress->option('color', '');
58+
}
59+
60+
public function test_progress_bar_each()
61+
{
62+
$progress = new ProgressBar(3, new Writer(static::$ou));
63+
$progress->each([]);
64+
$progress->each(new class() implements IteratorAggregate {
65+
public $a = 1;
66+
public $b = 2;
67+
public $c = 3;
68+
69+
public function getIterator()
70+
{
71+
return new ArrayIterator($this);
72+
}
73+
}, fn ($v, $k) => "$k: $v");
74+
75+
$this->assertBufferContains('===========================================> 100%');
76+
$this->assertBufferContains('c: 3');
77+
$this->assertNotNull((new Terminal)->height());
78+
79+
$this->expectException(UnexpectedValueException::class);
80+
(new ProgressBar(1))->current(2);
81+
}
82+
}

0 commit comments

Comments
 (0)