WrapperTest::trimData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 49
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 23
nc 1
nop 0
dl 0
loc 49
rs 9.552
c 1
b 0
f 0
1
<?php
2
/**
3
 * This file is part of graze/console-diff-renderer.
4
 *
5
 * Copyright (c) 2017 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/console-diff-renderer/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/console-diff-renderer
12
 */
13
14
namespace Graze\DiffRenderer\Test\Unit\Wrap;
15
16
use Graze\DiffRenderer\Terminal\DimensionsInterface;
17
use Graze\DiffRenderer\Terminal\Terminal;
18
use Graze\DiffRenderer\Test\TestCase;
19
use Graze\DiffRenderer\Wrap\Wrapper;
20
use Mockery;
21
22
class WrapperTest extends TestCase
23
{
24
    public function setUp()
25
    {
26
        mb_internal_encoding("UTF-8");
27
    }
28
29
    public function testWrapper()
30
    {
31
        $dimensions = Mockery::mock(DimensionsInterface::class);
32
        $dimensions->shouldReceive('getWidth')
33
                   ->andReturn(10, 20);
34
        $terminal = new Terminal(null, $dimensions);
0 ignored issues
show
Bug introduced by
$dimensions of type Mockery\MockInterface is incompatible with the type Graze\DiffRenderer\Termi...imensionsInterface|null expected by parameter $dimensions of Graze\DiffRenderer\Termi...Terminal::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        $terminal = new Terminal(null, /** @scrutinizer ignore-type */ $dimensions);
Loading history...
35
        $wrapper = new Wrapper($terminal);
36
37
        $this->assertEquals(10, $wrapper->getWidth());
38
        $this->assertEquals(20, $wrapper->getWidth());
39
    }
40
41
    /**
42
     * @dataProvider wrapData
43
     *
44
     * @param string|string[] $input
45
     * @param int             $width
46
     * @param string[]        $expected
47
     */
48
    public function testWrap($input, $width, array $expected)
49
    {
50
        $dimensions = Mockery::mock(DimensionsInterface::class);
51
        $dimensions->shouldReceive('getWidth')
52
                   ->andReturn($width);
53
        $terminal = new Terminal(null, $dimensions);
0 ignored issues
show
Bug introduced by
$dimensions of type Mockery\MockInterface is incompatible with the type Graze\DiffRenderer\Termi...imensionsInterface|null expected by parameter $dimensions of Graze\DiffRenderer\Termi...Terminal::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        $terminal = new Terminal(null, /** @scrutinizer ignore-type */ $dimensions);
Loading history...
54
        $wrapper = new Wrapper($terminal);
55
56
        $this->assertEquals($expected, $wrapper->wrap($input));
0 ignored issues
show
Bug introduced by
It seems like $input can also be of type string; however, parameter $input of Graze\DiffRenderer\Wrap\Wrapper::wrap() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
        $this->assertEquals($expected, $wrapper->wrap(/** @scrutinizer ignore-type */ $input));
Loading history...
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    public function wrapData()
63
    {
64
        return [
65
            [ // simple string
66
                ['1234567890'],
67
                5,
68
                [
69
                    '12345',
70
                    '67890',
71
                ],
72
            ],
73
            [ // boundary test
74
                ['123456'],
75
                5,
76
                ['12345', '6'],
77
            ],
78
            [ // array of strings
79
                [
80
                    '1234567890123456789012345678901234567890',
81
                    '123456789012345678901234567890',
82
                ],
83
                20,
84
                [
85
                    '12345678901234567890',
86
                    '12345678901234567890',
87
                    '12345678901234567890',
88
                    '1234567890',
89
                ],
90
            ],
91
            [ // support multi-byte characters
92
                ['😀😃😄😁😆😅😂🤣☺😊😇🙂🙃😉😌😍😘😗😙😚😋😜😝😛🤑🤗🤓😎🤡🤠😏😒😞😔😟😕🙁😣😖😫😩😤😠😡😶😐😑😯😦😧😮😲😵😳😱😨😰😢😥'],
93
                20,
94
                [
95
                    '😀😃😄😁😆😅😂🤣☺😊😇🙂🙃😉😌😍😘😗😙😚',
96
                    '😋😜😝😛🤑🤗🤓😎🤡🤠😏😒😞😔😟😕🙁😣😖😫',
97
                    '😩😤😠😡😶😐😑😯😦😧😮😲😵😳😱😨😰😢😥',
98
                ],
99
            ],
100
            [ // strip tags and wrap on non stripped version
101
                [
102
                    "\e[13m1234567890\e[42m1234567890\e[15m12345678901234567890\e[42m",
103
                    "\e[17m12345678901234567890\e[42m1234567890",
104
                ],
105
                20,
106
                [
107
                    "\e[13m1234567890\e[42m1234567890\e[15m",
108
                    "12345678901234567890\e[42m",
109
                    "\e[17m12345678901234567890\e[42m",
110
                    '1234567890',
111
                ],
112
            ],
113
            [ // ignore tags
114
                ['<info>info</info>infowarning<warning>warning</warning>warning'],
115
                10,
116
                [
117
                    '<info>info',
118
                    '</info>inf',
119
                    'owarning<w',
120
                    'arning>war',
121
                    'ning</warn',
122
                    'ing>warnin',
123
                    'g'
124
                ],
125
            ],
126
        ];
127
    }
128
129
    /**
130
     * @dataProvider trimData
131
     *
132
     * @param string|string[] $input
133
     * @param int             $width
134
     * @param array           $expected
135
     */
136
    public function testTrim($input, $width, array $expected)
137
    {
138
        $dimensions = Mockery::mock(DimensionsInterface::class);
139
        $dimensions->shouldReceive('getWidth')
140
                   ->andReturn($width);
141
        $terminal = new Terminal(null, $dimensions);
0 ignored issues
show
Bug introduced by
$dimensions of type Mockery\MockInterface is incompatible with the type Graze\DiffRenderer\Termi...imensionsInterface|null expected by parameter $dimensions of Graze\DiffRenderer\Termi...Terminal::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

141
        $terminal = new Terminal(null, /** @scrutinizer ignore-type */ $dimensions);
Loading history...
142
        $wrapper = new Wrapper($terminal);
143
144
        $this->assertEquals($expected, $wrapper->trim($input));
0 ignored issues
show
Bug introduced by
It seems like $input can also be of type string; however, parameter $input of Graze\DiffRenderer\Wrap\Wrapper::trim() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

144
        $this->assertEquals($expected, $wrapper->trim(/** @scrutinizer ignore-type */ $input));
Loading history...
145
    }
146
147
    /**
148
     * @return array
149
     */
150
    public function trimData()
151
    {
152
        return [
153
            [ // simple string
154
                ['1234567890'],
155
                5,
156
                ['12345'],
157
            ],
158
            [ // boundary test
159
                ['123456'],
160
                5,
161
                ['12345'],
162
            ],
163
            [
164
                ["\x20\x20\x20\x20\x20"],
165
                3,
166
                ["\x20\x20\x20"],
167
            ],
168
            [ // array of strings
169
                [
170
                    '1234567890123456789012345678901234567890',
171
                    '123456789012345678901234567890',
172
                ],
173
                20,
174
                [
175
                    '12345678901234567890',
176
                    '12345678901234567890',
177
                ],
178
            ],
179
            [ // support multi-byte characters
180
                ['😀😃😄😁😆😅😂🤣☺😊😇🙂🙃😉😌😍😘😗😙😚😋😜😝😛🤑🤗🤓😎🤡🤠😏😒😞😔😟😕🙁😣😖😫😩😤😠😡😶😐😑😯😦😧😮😲😵😳😱😨😰😢😥'],
181
                20,
182
                ['😀😃😄😁😆😅😂🤣☺😊😇🙂🙃😉😌😍😘😗😙😚'],
183
            ],
184
            [ // strip tags and trim on non stripped version
185
                [
186
                    "\e[13m1234567890\e[42m1234567890\e[15m12345678901234567890\e[42m",
187
                    "\e[17m12345678901234567890\e[42m1234567890",
188
                ],
189
                20,
190
                [
191
                    "\e[13m1234567890\e[42m1234567890\e[15m",
192
                    "\e[17m12345678901234567890\e[42m",
193
                ],
194
            ],
195
            [ // ignore tags
196
                ['<info>info</info>infowarning<warning>warning</warning>warning'],
197
                18,
198
                ['<info>info</info>i'],
199
            ],
200
        ];
201
    }
202
}
203