Completed
Push — master ( 89343c...3e93d4 )
by
unknown
36s queued 25s
created

emptyColumnNullAsEmptyProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 12
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\ColumnIterator;
11
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
12
use PHPUnit\Framework\Attributes\DataProvider;
13
use PHPUnit\Framework\TestCase;
14
15
class ColumnIteratorEmptyTest 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('C2', null, DataType::TYPE_NULL);
22
        $sheet->setCellValueExplicit('D2', '', DataType::TYPE_STRING);
23
        $sheet->setCellValueExplicit('E2', null, DataType::TYPE_NULL);
24
        $sheet->setCellValueExplicit('E3', '', DataType::TYPE_STRING);
25
        $sheet->setCellValueExplicit('F2', null, DataType::TYPE_NULL);
26
        $sheet->setCellValueExplicit('F3', 'PHP', DataType::TYPE_STRING);
27
        $sheet->setCellValueExplicit('G2', '', DataType::TYPE_STRING);
28
        $sheet->setCellValueExplicit('G3', 'PHP', DataType::TYPE_STRING);
29
        $sheet->setCellValueExplicit('H2', null, DataType::TYPE_NULL);
30
        $sheet->setCellValueExplicit('H3', '', DataType::TYPE_STRING);
31
        $sheet->setCellValueExplicit('H4', 'PHP', DataType::TYPE_STRING);
32
33
        return $sheet;
34
    }
35
36
    #[DataProvider('emptyColumnBasicProvider')]
37
    public function testIteratorEmptyColumn(string $columnId, bool $expectedEmpty): void
38
    {
39
        $spreadsheet = new Spreadsheet();
40
        $sheet = self::getPopulatedSheet($spreadsheet);
41
        $iterator = new ColumnIterator($sheet, 'A', 'I');
42
        $iterator->seek($columnId);
43
        $column = $iterator->current();
44
        $isEmpty = $column->isEmpty();
45
        self::assertSame($expectedEmpty, $isEmpty);
46
        $spreadsheet->disconnectWorksheets();
47
    }
48
49
    public static function emptyColumnBasicProvider(): array
50
    {
51
        return [
52
            ['A', false],
53
            ['B', true],
54
            ['C', false],
55
            ['D', false],
56
            ['E', false],
57
            ['F', false],
58
            ['G', false],
59
            ['H', false],
60
            ['I', true],
61
        ];
62
    }
63
64
    #[DataProvider('emptyColumnNullAsEmptyProvider')]
65
    public function testIteratorEmptyColumnWithNull(string $columnId, bool $expectedEmpty): void
66
    {
67
        $spreadsheet = new Spreadsheet();
68
        $sheet = self::getPopulatedSheet($spreadsheet);
69
        $iterator = new ColumnIterator($sheet, 'A', 'I');
70
        $iterator->seek($columnId);
71
        $column = $iterator->current();
72
        $isEmpty = $column->isEmpty(CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL);
73
        self::assertSame($expectedEmpty, $isEmpty);
74
        $spreadsheet->disconnectWorksheets();
75
    }
76
77
    public static function emptyColumnNullAsEmptyProvider(): array
78
    {
79
        return [
80
            ['A', false],
81
            ['B', true],
82
            ['C', true],
83
            ['D', false],
84
            ['E', false],
85
            ['F', false],
86
            ['G', false],
87
            ['H', false],
88
            ['I', true],
89
        ];
90
    }
91
92
    #[DataProvider('emptyColumnEmptyStringAsEmptyProvider')]
93
    public function testIteratorEmptyColumnWithEmptyString(string $columnId, bool $expectedEmpty): void
94
    {
95
        $spreadsheet = new Spreadsheet();
96
        $sheet = self::getPopulatedSheet($spreadsheet);
97
        $iterator = new ColumnIterator($sheet, 'A', 'I');
98
        $iterator->seek($columnId);
99
        $column = $iterator->current();
100
        $isEmpty = $column->isEmpty(CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL);
101
        self::assertSame($expectedEmpty, $isEmpty);
102
        $spreadsheet->disconnectWorksheets();
103
    }
104
105
    public static function emptyColumnEmptyStringAsEmptyProvider(): array
106
    {
107
        return [
108
            ['A', false],
109
            ['B', true],
110
            ['C', false],
111
            ['D', true],
112
            ['E', false],
113
            ['F', false],
114
            ['G', false],
115
            ['H', false],
116
            ['I', true],
117
        ];
118
    }
119
120
    #[DataProvider('emptyColumnNullAndEmptyStringAsEmptyProvider')]
121
    public function testIteratorEmptyColumnWithNullAndEmptyString(string $columnId, bool $expectedEmpty): void
122
    {
123
        $spreadsheet = new Spreadsheet();
124
        $sheet = self::getPopulatedSheet($spreadsheet);
125
        $iterator = new ColumnIterator($sheet, 'A', 'I');
126
        $iterator->seek($columnId);
127
        $column = $iterator->current();
128
        $isEmpty = $column->isEmpty(
129
            CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL | CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL
130
        );
131
        self::assertSame($expectedEmpty, $isEmpty);
132
        $spreadsheet->disconnectWorksheets();
133
    }
134
135
    public static function emptyColumnNullAndEmptyStringAsEmptyProvider(): array
136
    {
137
        return [
138
            ['A', false],
139
            ['B', true],
140
            ['C', true],
141
            ['D', true],
142
            ['E', true],
143
            ['F', false],
144
            ['G', false],
145
            ['H', false],
146
            ['I', true],
147
        ];
148
    }
149
150
    public function testIteratorEmptyColumnWithRowLimit(): void
151
    {
152
        $spreadsheet = new Spreadsheet();
153
        $sheet = self::getPopulatedSheet($spreadsheet);
154
        $sheet->setCellValue('A8', 'NO LONGER EMPTY');
155
156
        $iterator = new ColumnIterator($sheet, 'A', 'A');
157
        $column = $iterator->current();
158
159
        $isEmpty = $column->isEmpty(CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL);
160
        self::assertFalse($isEmpty);
161
        $isEmpty = $column->isEmpty(CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL, 2, 7);
162
        self::assertTrue($isEmpty);
163
164
        $spreadsheet->disconnectWorksheets();
165
    }
166
}
167