Completed
Push — master ( 4e7983...366592 )
by Mark
45s queued 37s
created

testIteratorEmptyRowWithColumnLimit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 15
rs 9.9332
cc 1
nc 1
nop 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
4
5
use PhpOffice\PhpSpreadsheet\Cell\DataType;
6
use PhpOffice\PhpSpreadsheet\Spreadsheet;
7
use PhpOffice\PhpSpreadsheet\Worksheet\CellIterator;
8
use PhpOffice\PhpSpreadsheet\Worksheet\RowIterator;
9
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
10
use PHPUnit\Framework\TestCase;
11
12
class RowIteratorEmptyTest extends TestCase
13
{
14
    private static function getPopulatedSheet(Spreadsheet $spreadsheet): Worksheet
15
    {
16
        $sheet = $spreadsheet->getActiveSheet();
17
        $sheet->setCellValueExplicit('A1', 'Hello World', DataType::TYPE_STRING);
18
        $sheet->setCellValueExplicit('B3', null, DataType::TYPE_NULL);
19
        $sheet->setCellValueExplicit('B4', '', DataType::TYPE_STRING);
20
        $sheet->setCellValueExplicit('B5', null, DataType::TYPE_NULL);
21
        $sheet->setCellValueExplicit('C5', '', DataType::TYPE_STRING);
22
        $sheet->setCellValueExplicit('B6', null, DataType::TYPE_NULL);
23
        $sheet->setCellValueExplicit('C6', 'PHP', DataType::TYPE_STRING);
24
        $sheet->setCellValueExplicit('B7', '', DataType::TYPE_STRING);
25
        $sheet->setCellValueExplicit('C7', 'PHP', DataType::TYPE_STRING);
26
        $sheet->setCellValueExplicit('B8', null, DataType::TYPE_NULL);
27
        $sheet->setCellValueExplicit('C8', '', DataType::TYPE_STRING);
28
        $sheet->setCellValueExplicit('D8', 'PHP', DataType::TYPE_STRING);
29
30
        return $sheet;
31
    }
32
33
    /**
34
     * @dataProvider emptyRowBasic
35
     */
36
    public function testIteratorEmptyRow(int $rowId, bool $expectedEmpty): void
37
    {
38
        $spreadsheet = new Spreadsheet();
39
        $sheet = self::getPopulatedSheet($spreadsheet);
40
        $iterator = new RowIterator($sheet, 1, 9);
41
        $iterator->seek($rowId);
42
        $row = $iterator->current();
43
44
        $isEmpty = $row->isEmpty();
45
        self::assertSame($expectedEmpty, $isEmpty);
46
47
        $spreadsheet->disconnectWorksheets();
48
    }
49
50
    public function emptyRowBasic(): array
51
    {
52
        return [
53
            [1, false],
54
            [2, true],
55
            [3, false],
56
            [4, false],
57
            [5, false],
58
            [6, false],
59
            [7, false],
60
            [8, false],
61
            [9, true],
62
        ];
63
    }
64
65
    /**
66
     * @dataProvider emptyRowNullAsEmpty
67
     */
68
    public function testIteratorEmptyRowWithNull(int $rowId, bool $expectedEmpty): void
69
    {
70
        $spreadsheet = new Spreadsheet();
71
        $sheet = self::getPopulatedSheet($spreadsheet);
72
        $iterator = new RowIterator($sheet, 1, 9);
73
        $iterator->seek($rowId);
74
        $row = $iterator->current();
75
        $isEmpty = $row->isEmpty(CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL);
76
        self::assertSame($expectedEmpty, $isEmpty);
77
        $spreadsheet->disconnectWorksheets();
78
    }
79
80
    public function emptyRowNullAsEmpty(): array
81
    {
82
        return [
83
            [1, false],
84
            [2, true],
85
            [3, true],
86
            [4, false],
87
            [5, false],
88
            [6, false],
89
            [7, false],
90
            [8, false],
91
            [9, true],
92
        ];
93
    }
94
95
    /**
96
     * @dataProvider emptyRowEmptyStringAsEmpty
97
     */
98
    public function testIteratorEmptyRowWithEmptyString(int $rowId, bool $expectedEmpty): void
99
    {
100
        $spreadsheet = new Spreadsheet();
101
        $sheet = self::getPopulatedSheet($spreadsheet);
102
        $iterator = new RowIterator($sheet, 1, 9);
103
        $iterator->seek($rowId);
104
        $row = $iterator->current();
105
        $isEmpty = $row->isEmpty(CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL);
106
        self::assertSame($expectedEmpty, $isEmpty);
107
        $spreadsheet->disconnectWorksheets();
108
    }
109
110
    public function emptyRowEmptyStringAsEmpty(): array
111
    {
112
        return [
113
            [1, false],
114
            [2, true],
115
            [3, false],
116
            [4, true],
117
            [5, false],
118
            [6, false],
119
            [7, false],
120
            [8, false],
121
            [9, true],
122
        ];
123
    }
124
125
    /**
126
     * @dataProvider emptyRowNullAndEmptyStringAsEmpty
127
     */
128
    public function testIteratorEmptyRowWithNullAndEmptyString(int $rowId, bool $expectedEmpty): void
129
    {
130
        $spreadsheet = new Spreadsheet();
131
        $sheet = self::getPopulatedSheet($spreadsheet);
132
        $iterator = new RowIterator($sheet, 1, 9);
133
        $iterator->seek($rowId);
134
        $row = $iterator->current();
135
        $isEmpty = $row->isEmpty(
136
            CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL | CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL
137
        );
138
        self::assertSame($expectedEmpty, $isEmpty);
139
        $spreadsheet->disconnectWorksheets();
140
    }
141
142
    public function emptyRowNullAndEmptyStringAsEmpty(): array
143
    {
144
        return [
145
            [1, false],
146
            [2, true],
147
            [3, true],
148
            [4, true],
149
            [5, true],
150
            [6, false],
151
            [7, false],
152
            [8, false],
153
            [9, true],
154
        ];
155
    }
156
157
    public function testIteratorEmptyRowWithColumnLimit(): void
158
    {
159
        $spreadsheet = new Spreadsheet();
160
        $sheet = self::getPopulatedSheet($spreadsheet);
161
        $sheet->setCellValue('E3', 'NO LONGER EMPTY');
162
163
        $iterator = new RowIterator($sheet, 3, 3);
164
        $row = $iterator->current();
165
166
        $isEmpty = $row->isEmpty(CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL);
167
        self::assertFalse($isEmpty);
168
        $isEmpty = $row->isEmpty(CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL, 'A', 'D');
169
        self::assertTrue($isEmpty);
170
171
        $spreadsheet->disconnectWorksheets();
172
    }
173
}
174