TinyProgressBarTest::invalidLengthsData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of graze/parallel-process.
4
 *
5
 * Copyright © 2018 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/parallel-process/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/parallel-process
12
 */
13
14
namespace Graze\ParallelProcess\Test\Unit\Display;
15
16
use Graze\ParallelProcess\Display\TinyProgressBar;
17
use Graze\ParallelProcess\Test\TestCase;
18
19
class TinyProgressBarTest extends TestCase
20
{
21
    /**
22
     * @dataProvider barDataProvider
23
     *
24
     * @param int    $length
25
     * @param float  $position
26
     * @param float  $max
27
     * @param string $expected
28
     * @param string $format
29
     * @param array  $barChars
30
     */
31
    public function testBar(
32
        $length,
33
        $position,
34
        $max,
35
        $expected,
36
        $format = '▕{bar}▏{perc} {position}/{max}',
37
        array $barChars = []
38
    ) {
39
        $bar = new TinyProgressBar($length, $format, $max);
40
41
        if (count($barChars) > 0) {
42
            $bar->setBarCharacters($barChars);
43
        }
44
45
        $bar->setPosition($position);
46
47
        $this->assertEquals($expected, $bar->render());
48
    }
49
50
    /**
51
     * @return array
52
     */
53
    public function barDataProvider()
54
    {
55
        return [
56
            [3, 10, 100, '▕▍  ▏ 10% 10/100'],
57
            [3, 1, 100, '▕▏  ▏  1% 1/100'], // anything over 0 will display something
58
            [3, 0, 100, '▕   ▏  0% 0/100'],
59
            [3, 100, 100, '▕███▏100% 100/100'],
60
            [3, 99, 100, '▕██▉▏ 99% 99/100'], // 99% will display less than 100%
61
            [5, 20, 100, '▕█    ▏ 20% 20/100'],
62
            [3, 0.3, 1, '▕▉  ▏ 30% 0.3/1'],
63
            [10, 12.5, 120, '▕█         ▏ 10% 12.5/120'],
64
            [3, -10, 100, '▕   ▏  0% 0/100'], // position must be greater than or equal to than 0
65
            [3, 120, 100, '▕███▏100% 100/100'], // position must be less than or equal to max
66
            [3, 100, 100, '▕███▏100%', '▕{bar}▏{perc}'],
67
            [3, 100, 100, '▕███▏', '▕{bar}▏'],
68
            [3, 100, 100, '███', '{bar}'],
69
            [3, 100, 100, '100/100', '{position}/{max}'],
70
            [3, 100, 100, '100%', '{perc}'],
71
            [10, 15, 100, '▕█▄        ▏ 15% 15/100', '▕{bar}▏{perc} {position}/{max}', [" ", "▄", "█"]],
72
        ];
73
    }
74
75
    /**
76
     * @dataProvider invalidLengthsData
77
     * @expectedException \InvalidArgumentException
78
     *
79
     * @param int $length
80
     */
81
    public function testInvalidLengthWillThrowAnException($length)
82
    {
83
        new TinyProgressBar($length);
84
    }
85
86
    /**
87
     * @return array
88
     */
89
    public function invalidLengthsData()
90
    {
91
        return [
92
            [0],
93
            [-1],
94
            [-9223372036854775808],
95
        ];
96
    }
97
98
    /**
99
     * @dataProvider invalidMaxData
100
     * @expectedException \InvalidArgumentException
101
     *
102
     * @param int $max
103
     */
104
    public function testInvalidMaximumValuesWillThrowAnException($max)
105
    {
106
        new TinyProgressBar(3, TinyProgressBar::FORMAT_DEFAULT, $max);
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    public function invalidMaxData()
113
    {
114
        return [
115
            [0],
116
            [-1],
117
            [-9223372036854775808],
118
        ];
119
    }
120
121
    public function testAccessors()
122
    {
123
        $bar = new TinyProgressBar(5);
124
125
        $this->assertEquals(0, $bar->getPosition());
126
        $this->assertEquals(100, $bar->getMax());
127
        $this->assertEquals(5, $bar->getLength());
128
        $this->assertEquals(TinyProgressBar::FORMAT_DEFAULT, $bar->getFormat());
129
130
        $this->assertSame($bar, $bar->setPosition(5));
131
        $this->assertSame($bar, $bar->setMax(200));
132
        $this->assertSame($bar, $bar->setLength(10));
133
        $this->assertSame($bar, $bar->setFormat(TinyProgressBar::FORMAT_SHORT));
134
135
        $this->assertEquals(5, $bar->getPosition());
136
        $this->assertEquals(200, $bar->getMax());
137
        $this->assertEquals(10, $bar->getLength());
138
        $this->assertEquals(TinyProgressBar::FORMAT_SHORT, $bar->getFormat());
139
    }
140
141
    public function testAdvance()
142
    {
143
        $bar = new TinyProgressBar(5);
144
145
        $this->assertEquals(0, $bar->getPosition());
146
147
        $this->assertSame($bar, $bar->advance());
148
        $this->assertEquals(1, $bar->getPosition());
149
150
        $this->assertSame($bar, $bar->advance(3));
151
        $this->assertEquals(4, $bar->getPosition());
152
153
        $this->assertSame($bar, $bar->advance(0.2));
154
        $this->assertEquals(4.2, $bar->getPosition(), '', 0.00001);
155
    }
156
}
157