Skip to content

Commit

Permalink
Add chart type data provider to embedded spreadsheets test
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-roth committed Aug 14, 2021
1 parent a6f01f6 commit 64ef59b
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 30 deletions.
7 changes: 7 additions & 0 deletions src/PhpPresentation/Shape/Chart/Type/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ abstract class AbstractType implements ComparableInterface
*/
private $series = [];

/**
* Returns xml element name for a chart type implementation
*
* @return string
*/
abstract public function getXmlElementName(): string;

/**
* Has Axis X?
*/
Expand Down
2 changes: 1 addition & 1 deletion src/PhpPresentation/Shape/Chart/Type/AbstractTypeBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* \PhpOffice\PhpPresentation\Shape\Chart\Type\Bar.
*/
class AbstractTypeBar extends AbstractType
abstract class AbstractTypeBar extends AbstractType
{
/** Orientation of bars */
public const DIRECTION_VERTICAL = 'col';
Expand Down
2 changes: 1 addition & 1 deletion src/PhpPresentation/Shape/Chart/Type/AbstractTypeLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace PhpOffice\PhpPresentation\Shape\Chart\Type;

class AbstractTypeLine extends AbstractType
abstract class AbstractTypeLine extends AbstractType
{
/**
* Is Line Smooth?
Expand Down
2 changes: 1 addition & 1 deletion src/PhpPresentation/Shape/Chart/Type/AbstractTypePie.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* \PhpOffice\PhpPresentation\Shape\Chart\Type\Bar.
*/
class AbstractTypePie extends AbstractType
abstract class AbstractTypePie extends AbstractType
{
/**
* Create a new self instance.
Expand Down
5 changes: 5 additions & 0 deletions src/PhpPresentation/Shape/Chart/Type/Area.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public function getHashCode(): string

return md5($hash . __CLASS__);
}

public function getXmlElementName(): string
{
return 'c:areaChart';
}
}
5 changes: 5 additions & 0 deletions src/PhpPresentation/Shape/Chart/Type/Bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public function getHashCode(): string
{
return md5(parent::getHashCode() . __CLASS__);
}

public function getXmlElementName(): string
{
return 'c:barChart';
}
}
5 changes: 5 additions & 0 deletions src/PhpPresentation/Shape/Chart/Type/Bar3D.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public function getHashCode(): string
{
return md5(parent::getHashCode() . __CLASS__);
}

public function getXmlElementName(): string
{
return 'c:bar3DChart';
}
}
5 changes: 5 additions & 0 deletions src/PhpPresentation/Shape/Chart/Type/Doughnut.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,9 @@ public function getHashCode(): string
{
return md5(parent::getHashCode() . __CLASS__);
}

public function getXmlElementName(): string
{
return 'c:doughnutChart';
}
}
5 changes: 5 additions & 0 deletions src/PhpPresentation/Shape/Chart/Type/Line.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ public function getHashCode(): string

return md5(parent::getHashCode() . $hash . __CLASS__);
}

public function getXmlElementName(): string
{
return 'c:lineChart';
}
}
5 changes: 5 additions & 0 deletions src/PhpPresentation/Shape/Chart/Type/Pie.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public function getHashCode(): string
{
return md5(parent::getHashCode() . __CLASS__);
}

public function getXmlElementName(): string
{
return 'c:pieChart';
}
}
5 changes: 5 additions & 0 deletions src/PhpPresentation/Shape/Chart/Type/Pie3D.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public function getHashCode(): string
{
return md5(parent::getHashCode() . __CLASS__);
}

public function getXmlElementName(): string
{
return 'c:pie3DChart';
}
}
5 changes: 5 additions & 0 deletions src/PhpPresentation/Shape/Chart/Type/Radar.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ public function getHashCode(): string

return md5($hash . __CLASS__);
}

public function getXmlElementName(): string
{
return 'c:radarChart';
}
}
5 changes: 5 additions & 0 deletions src/PhpPresentation/Shape/Chart/Type/Scatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ public function getHashCode(): string

return md5(parent::getHashCode() . $hash . __CLASS__);
}

public function getXmlElementName(): string
{
return 'c:scatterChart';
}
}
18 changes: 9 additions & 9 deletions src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ protected function writeLayout(XMLWriter $objWriter, $subject): void
protected function writeTypeArea(XMLWriter $objWriter, Area $subject, bool $includeSheet = false): void
{
// c:lineChart
$objWriter->startElement('c:areaChart');
$objWriter->startElement($subject->getXmlElementName());

// c:grouping
$objWriter->startElement('c:grouping');
Expand Down Expand Up @@ -804,7 +804,7 @@ protected function writeTypeArea(XMLWriter $objWriter, Area $subject, bool $incl
protected function writeTypeBar(XMLWriter $objWriter, Bar $subject, bool $includeSheet = false): void
{
// c:barChart
$objWriter->startElement('c:barChart');
$objWriter->startElement($subject->getXmlElementName());

// c:barDir
$objWriter->startElement('c:barDir');
Expand Down Expand Up @@ -1017,7 +1017,7 @@ protected function writeTypeBar(XMLWriter $objWriter, Bar $subject, bool $includ
protected function writeTypeBar3D(XMLWriter $objWriter, Bar3D $subject, bool $includeSheet = false): void
{
// c:bar3DChart
$objWriter->startElement('c:bar3DChart');
$objWriter->startElement($subject->getXmlElementName());

// c:barDir
$objWriter->startElement('c:barDir');
Expand Down Expand Up @@ -1210,7 +1210,7 @@ protected function writeTypeBar3D(XMLWriter $objWriter, Bar3D $subject, bool $in
protected function writeTypeDoughnut(XMLWriter $objWriter, Doughnut $subject, bool $includeSheet = false): void
{
// c:pieChart
$objWriter->startElement('c:doughnutChart');
$objWriter->startElement($subject->getXmlElementName());

// c:varyColors
$objWriter->startElement('c:varyColors');
Expand Down Expand Up @@ -1369,7 +1369,7 @@ protected function writeTypeDoughnut(XMLWriter $objWriter, Doughnut $subject, bo
protected function writeTypePie(XMLWriter $objWriter, Pie $subject, bool $includeSheet = false): void
{
// c:pieChart
$objWriter->startElement('c:pieChart');
$objWriter->startElement($subject->getXmlElementName());

// c:varyColors
$objWriter->startElement('c:varyColors');
Expand Down Expand Up @@ -1534,7 +1534,7 @@ protected function writeTypePie(XMLWriter $objWriter, Pie $subject, bool $includ
protected function writeTypePie3D(XMLWriter $objWriter, Pie3D $subject, bool $includeSheet = false): void
{
// c:pie3DChart
$objWriter->startElement('c:pie3DChart');
$objWriter->startElement($subject->getXmlElementName());

// c:varyColors
$objWriter->startElement('c:varyColors');
Expand Down Expand Up @@ -1693,7 +1693,7 @@ protected function writeTypePie3D(XMLWriter $objWriter, Pie3D $subject, bool $in
protected function writeTypeLine(XMLWriter $objWriter, Line $subject, bool $includeSheet = false): void
{
// c:lineChart
$objWriter->startElement('c:lineChart');
$objWriter->startElement($subject->getXmlElementName());

// c:grouping
$objWriter->startElement('c:grouping');
Expand Down Expand Up @@ -1864,7 +1864,7 @@ protected function writeTypeLine(XMLWriter $objWriter, Line $subject, bool $incl
protected function writeTypeRadar(XMLWriter $objWriter, Radar $subject, bool $includeSheet = false): void
{
// c:scatterChart
$objWriter->startElement('c:radarChart');
$objWriter->startElement($subject->getXmlElementName());

// c:radarStyle
$objWriter->startElement('c:radarStyle');
Expand Down Expand Up @@ -2033,7 +2033,7 @@ protected function writeTypeRadar(XMLWriter $objWriter, Radar $subject, bool $in
protected function writeTypeScatter(XMLWriter $objWriter, Scatter $subject, bool $includeSheet = false): void
{
// c:scatterChart
$objWriter->startElement('c:scatterChart');
$objWriter->startElement($subject->getXmlElementName());

// c:scatterStyle
$objWriter->startElement('c:scatterStyle');
Expand Down
60 changes: 44 additions & 16 deletions tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,22 @@ public function testChartDisplayBlankAs(): void
$this->assertIsSchemaECMA376Valid();
}

public function testChartIncludeSpreadsheet(): void
/**
* @dataProvider dataProviderIncludedSpreadsheet
*/
public function testChartIncludeSpreadsheet(string $chartType): void
{
$oSlide = $this->oPresentation->getActiveSlide();
$oShape = $oSlide->createChartShape();
$oShape->setIncludeSpreadsheet(true);
$oLine = new Line();
/** @var AbstractType $oChart */
$oChart = new $chartType();
$oSeries = new Series('Downloads', $this->seriesData);
$oLine->addSeries($oSeries);
$oShape->getPlotArea()->setType($oLine);
$oChart->addSeries($oSeries);
$oShape->getPlotArea()->setType($oChart);

$chartElementName = $oChart->getXmlElementName();
$chartBaseXmlPath = sprintf('/c:chartSpace/c:chart/c:plotArea/%s', $chartElementName);

$this->assertTrue($oShape->hasIncludedSpreadsheet());

Expand All @@ -103,26 +110,26 @@ public function testChartIncludeSpreadsheet(): void

$element = '/p:sld/p:cSld/p:spTree/p:graphicFrame/a:graphic/a:graphicData';
$this->assertZipXmlElementExists('ppt/slides/slide1.xml', $element);
$element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart';
$element = $chartBaseXmlPath;
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element);
$element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser';
$element = $chartBaseXmlPath . '/c:ser';
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element);
$this->assertZipXmlElementCount('ppt/charts/' . $oShape->getIndexedFilename(), $element, 1);
$element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser/c:tx/c:v';
$element = $chartBaseXmlPath . '/c:ser/c:tx/c:v';
$this->assertZipXmlElementNotExists('ppt/charts/' . $oShape->getIndexedFilename(), $element);
$element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser/c:tx/c:strRef';
$element = $chartBaseXmlPath . '/c:ser/c:tx/c:strRef';
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element);
$element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser/c:tx/c:strRef/c:f';
$element = $chartBaseXmlPath . '/c:ser/c:tx/c:strRef/c:f';
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element);
$this->assertZipXmlElementEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'Sheet1!$B$1');
$element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser/c:tx/c:strRef/c:strCache';
$element = $chartBaseXmlPath . '/c:ser/c:tx/c:strRef/c:strCache';
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element);
$element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser/c:tx/c:strRef/c:strCache/c:pt';
$element = $chartBaseXmlPath . '/c:ser/c:tx/c:strRef/c:strCache/c:pt';
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element);
$element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser/c:tx/c:strRef/c:strCache/c:pt/c:v';
$element = $chartBaseXmlPath . '/c:ser/c:tx/c:strRef/c:strCache/c:pt/c:v';
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element);
$this->assertZipXmlElementEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'Downloads');
$element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser/c:val/c:numRef/c:numCache/c:pt/c:v';
$element = $chartBaseXmlPath . '/c:ser/c:val/c:numRef/c:numCache/c:pt/c:v';
$this->assertZipXmlElementCount('ppt/charts/' . $oShape->getIndexedFilename(), $element, count($this->seriesData));
foreach (array_values($this->seriesData) as $index => $value) {
$this->assertZipXmlElementAtIndexEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, $index, $value);
Expand All @@ -149,11 +156,11 @@ public function testChartIncludeSpreadsheet(): void

$element = '/p:sld/p:cSld/p:spTree/p:graphicFrame/a:graphic/a:graphicData';
$this->assertZipXmlElementExists('ppt/slides/slide1.xml', $element);
$element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart';
$element = $chartBaseXmlPath;
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element);
$element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser';
$element = $chartBaseXmlPath . '/c:ser';
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element);
$element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser/c:tx/c:v';
$element = $chartBaseXmlPath . '/c:ser/c:tx/c:v';
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element);
$this->assertZipXmlElementEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'Downloads');
$element = '/c:chartSpace/c:externalData';
Expand Down Expand Up @@ -1483,4 +1490,25 @@ public function dataProviderMarkerSymbol(): iterable
yield [$symbol];
}
}

/**
* @return array<array<string>>
*/
public function dataProviderIncludedSpreadsheet(): iterable
{
$chartTypes = [
Area::class,
Bar::class,
Bar3D::class,
Doughnut::class,
Pie::class,
Pie3D::class,
Line::class,
Radar::class,
Scatter::class,
];
foreach ($chartTypes as $chartType) {
yield [$chartType];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public function assertZipXmlElementExists($filePath, $xPath): void
* @param string $xPath
* @param int $index
*/
public function assertZipXmlElementAtIndexExists($filePath, $xPath, $index): void
public function assertZipXmlElementAtIndexExists(string $filePath, string $xPath, int $index): void
{
$this->writePresentationFile($this->oPresentation, $this->writerName);
$nodeList = $this->getXmlNodeList($filePath, $xPath);
Expand Down Expand Up @@ -306,7 +306,7 @@ public function assertZipXmlElementEquals($filePath, $xPath, $value): void
* @param int $index
* @param mixed $value
*/
public function assertZipXmlElementAtIndexEquals($filePath, $xPath, $index, $value): void
public function assertZipXmlElementAtIndexEquals(string $filePath, string $xPath, int $index, $value): void
{
$this->writePresentationFile($this->oPresentation, $this->writerName);
$nodeList = $this->getXmlNodeList($filePath, $xPath);
Expand Down

0 comments on commit 64ef59b

Please sign in to comment.