Failed Conditions
Push — master ( 0c93bb...a2be57 )
by Adrien
29:37 queued 19:11
created

Issue589Test::testLineChartFill()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 39
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 31
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 39
rs 9.1128
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;
4
5
use DOMDocument;
6
use DOMNode;
7
use PhpOffice\PhpSpreadsheet\Chart\Chart;
8
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
9
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
10
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
11
use PhpOffice\PhpSpreadsheet\Shared\File;
12
use PhpOffice\PhpSpreadsheet\Spreadsheet;
13
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as Writer;
14
use PHPUnit\Framework\TestCase;
15
use ZipArchive;
16
17
class Issue589Test extends TestCase
18
{
19
    /**
20
     * Build a testable chart in a spreadsheet and set fill color for series.
21
     *
22
     * @param string|string[] $color HEX color or array with HEX colors
23
     */
24
    private function buildChartSpreadsheet($color): Spreadsheet
25
    {
26
        // Problem occurs when setting plot line color
27
        // The output chart xml file is missing the a:ln tag
28
        $spreadsheet = new Spreadsheet();
29
        $worksheet = $spreadsheet->getActiveSheet();
30
        $worksheet->fromArray(
31
            [
32
                [2010],
33
                [12],
34
                [56],
35
            ]
36
        );
37
38
        $dataSeriesLabels = [
39
            new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$1', null, 1),
40
        ];
41
        $dataSeriesLabels[0]->setFillColor($color);
42
        $dataSeriesValues = [
43
            new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$A$2:$A$3', null, 2),
44
        ];
45
46
        // Build the dataseries
47
        $series = new DataSeries(
48
            DataSeries::TYPE_LINECHART,
49
            DataSeries::GROUPING_STACKED,
50
            range(0, count($dataSeriesValues) - 1),
51
            $dataSeriesLabels,
52
            [],
53
            $dataSeriesValues
54
        );
55
56
        // Set the series in the plot area
57
        $plotArea = new PlotArea(null, [$series]);
58
59
        // Create the chart
60
        $chart = new Chart(
61
            'chart1',
62
            null,
63
            null,
64
            $plotArea
65
        );
66
67
        // Add the chart to the worksheet
68
        $worksheet->addChart($chart);
69
70
        return $spreadsheet;
71
    }
72
73
    public function testLineChartFill(): void
74
    {
75
        $outputFilename = File::temporaryFilename();
76
        $spreadsheet = $this->buildChartSpreadsheet('98B954');
77
        $writer = new Writer($spreadsheet);
78
        $writer->setIncludeCharts(true);
79
        $writer->save($outputFilename);
80
81
        $zip = new ZipArchive();
82
        $zip->open($outputFilename);
83
        $resultChart1Raw = $zip->getFromName('xl/charts/chart1.xml');
84
        $zip->close();
85
        unlink($outputFilename);
86
87
        $dom = new DOMDocument();
88
        if ($resultChart1Raw === false) {
89
            self::fail('Unable to open the chart file');
90
        } else {
91
            $loaded = $dom->loadXML($resultChart1Raw);
92
            if (!$loaded) {
93
                self::fail('Unable to load the chart xml');
94
            } else {
95
                $series = $dom->getElementsByTagName('ser');
96
                $firstSeries = $series->item(0);
97
                if ($firstSeries === null) {
98
                    self::fail('The chart XML does not contain a \'ser\' tag!');
99
                } else {
100
                    $spPrList = $firstSeries->getElementsByTagName('spPr');
101
102
                    // expect to see only one element with name 'c:spPr'
103
                    self::assertCount(1, $spPrList);
104
105
                    /** @var DOMNode $node */
106
                    $node = $spPrList->item(0); // Get the spPr element
107
                    $actualXml = $dom->saveXML($node);
108
                    if ($actualXml === false) {
109
                        self::fail('Failure saving the spPr element as xml string!');
110
                    } else {
111
                        self::assertXmlStringEqualsXmlString('<c:spPr><a:ln w="12700"><a:solidFill><a:srgbClr val="98B954"/></a:solidFill></a:ln></c:spPr>', $actualXml);
112
                    }
113
                }
114
            }
115
        }
116
    }
117
118
    public function testLineChartFillIgnoresColorArray(): void
119
    {
120
        $outputFilename = File::temporaryFilename();
121
        $spreadsheet = $this->buildChartSpreadsheet(['98B954']);
122
        $writer = new Writer($spreadsheet);
123
        $writer->setIncludeCharts(true);
124
        $writer->save($outputFilename);
125
126
        $zip = new ZipArchive();
127
        $zip->open($outputFilename);
128
        $resultChart1Raw = $zip->getFromName('xl/charts/chart1.xml');
129
        $zip->close();
130
        unlink($outputFilename);
131
132
        $dom = new DOMDocument();
133
        if ($resultChart1Raw === false) {
134
            self::fail('Unable to open the chart file');
135
        } else {
136
            $loaded = $dom->loadXML($resultChart1Raw);
137
            if (!$loaded) {
138
                self::fail('Unable to load the chart xml');
139
            } else {
140
                $series = $dom->getElementsByTagName('ser');
141
                $firstSeries = $series->item(0);
142
                if ($firstSeries === null) {
143
                    self::fail('The chart XML does not contain a \'ser\' tag!');
144
                } else {
145
                    $spPrList = $firstSeries->getElementsByTagName('spPr');
146
147
                    // expect to see only one element with name 'c:spPr'
148
                    self::assertCount(1, $spPrList);
149
150
                    /** @var DOMNode $node */
151
                    $node = $spPrList->item(0); // Get the spPr element
152
                    $actualXml = $dom->saveXML($node);
153
                    if ($actualXml === false) {
154
                        self::fail('Failure saving the spPr element as xml string!');
155
                    } else {
156
                        self::assertXmlStringEqualsXmlString('<c:spPr><a:ln w="12700"></a:ln></c:spPr>', $actualXml);
157
                    }
158
                }
159
            }
160
        }
161
    }
162
}
163