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