Passed
Pull Request — master (#4368)
by Owen
12:09
created

ColumnCellIterator2Test   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 18
eloc 65
dl 0
loc 111
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testNullOrCreate() 0 19 4
A testEndRange() 0 24 5
A testEmptyColumn() 0 17 3
A testNullOrCreateOption() 0 11 2
A providerNullOrCreate() 0 6 1
A providerExistingCell() 0 6 1
A isCellNull() 0 3 1
A providerEmptyColumn() 0 6 1
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\ColumnCellIterator;
11
use PHPUnit\Framework\Attributes\DataProvider;
12
use PHPUnit\Framework\TestCase;
13
14
class ColumnCellIterator2Test 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 testEndRange(?bool $existing, string $expectedResultFirst, string $expectedResultLast): void
24
    {
25
        $spreadsheet = new Spreadsheet();
26
        $sheet = $spreadsheet->getActiveSheet();
27
        $sheet->getCell('B2')->setValue('cellb2');
28
        $sheet->getCell('B6')->setValue('cellb6');
29
30
        $iterator = new ColumnCellIterator($sheet, 'B', 1, 8);
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, 'B1', 'B8'],
52
            [false, 'B1', 'B8'],
53
            [true, 'B2', 'B6'],
54
        ];
55
    }
56
57
    #[DataProvider('providerEmptyColumn')]
58
    public function testEmptyColumn(?bool $existing, int $expectedResult): void
59
    {
60
        $spreadsheet = new Spreadsheet();
61
        $sheet = $spreadsheet->getActiveSheet();
62
        $sheet->getCell('B2')->setValue('cellb2');
63
        $sheet->getCell('B6')->setValue('cellb6');
64
65
        $iterator = new ColumnCellIterator($sheet, 'C');
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 providerEmptyColumn(): 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 ColumnCellIterator($sheet, 'F');
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('F4')->setValue('cellf2');
105
106
        $iterator = new ColumnCellIterator($sheet, 'F');
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, 3, 0], // Default behaviour
123
            [CellIterator::IF_NOT_EXISTS_CREATE_NEW, 3, 0],
124
            [CellIterator::IF_NOT_EXISTS_RETURN_NULL, 0, 3],
125
        ];
126
    }
127
}
128