Completed
Push — master ( dfd9c5...ccebf0 )
by Mark
161:27 queued 155:49
created

PhpSpreadsheetTests/Functional/ReadFilterTest.php (1 issue)

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Functional;
4
5
use PhpOffice\PhpSpreadsheet\Reader\IReadFilter;
6
use PhpOffice\PhpSpreadsheet\Spreadsheet;
7
8
class ReadFilterTest extends AbstractFunctional
9
{
10
    public function providerCellsValues()
11
    {
12
        $cellValues = [
13
            // one argument as a multidimensional array
14
            [1, 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'],
15
            [2, 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'],
16
            [3, 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'],
17
            [4, 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'],
18
            [5, 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'],
19
            [6, 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'],
20
            [7, 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'],
21
            [8, 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'],
22
            [9, 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'],
23
            [10, 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'],
24
        ];
25
26
        return [
27
            ['Xlsx', $cellValues],
28
            ['Ods', $cellValues],
29
        ];
30
    }
31
32
    /**
33
     * Test load Xlsx file with many empty cells with no filter used.
34
     *
35
     * @dataProvider providerCellsValues
36
     *
37
     * @param  array $arrayData
38
     * @param mixed $format
39
     */
40
    public function testXlsxLoadWithoutReadFilter($format, array $arrayData)
41
    {
42
        $spreadsheet = new Spreadsheet();
43
44
        $spreadsheet->getActiveSheet()->fromArray($arrayData, null, 'A1');
45
46
        $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format);
47
        $sheet = $reloadedSpreadsheet->getSheet(0);
48
        // test highest column (very specific num of columns because of some 3rd party software)
49
        self::assertSame('J', $sheet->getHighestColumn());
50
51
        // test highest row (very specific num of rows because of some 3rd party software)
52
        self::assertEquals(10, $sheet->getHighestRow());
53
54
        // test top left coordinate
55
        $sortedCoordinates = $sheet->getCellCollection()->getSortedCoordinates();
56
        $coordinateTopLeft = reset($sortedCoordinates);
57
        self::assertSame('A1', $coordinateTopLeft);
58
    }
59
60
    /**
61
     * Test load Xlsx file with many empty cells (and big max row number) with readfilter.
62
     *
63
     * @dataProvider providerCellsValues
64
     *
65
     * @param array $arrayData
66
     * @param mixed $format
67
     */
68
    public function testXlsxLoadWithReadFilter($format, array $arrayData)
69
    {
70
        $spreadsheet = new Spreadsheet();
71
        $spreadsheet->getActiveSheet()->fromArray($arrayData, null, 'A1');
72
73
        $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format, function ($reader) {
74
            // Create a stub for the readFilter class.
75
            $readFilterStub = $this->createMock(IReadFilter::class);
76
            $readFilterStub->method('readCell')
77
                ->will($this->returnCallback([$this, 'readFilterReadCell']));
78
            // apply filter
79
            $reader->setReadFilter($readFilterStub);
80
        });
81
        $sheet = $reloadedSpreadsheet->getSheet(0);
82
        // test highest column (very specific num of columns because of some 3rd party software)
83
        self::assertSame('D', $sheet->getHighestColumn());
84
85
        // test highest row (very specific num of rows because of some 3rd party software)
86
        self::assertEquals(6, $sheet->getHighestRow());
87
88
        // test top left coordinate
89
        $sortedCoordinates = $sheet->getCellCollection()->getSortedCoordinates();
90
        $coordinateTopLeft = reset($sortedCoordinates);
91
        self::assertSame('B2', $coordinateTopLeft);
92
    }
93
94
    /**
95
     * @see \PhpOffice\PhpSpreadsheet\Reader\IReadFilter::readCell()
96
     *
97
     * @param string $column Column address (as a string value like "A", or "IV")
98
     * @param int $row Row number
99
     * @param string $worksheetName Optional worksheet name
100
     *
101
     * @return bool
102
     */
103
    public function readFilterReadCell($column, $row, $worksheetName = '')
0 ignored issues
show
The parameter $worksheetName is not used and could be removed. ( Ignorable by Annotation )

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

103
    public function readFilterReadCell($column, $row, /** @scrutinizer ignore-unused */ $worksheetName = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
104
    {
105
        // define filter range
106
        $rowMin = 2;
107
        $rowMax = 6;
108
        $columnMin = 'B';
109
        $columnMax = 'D';
110
111
        $r = (int) $row;
112
        if ($r > $rowMax || $r < $rowMin) {
113
            return false;
114
        }
115
116
        $col = sprintf('%04s', $column);
117
        if ($col > sprintf('%04s', $columnMax) ||
118
            $col < sprintf('%04s', $columnMin)) {
119
            return false;
120
        }
121
122
        return true;
123
    }
124
}
125