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

testIteratorEmptyColumnWithRowLimit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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