Completed
Push — develop ( 4e0344...49775b )
by Adrien
27:07
created

CoordinateTest::testMergeRangesInCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Cell;
4
5
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
6
use PhpOffice\PhpSpreadsheet\Exception;
7
use PHPUnit\Framework\TestCase;
8
9
class CoordinateTest extends TestCase
10
{
11
    /**
12
     * @dataProvider providerColumnString
13
     *
14
     * @param mixed $expectedResult
15
     * @param mixed $string
16
     */
17
    public function testColumnIndexFromString($expectedResult, $string)
18
    {
19
        $columnIndex = Coordinate::columnIndexFromString($string);
20
        self::assertEquals($expectedResult, $columnIndex);
21
22
        $stringBack = Coordinate::stringFromColumnIndex($columnIndex);
23
        self::assertEquals($stringBack, $string, 'should be able to get the original input with opposite method');
24
    }
25
26
    public function providerColumnString()
27
    {
28
        return require 'data/ColumnString.php';
29
    }
30
31
    public function testColumnIndexFromStringTooLong()
32
    {
33
        $cellAddress = 'ABCD';
34
35
        try {
36
            Coordinate::columnIndexFromString($cellAddress);
37
        } catch (\Exception $e) {
38
            self::assertInstanceOf(Exception::class, $e);
39
            self::assertEquals($e->getMessage(), 'Column string index can not be longer than 3 characters');
40
41
            return;
42
        }
43
        $this->fail('An expected exception has not been raised.');
44
    }
45
46
    public function testColumnIndexFromStringTooShort()
47
    {
48
        $cellAddress = '';
49
50
        try {
51
            Coordinate::columnIndexFromString($cellAddress);
52
        } catch (\Exception $e) {
53
            self::assertInstanceOf(Exception::class, $e);
54
            self::assertEquals($e->getMessage(), 'Column string index can not be empty');
55
56
            return;
57
        }
58
        $this->fail('An expected exception has not been raised.');
59
    }
60
61
    /**
62
     * @dataProvider providerColumnIndex
63
     *
64
     * @param mixed $expectedResult
65
     * @param int $columnIndex
66
     */
67
    public function testStringFromColumnIndex($expectedResult, $columnIndex)
68
    {
69
        $string = Coordinate::stringFromColumnIndex($columnIndex);
70
        self::assertEquals($expectedResult, $string);
71
72
        $columnIndexBack = Coordinate::columnIndexFromString($string);
73
        self::assertEquals($columnIndexBack, $columnIndex, 'should be able to get the original input with opposite method');
74
    }
75
76
    public function providerColumnIndex()
77
    {
78
        return require 'data/ColumnIndex.php';
79
    }
80
81
    /**
82
     * @dataProvider providerCoordinates
83
     *
84
     * @param mixed $expectedResult
85
     */
86
    public function testCoordinateFromString($expectedResult, ...$args)
87
    {
88
        $result = Coordinate::coordinateFromString(...$args);
1 ignored issue
show
Bug introduced by
$args is expanded, but the parameter $pCoordinateString of PhpOffice\PhpSpreadsheet...:coordinateFromString() does not expect variable arguments. ( Ignorable by Annotation )

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

88
        $result = Coordinate::coordinateFromString(/** @scrutinizer ignore-type */ ...$args);
Loading history...
89
        self::assertEquals($expectedResult, $result);
90
    }
91
92
    public function providerCoordinates()
93
    {
94
        return require 'data/CellCoordinates.php';
95
    }
96
97
    public function testCoordinateFromStringWithRangeAddress()
98
    {
99
        $cellAddress = 'A1:AI2012';
100
101
        try {
102
            Coordinate::coordinateFromString($cellAddress);
103
        } catch (\Exception $e) {
104
            self::assertInstanceOf(Exception::class, $e);
105
            self::assertEquals($e->getMessage(), 'Cell coordinate string can not be a range of cells');
106
107
            return;
108
        }
109
        $this->fail('An expected exception has not been raised.');
110
    }
111
112
    public function testCoordinateFromStringWithEmptyAddress()
113
    {
114
        $cellAddress = '';
115
116
        try {
117
            Coordinate::coordinateFromString($cellAddress);
118
        } catch (\Exception $e) {
119
            self::assertInstanceOf(Exception::class, $e);
120
            self::assertEquals($e->getMessage(), 'Cell coordinate can not be zero-length string');
121
122
            return;
123
        }
124
        $this->fail('An expected exception has not been raised.');
125
    }
126
127
    public function testCoordinateFromStringWithInvalidAddress()
128
    {
129
        $cellAddress = 'AI';
130
131
        try {
132
            Coordinate::coordinateFromString($cellAddress);
133
        } catch (\Exception $e) {
134
            self::assertInstanceOf(Exception::class, $e);
135
            self::assertEquals($e->getMessage(), 'Invalid cell coordinate ' . $cellAddress);
136
137
            return;
138
        }
139
        $this->fail('An expected exception has not been raised.');
140
    }
141
142
    /**
143
     * @dataProvider providerAbsoluteCoordinates
144
     *
145
     * @param mixed $expectedResult
146
     */
147
    public function testAbsoluteCoordinateFromString($expectedResult, ...$args)
148
    {
149
        $result = Coordinate::absoluteCoordinate(...$args);
1 ignored issue
show
Bug introduced by
$args is expanded, but the parameter $pCoordinateString of PhpOffice\PhpSpreadsheet...e::absoluteCoordinate() does not expect variable arguments. ( Ignorable by Annotation )

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

149
        $result = Coordinate::absoluteCoordinate(/** @scrutinizer ignore-type */ ...$args);
Loading history...
150
        self::assertEquals($expectedResult, $result);
151
    }
152
153
    public function providerAbsoluteCoordinates()
154
    {
155
        return require 'data/CellAbsoluteCoordinate.php';
156
    }
157
158
    public function testAbsoluteCoordinateFromStringWithRangeAddress()
159
    {
160
        $cellAddress = 'A1:AI2012';
161
162
        try {
163
            Coordinate::absoluteCoordinate($cellAddress);
164
        } catch (\Exception $e) {
165
            self::assertInstanceOf(Exception::class, $e);
166
            self::assertEquals($e->getMessage(), 'Cell coordinate string can not be a range of cells');
167
168
            return;
169
        }
170
        $this->fail('An expected exception has not been raised.');
171
    }
172
173
    /**
174
     * @dataProvider providerAbsoluteReferences
175
     *
176
     * @param mixed $expectedResult
177
     */
178
    public function testAbsoluteReferenceFromString($expectedResult, ...$args)
179
    {
180
        $result = Coordinate::absoluteReference(...$args);
1 ignored issue
show
Bug introduced by
$args is expanded, but the parameter $pCoordinateString of PhpOffice\PhpSpreadsheet...te::absoluteReference() does not expect variable arguments. ( Ignorable by Annotation )

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

180
        $result = Coordinate::absoluteReference(/** @scrutinizer ignore-type */ ...$args);
Loading history...
181
        self::assertEquals($expectedResult, $result);
182
    }
183
184
    public function providerAbsoluteReferences()
185
    {
186
        return require 'data/CellAbsoluteReference.php';
187
    }
188
189
    public function testAbsoluteReferenceFromStringWithRangeAddress()
190
    {
191
        $cellAddress = 'A1:AI2012';
192
193
        try {
194
            Coordinate::absoluteReference($cellAddress);
195
        } catch (\Exception $e) {
196
            self::assertInstanceOf(Exception::class, $e);
197
            self::assertEquals($e->getMessage(), 'Cell coordinate string can not be a range of cells');
198
199
            return;
200
        }
201
        $this->fail('An expected exception has not been raised.');
202
    }
203
204
    /**
205
     * @dataProvider providerSplitRange
206
     *
207
     * @param mixed $expectedResult
208
     */
209
    public function testSplitRange($expectedResult, ...$args)
210
    {
211
        $result = Coordinate::splitRange(...$args);
1 ignored issue
show
Bug introduced by
$args is expanded, but the parameter $pRange of PhpOffice\PhpSpreadsheet...oordinate::splitRange() does not expect variable arguments. ( Ignorable by Annotation )

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

211
        $result = Coordinate::splitRange(/** @scrutinizer ignore-type */ ...$args);
Loading history...
212
        foreach ($result as $key => $split) {
213
            if (!is_array($expectedResult[$key])) {
214
                self::assertEquals($expectedResult[$key], $split[0]);
215
            } else {
216
                self::assertEquals($expectedResult[$key], $split);
217
            }
218
        }
219
    }
220
221
    public function providerSplitRange()
222
    {
223
        return require 'data/CellSplitRange.php';
224
    }
225
226
    /**
227
     * @dataProvider providerBuildRange
228
     *
229
     * @param mixed $expectedResult
230
     */
231
    public function testBuildRange($expectedResult, ...$args)
232
    {
233
        $result = Coordinate::buildRange(...$args);
1 ignored issue
show
Bug introduced by
$args is expanded, but the parameter $pRange of PhpOffice\PhpSpreadsheet...oordinate::buildRange() does not expect variable arguments. ( Ignorable by Annotation )

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

233
        $result = Coordinate::buildRange(/** @scrutinizer ignore-type */ ...$args);
Loading history...
234
        self::assertEquals($expectedResult, $result);
235
    }
236
237
    public function providerBuildRange()
238
    {
239
        return require 'data/CellBuildRange.php';
240
    }
241
242
    /**
243
     * @expectedException \TypeError
244
     */
245
    public function testBuildRangeInvalid()
246
    {
247
        if (PHP_MAJOR_VERSION < 7) {
248
            $this->markTestSkipped('Cannot catch type hinting error with PHP 5.6');
249
        }
250
251
        $cellRange = '';
252
        Coordinate::buildRange($cellRange);
0 ignored issues
show
Bug introduced by
$cellRange of type string is incompatible with the type array expected by parameter $pRange of PhpOffice\PhpSpreadsheet...oordinate::buildRange(). ( Ignorable by Annotation )

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

252
        Coordinate::buildRange(/** @scrutinizer ignore-type */ $cellRange);
Loading history...
253
    }
254
255
    /**
256
     * @dataProvider providerRangeBoundaries
257
     *
258
     * @param mixed $expectedResult
259
     */
260
    public function testRangeBoundaries($expectedResult, ...$args)
261
    {
262
        $result = Coordinate::rangeBoundaries(...$args);
1 ignored issue
show
Bug introduced by
$args is expanded, but the parameter $pRange of PhpOffice\PhpSpreadsheet...nate::rangeBoundaries() does not expect variable arguments. ( Ignorable by Annotation )

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

262
        $result = Coordinate::rangeBoundaries(/** @scrutinizer ignore-type */ ...$args);
Loading history...
263
        self::assertEquals($expectedResult, $result);
264
    }
265
266
    public function providerRangeBoundaries()
267
    {
268
        return require 'data/CellRangeBoundaries.php';
269
    }
270
271
    /**
272
     * @dataProvider providerRangeDimension
273
     *
274
     * @param mixed $expectedResult
275
     */
276
    public function testRangeDimension($expectedResult, ...$args)
277
    {
278
        $result = Coordinate::rangeDimension(...$args);
1 ignored issue
show
Bug introduced by
$args is expanded, but the parameter $pRange of PhpOffice\PhpSpreadsheet...inate::rangeDimension() does not expect variable arguments. ( Ignorable by Annotation )

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

278
        $result = Coordinate::rangeDimension(/** @scrutinizer ignore-type */ ...$args);
Loading history...
279
        self::assertEquals($expectedResult, $result);
280
    }
281
282
    public function providerRangeDimension()
283
    {
284
        return require 'data/CellRangeDimension.php';
285
    }
286
287
    /**
288
     * @dataProvider providerGetRangeBoundaries
289
     *
290
     * @param mixed $expectedResult
291
     */
292
    public function testGetRangeBoundaries($expectedResult, ...$args)
293
    {
294
        $result = Coordinate::getRangeBoundaries(...$args);
1 ignored issue
show
Bug introduced by
$args is expanded, but the parameter $pRange of PhpOffice\PhpSpreadsheet...e::getRangeBoundaries() does not expect variable arguments. ( Ignorable by Annotation )

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

294
        $result = Coordinate::getRangeBoundaries(/** @scrutinizer ignore-type */ ...$args);
Loading history...
295
        self::assertEquals($expectedResult, $result);
296
    }
297
298
    public function providerGetRangeBoundaries()
299
    {
300
        return require 'data/CellGetRangeBoundaries.php';
301
    }
302
303
    /**
304
     * @dataProvider providerExtractAllCellReferencesInRange
305
     *
306
     * @param mixed $expectedResult
307
     */
308
    public function testExtractAllCellReferencesInRange($expectedResult, ...$args)
309
    {
310
        $result = Coordinate::extractAllCellReferencesInRange(...$args);
1 ignored issue
show
Bug introduced by
$args is expanded, but the parameter $pRange of PhpOffice\PhpSpreadsheet...CellReferencesInRange() does not expect variable arguments. ( Ignorable by Annotation )

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

310
        $result = Coordinate::extractAllCellReferencesInRange(/** @scrutinizer ignore-type */ ...$args);
Loading history...
311
        self::assertEquals($expectedResult, $result);
312
    }
313
314
    public function providerExtractAllCellReferencesInRange()
315
    {
316
        return require 'data/CellExtractAllCellReferencesInRange.php';
317
    }
318
319
    /**
320
     * @dataProvider providerMergeRangesInCollection
321
     *
322
     * @param mixed $expectedResult
323
     */
324
    public function testMergeRangesInCollection($expectedResult, ...$args)
325
    {
326
        $result = Coordinate::mergeRangesInCollection(...$args);
1 ignored issue
show
Bug introduced by
$args is expanded, but the parameter $pCoordCollection of PhpOffice\PhpSpreadsheet...rgeRangesInCollection() does not expect variable arguments. ( Ignorable by Annotation )

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

326
        $result = Coordinate::mergeRangesInCollection(/** @scrutinizer ignore-type */ ...$args);
Loading history...
327
        self::assertEquals($expectedResult, $result);
328
    }
329
330
    public function providerMergeRangesInCollection()
331
    {
332
        return require 'data/CellMergeRangesInCollection.php';
333
    }
334
335
    /**
336
     * @dataProvider providerCoordinateIsRange
337
     *
338
     * @param mixed $expectedResult
339
     */
340
    public function testCoordinateIsRange($expectedResult, ...$args)
341
    {
342
        $result = Coordinate::coordinateIsRange(...$args);
1 ignored issue
show
Bug introduced by
$args is expanded, but the parameter $coord of PhpOffice\PhpSpreadsheet...te::coordinateIsRange() does not expect variable arguments. ( Ignorable by Annotation )

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

342
        $result = Coordinate::coordinateIsRange(/** @scrutinizer ignore-type */ ...$args);
Loading history...
343
        self::assertEquals($expectedResult, $result);
344
    }
345
346
    public function providerCoordinateIsRange()
347
    {
348
        return require 'data/CoordinateIsRange.php';
349
    }
350
}
351