Skip to content

Commit

Permalink
Add test to validate issue 589 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagomalheiro committed Oct 31, 2021
1 parent a97800b commit e8a667b
Showing 1 changed file with 180 additions and 0 deletions.
180 changes: 180 additions & 0 deletions tests/PhpSpreadsheetTests/Writer/Xlsx/Issue589Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;

use DOMDocument;
use PhpOffice\PhpSpreadsheet\Chart\Chart;
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as Writer;
use PHPUnit\Framework\TestCase;
use ZipArchive;

class Issue589Test extends TestCase
{
/**
* Build a testable chart in a spreadsheet and set fill color for series.
*
* @param string|string[] $color HEX color or array with HEX colors
*/
private function buildChartSpreadsheet($color): Spreadsheet
{
// Problem occurs when setting plot line color
// The output chart xml file is missing the a:ln tag
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->fromArray(
[
[2010],
[12],
[56],
]
);

// Set the Labels for each data series we want to plot
// Datatype
// Cell reference for data
// Format Code
// Number of datapoints in series
// Data values
// Data Marker
// Fill Color
$dataSeriesLabels = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$1', null, 1),
];
$dataSeriesLabels[0]->setFillColor($color);
// Set the Data values for each data series we want to plot
// Datatype
// Cell reference for data
// Format Code
// Number of datapoints in series
// Data values
// Data Marker
$dataSeriesValues = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$A$2:$A$3', null, 2),
];

// Build the dataseries
$series = new DataSeries(
DataSeries::TYPE_LINECHART, // plotType
DataSeries::GROUPING_STACKED, // plotGrouping
range(0, count($dataSeriesValues) - 1), // plotOrder
$dataSeriesLabels, // plotLabel
[], // plotCategory
$dataSeriesValues // plotValues
);

// Set the series in the plot area
$plotArea = new PlotArea(null, [$series]);

// Create the chart
$chart = new Chart(
'chart1', // name
null, // title
null, // legend
$plotArea, // plotArea
true, // plotVisibleOnly
DataSeries::EMPTY_AS_GAP, // displayBlanksAs
null, // xAxisLabel
null // yAxisLabel
);

// Add the chart to the worksheet
$worksheet->addChart($chart);

return $spreadsheet;
}

public function testLineChartFill(): void
{
$outputFilename = File::temporaryFilename();
$spreadsheet = $this->buildChartSpreadsheet('98B954');
$writer = new Writer($spreadsheet);
$writer->setIncludeCharts(true);
$writer->save($outputFilename);

$zip = new ZipArchive();
$zip->open($outputFilename);
$resultChart1Raw = $zip->getFromName('xl/charts/chart1.xml');
$zip->close();
unlink($outputFilename);

$dom = new DOMDocument();
if ($resultChart1Raw === false) {
self::fail('Unable to open the chart file');
} else {
$loaded = $dom->loadXML($resultChart1Raw);
if (!$loaded) {
self::fail('Unable to load the chart xml');
} else {
$series = $dom->getElementsByTagName('ser');
$firstSeries = $series->item(0);
if ($firstSeries === null) {
self::fail('The chart XML does not contain a \'ser\' tag!');
} else {
$spPrList = $firstSeries->getElementsByTagName('spPr');

// expect to see only one element with name 'c:spPr'
self::assertCount(1, $spPrList);

/** @var DOMNode $node */
$node = $spPrList->item(0); // Get the spPr element
$actualXml = $dom->saveXML($node);
if ($actualXml === false) {
self::fail('Failure saving the spPr element as xml string!');
} else {
self::assertXmlStringEqualsXmlString('<c:spPr><a:ln w="12700"><a:solidFill><a:srgbClr val="98B954"/></a:solidFill></a:ln></c:spPr>', $actualXml);
}
}
}
}
}

public function testLineChartFillIgnoresColorArray(): void
{
$outputFilename = File::temporaryFilename();
$spreadsheet = $this->buildChartSpreadsheet(['98B954']);
$writer = new Writer($spreadsheet);
$writer->setIncludeCharts(true);
$writer->save($outputFilename);

$zip = new ZipArchive();
$zip->open($outputFilename);
$resultChart1Raw = $zip->getFromName('xl/charts/chart1.xml');
$zip->close();
unlink($outputFilename);

$dom = new DOMDocument();
if ($resultChart1Raw === false) {
self::fail('Unable to open the chart file');
} else {
$loaded = $dom->loadXML($resultChart1Raw);
if (!$loaded) {
self::fail('Unable to load the chart xml');
} else {
$series = $dom->getElementsByTagName('ser');
$firstSeries = $series->item(0);
if ($firstSeries === null) {
self::fail('The chart XML does not contain a \'ser\' tag!');
} else {
$spPrList = $firstSeries->getElementsByTagName('spPr');

// expect to see only one element with name 'c:spPr'
self::assertCount(1, $spPrList);

/** @var DOMNode $node */
$node = $spPrList->item(0); // Get the spPr element
$actualXml = $dom->saveXML($node);
if ($actualXml === false) {
self::fail('Failure saving the spPr element as xml string!');
} else {
self::assertXmlStringEqualsXmlString('<c:spPr><a:ln w="12700"></a:ln></c:spPr>', $actualXml);
}
}
}
}
}
}

0 comments on commit e8a667b

Please sign in to comment.