Passed
Push — master ( d88efc...653645 )
by
unknown
12:43 queued 21s
created

ConditionalFormatIconSetTest::testWriteIconSets()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 42
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 42
rs 9.1768
cc 5
nc 16
nop 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;
6
7
use Generator;
8
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9
use PhpOffice\PhpSpreadsheet\Style\Conditional;
10
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalFormatValueObject;
11
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalIconSet;
12
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\IconSetValues;
13
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
14
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
15
use PHPUnit\Framework\Attributes\DataProvider;
16
17
class ConditionalFormatIconSetTest extends AbstractFunctional
18
{
19
    private const COORDINATE = 'A1:A3';
20
21
    #[DataProvider('iconSetsProvider')]
22
    public function testWriteIconSets(
23
        string $expected,
24
        ?IconSetValues $type,
25
        ?bool $reverse = null,
26
        ?bool $showValue = null,
27
        ?bool $custom = null,
28
    ): void {
29
        $condition = new Conditional();
30
        $condition->setConditionType(Conditional::CONDITION_ICONSET);
31
        $iconSet = $condition->setIconSet(new ConditionalIconSet())
32
            ->getIconSet();
33
        self::assertNotNull($iconSet);
34
        if ($type !== null) {
35
            $iconSet->setIconSetType($type);
36
        }
37
        $iconSet->setCfvos([
38
            new ConditionalFormatValueObject('percent', 0),
39
            new ConditionalFormatValueObject('percent', 33),
40
            new ConditionalFormatValueObject('percent', 67),
41
        ]);
42
        if ($reverse !== null) {
43
            $iconSet->setReverse($reverse);
44
        }
45
        if ($showValue !== null) {
46
            $iconSet->setShowValue($showValue);
47
        }
48
        if ($custom !== null) {
49
            $iconSet->setCustom($custom);
50
        }
51
52
        $spreadsheet = new Spreadsheet();
53
        $worksheet = $spreadsheet->getActiveSheet();
54
        $worksheet->setConditionalStyles(self::COORDINATE, [$condition]);
55
56
        $writer = new Xlsx($spreadsheet);
57
        $writerWorksheet = new Xlsx\Worksheet($writer);
58
        $data = $writerWorksheet->writeWorksheet($worksheet, []);
59
60
        $expected = preg_replace(['/^\s+/m', "/\n/"], '', $expected);
61
        self::assertIsString($expected);
62
        self::assertStringContainsString($expected, $data);
63
    }
64
65
    public static function iconSetsProvider(): Generator
66
    {
67
        $coordinate = self::COORDINATE;
68
        $cfvos = <<<XML
69
            <cfvo type="percent" val="0"/>
70
            <cfvo type="percent" val="33"/>
71
            <cfvo type="percent" val="67"/>
72
            XML;
73
        foreach (IconSetValues::cases() as $type) {
74
            yield $type->name => [
75
                <<<XML
76
                    <conditionalFormatting sqref="{$coordinate}">
77
                        <cfRule type="iconSet" priority="1">
78
                            <iconSet iconSet="{$type->value}">
79
                                {$cfvos}
80
                            </iconSet>
81
                        </cfRule>
82
                    </conditionalFormatting>
83
                    XML,
84
                $type,
85
            ];
86
        }
87
88
        yield 'null' => [
89
            <<<XML
90
                <conditionalFormatting sqref="{$coordinate}">
91
                    <cfRule type="iconSet" priority="1">
92
                        <iconSet>
93
                            {$cfvos}
94
                        </iconSet>
95
                    </cfRule>
96
                </conditionalFormatting>
97
                XML,
98
            null,
99
        ];
100
101
        foreach ([1, 0] as $reverse) {
102
            yield "null/reverse=$reverse" => [
103
                <<<XML
104
                    <conditionalFormatting sqref="{$coordinate}">
105
                        <cfRule type="iconSet" priority="1">
106
                            <iconSet reverse="$reverse">
107
                                {$cfvos}
108
                            </iconSet>
109
                        </cfRule>
110
                    </conditionalFormatting>
111
                    XML,
112
                null,
113
                $reverse === 1,
114
            ];
115
        }
116
117
        foreach ([1, 0] as $showValue) {
118
            yield "null/showValue=$showValue" => [
119
                <<<XML
120
                    <conditionalFormatting sqref="{$coordinate}">
121
                        <cfRule type="iconSet" priority="1">
122
                            <iconSet showValue="$showValue">
123
                                {$cfvos}
124
                            </iconSet>
125
                        </cfRule>
126
                    </conditionalFormatting>
127
                    XML,
128
                null,
129
                null,
130
                $showValue === 1,
131
            ];
132
        }
133
134
        foreach ([1, 0] as $custom) {
135
            yield "null/custom=$custom" => [
136
                <<<XML
137
                    <conditionalFormatting sqref="{$coordinate}">
138
                        <cfRule type="iconSet" priority="1">
139
                            <iconSet custom="$custom">
140
                                {$cfvos}
141
                            </iconSet>
142
                        </cfRule>
143
                    </conditionalFormatting>
144
                    XML,
145
                null,
146
                null,
147
                null,
148
                $custom === 1,
149
            ];
150
        }
151
    }
152
}
153