1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Worksheet; |
6
|
|
|
|
7
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Cell; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\CellIterator; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\RowCellIterator; |
11
|
|
|
use PHPUnit\Framework\Attributes\DataProvider; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
|
14
|
|
|
class RowCellIterator2Test extends TestCase |
15
|
|
|
{ |
16
|
|
|
// Phpstan does not think RowCellIterator can return null |
17
|
|
|
private static function isCellNull(?Cell $item): bool |
18
|
|
|
{ |
19
|
|
|
return $item === null; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
#[DataProvider('providerExistingCell')] |
23
|
|
|
public function testEndRangeTrue(?bool $existing, string $expectedResultFirst, string $expectedResultLast): void |
24
|
|
|
{ |
25
|
|
|
$spreadsheet = new Spreadsheet(); |
26
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
27
|
|
|
$sheet->getCell('C2')->setValue('cellb2'); |
28
|
|
|
$sheet->getCell('F2')->setValue('cellf2'); |
29
|
|
|
|
30
|
|
|
$iterator = new RowCellIterator($sheet, 2, 'B', 'H'); |
31
|
|
|
if (isset($existing)) { |
32
|
|
|
$iterator->setIterateOnlyExistingCells($existing); |
33
|
|
|
} |
34
|
|
|
$lastCoordinate = ''; |
35
|
|
|
$firstCoordinate = ''; |
36
|
|
|
foreach ($iterator as $cell) { |
37
|
|
|
if (!self::isCellNull($cell)) { |
38
|
|
|
$lastCoordinate = $cell->getCoordinate(); |
39
|
|
|
if (!$firstCoordinate) { |
40
|
|
|
$firstCoordinate = $lastCoordinate; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
self::assertSame($expectedResultFirst, $firstCoordinate); |
45
|
|
|
self::assertSame($expectedResultLast, $lastCoordinate); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public static function providerExistingCell(): array |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
|
|
[null, 'B2', 'H2'], |
52
|
|
|
[false, 'B2', 'H2'], |
53
|
|
|
[true, 'C2', 'F2'], |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
#[DataProvider('providerEmptyRow')] |
58
|
|
|
public function testEmptyRow(?bool $existing, int $expectedResult): void |
59
|
|
|
{ |
60
|
|
|
$spreadsheet = new Spreadsheet(); |
61
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
62
|
|
|
$sheet->getCell('B2')->setValue('cellb2'); |
63
|
|
|
$sheet->getCell('F2')->setValue('cellf2'); |
64
|
|
|
|
65
|
|
|
$iterator = new RowCellIterator($sheet, 3); |
66
|
|
|
if (isset($existing)) { |
67
|
|
|
$iterator->setIterateOnlyExistingCells($existing); |
68
|
|
|
} |
69
|
|
|
$numCells = 0; |
70
|
|
|
foreach ($iterator as $cell) { |
71
|
|
|
++$numCells; |
72
|
|
|
} |
73
|
|
|
self::assertSame($expectedResult, $numCells); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public static function providerEmptyRow(): array |
77
|
|
|
{ |
78
|
|
|
return [ |
79
|
|
|
[null, 6], // Default behaviour |
80
|
|
|
[false, 6], |
81
|
|
|
[true, 0], |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
#[DataProvider('providerNullOrCreate')] |
86
|
|
|
public function testNullOrCreateOption(?bool $existingBehaviour, int $expectedCreatedResult): void |
87
|
|
|
{ |
88
|
|
|
$spreadsheet = new Spreadsheet(); |
89
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
90
|
|
|
$iterator = new RowCellIterator($sheet, 2); |
91
|
|
|
if (isset($existingBehaviour)) { |
92
|
|
|
$iterator->setIfNotExists($existingBehaviour); |
93
|
|
|
} |
94
|
|
|
$notExistsBehaviour = $iterator->getIfNotExists(); |
95
|
|
|
self::assertSame($expectedCreatedResult > 0, $notExistsBehaviour); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
#[DataProvider('providerNullOrCreate')] |
99
|
|
|
public function testNullOrCreate(?bool $existing, int $expectedCreatedResult, int $expectedNullResult): void |
100
|
|
|
{ |
101
|
|
|
$spreadsheet = new Spreadsheet(); |
102
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
103
|
|
|
$sheet->getCell('B2')->setValue('cellb2'); |
104
|
|
|
$sheet->getCell('F2')->setValue('cellf2'); |
105
|
|
|
|
106
|
|
|
$iterator = new RowCellIterator($sheet, 2); |
107
|
|
|
if (isset($existing)) { |
108
|
|
|
$iterator->setIfNotExists($existing); |
109
|
|
|
} |
110
|
|
|
$numCreatedCells = $numEmptyCells = 0; |
111
|
|
|
foreach ($iterator as $cell) { |
112
|
|
|
$numCreatedCells += (int) (!self::isCellNull($cell) && $cell->getValue() === null); |
113
|
|
|
$numEmptyCells += (int) self::isCellNull($cell); |
114
|
|
|
} |
115
|
|
|
self::assertSame($expectedCreatedResult, $numCreatedCells); |
116
|
|
|
self::assertSame($expectedNullResult, $numEmptyCells); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public static function providerNullOrCreate(): array |
120
|
|
|
{ |
121
|
|
|
return [ |
122
|
|
|
[null, 4, 0], // Default behaviour |
123
|
|
|
[CellIterator::IF_NOT_EXISTS_CREATE_NEW, 4, 0], |
124
|
|
|
[CellIterator::IF_NOT_EXISTS_RETURN_NULL, 0, 4], |
125
|
|
|
]; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|