MultipleBarTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 84
dl 0
loc 154
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A test_one_line() 0 19 2
A test_throw_exception() 0 10 1
A test_one_line_on_stderror() 0 20 2
A setUp() 0 5 1
A test_two_line_unknown_length() 0 41 3
A test_two_line_known_length() 0 35 3
1
<?php
2
3
namespace coExp\WunderBar\Tests;
4
5
use coExp\WunderBar\Exception\MultipleBarConfigurationException;
6
use coExp\WunderBar\MultipleBar;
7
use DateTime;
8
use \Symfony\Component\Console\Helper\ProgressBar as ProgressBar;
9
use PHPUnit\Framework\TestCase;
10
use Symfony\Component\Console\Output\ConsoleOutput;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class MultipleBarTest extends TestCase
14
{
15
    /** @var OutputInterface */
16
    protected $output;
17
18
    public function setUp()
19
    {
20
        parent::setUp();
21
22
        $this->output = new ConsoleOutput();
23
    }
24
25
    /**
26
     * @throws MultipleBarConfigurationException
27
     */
28
    public function test_throw_exception()
29
    {
30
        $this->expectException(MultipleBarConfigurationException::class);
31
32
        $mb = (new MultipleBar($this->output))
33
            ->setTitle(__METHOD__)
34
            ->addProgressBar(-2)
35
            ->show();
36
37
        $mb->erase();
38
    }
39
40
    /**
41
     * @throws MultipleBarConfigurationException
42
     */
43
    public function test_one_line()
44
    {
45
        $mb = (new MultipleBar($this->output))
46
            ->setTitle(__METHOD__.': '.(new DateTime())->format(DATE_ATOM))
47
            ->addProgressBar();
48
49
        $mb->getProgressBarByIndex(0)
50
            ->setMaxSteps(20);
51
52
        $this->assertInstanceOf(ProgressBar::class, $mb->getProgressBarByIndex(0));
53
        $this->assertNull($mb->getProgressBarByIndex(1));
54
55
        for ($i = 0 ; $i < 20 ; $i++) {
56
            $mb->getProgressBarByIndex(0)->advance();
57
            $mb->show();
58
            usleep(100000);
59
        }
60
61
        $mb->erase();
62
    }
63
64
    /**
65
     * @throws MultipleBarConfigurationException
66
     */
67
    public function test_one_line_on_stderror()
68
    {
69
        $mb = (new MultipleBar($this->output))
70
            ->setStdError(true)
71
            ->setTitle(__METHOD__.': '.(new DateTime())->format(DATE_ATOM))
72
            ->addProgressBar();
73
74
        $mb->getProgressBarByIndex(0)
75
            ->setMaxSteps(20);
76
77
        $this->assertInstanceOf(ProgressBar::class, $mb->getProgressBarByIndex(0));
78
        $this->assertNull($mb->getProgressBarByIndex(1));
79
80
        for ($i = 0 ; $i < 20 ; $i++) {
81
            $mb->getProgressBarByIndex(0)->advance();
82
            $mb->show();
83
            usleep(100000);
84
        }
85
86
        $mb->erase();
87
    }
88
89
    public function test_two_line_unknown_length()
90
    {
91
        $mb = (new MultipleBar($this->output))
92
            ->setTitle(__METHOD__.': '.(new DateTime())->format(DATE_ATOM))
93
            ->addProgressBarByName(['Master']);
94
95
        /** @see https://symfony.com/doc/current/components/console/helpers/progressbar.html#custom-formats */
96
        ProgressBar::setFormatDefinition('custom', '%message%: %percent%% %current%/%max% |%bar%');
97
98
        $masterProgressBar = $mb->getProgressBarByName('Master');
99
        $masterProgressBar->setMaxSteps(4);
100
        $masterProgressBar->setMessage('Master');
101
        $masterProgressBar->setFormat('custom');
102
103
        $this->assertInstanceOf(ProgressBar::class, $mb->getProgressBarByName('Master'));
104
        $this->assertNull($mb->getProgressBarByName('Child'));
105
106
        for ($i = 0 ; $i < 4 ; $i++) {
107
            $mb->addProgressBarByName(['Child']);
108
109
            $this->assertInstanceOf(ProgressBar::class, $mb->getProgressBarByName('Child'));
110
            $this->assertNull($mb->getProgressBarByName('Child 2'));
111
112
            $numberStuff = rand(23, 56);
113
            $childProgressBar = $mb->getProgressBarByName('Child');
114
            $childProgressBar->setMaxSteps($numberStuff);
115
            $childProgressBar->setMessage("Children #$i");
116
            $childProgressBar->setFormat('custom');
117
118
            for ($j = 0; $j < $numberStuff; $j++) {
119
                $mb->getProgressBarByName('Child')->advance();
120
                $mb->show();
121
                usleep(100000);
122
            }
123
124
            $mb->removeProgressBarByName('Child');
125
126
            $mb->getProgressBarByName('Master')->advance();
127
        }
128
129
        $mb->erase();
130
    }
131
132
    public function test_two_line_known_length()
133
    {
134
        $length = [23, 24, 25, 26]; // total = 98
135
136
        $mb = (new MultipleBar($this->output))
137
            ->setTitle(__METHOD__.': '.(new DateTime())->format(DATE_ATOM))
138
            ->addProgressBarByName(['Master', 'Child']);
139
140
        /** @see https://symfony.com/doc/current/components/console/helpers/progressbar.html#custom-formats */
141
        ProgressBar::setFormatDefinition('custom', '%message%: %percent%% %current%/%max% |%bar%');
142
143
        $masterProgressBar = $mb->getProgressBarByName('Master');
144
        $masterProgressBar->setMaxSteps(98);
145
        $masterProgressBar->setMessage('Master');
146
        $masterProgressBar->setFormat('custom');
147
148
        $this->assertInstanceOf(ProgressBar::class, $mb->getProgressBarByName('Master'));
149
        $this->assertInstanceOf(ProgressBar::class, $mb->getProgressBarByName('Child'));
150
151
        foreach ($length as $i => $numberStuff) {
152
            $childProgressBar = $mb->getProgressBarByName('Child');
153
            $childProgressBar->setProgress(0);
154
            $childProgressBar->setMaxSteps($numberStuff);
155
            $childProgressBar->setMessage("Children #$i");
156
            $childProgressBar->setFormat('custom');
157
158
            for ($j = 0; $j < $numberStuff; $j++) {
159
                $mb->getProgressBarByName('Child')->advance();
160
                $mb->getProgressBarByName('Master')->advance();
161
                $mb->show();
162
                usleep(100000);
163
            }
164
        }
165
166
        $mb->erase();
167
    }
168
}
169