Skip to content

Commit 9f73990

Browse files
committed
IMPROVED : Unit Tests
1 parent c8b57c0 commit 9f73990

File tree

6 files changed

+172
-11
lines changed

6 files changed

+172
-11
lines changed

src/PhpPowerpoint/Writer/ODPresentation/Content.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -719,9 +719,6 @@ public function writeGroupStyle(XMLWriter $objWriter, Group $group)
719719
if ($shape instanceof Table) {
720720
$this->writeTableStyle($objWriter, $shape);
721721
}
722-
if ($shape instanceof Group) {
723-
$this->writeGroupStyle($objWriter, $shape);
724-
}
725722
}
726723
}
727724

tests/PhpPowerpoint/Tests/Slide/NoteTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,30 @@ public function testParent()
3939
$this->assertInstanceOf('PhpOffice\\PhpPowerpoint\\Slide', $object->getParent());
4040
}
4141

42+
public function testExtent()
43+
{
44+
$object = new Note();
45+
$this->assertNotNull($object->getExtentX());
46+
47+
$object = new Note();
48+
$this->assertNotNull($object->getExtentY());
49+
}
50+
51+
public function testHashCode()
52+
{
53+
$object = new Note();
54+
$this->assertInternalType('string', $object->getHashCode());
55+
}
56+
57+
public function testOffset()
58+
{
59+
$object = new Note();
60+
$this->assertNotNull($object->getOffsetX());
61+
62+
$object = new Note();
63+
$this->assertNotNull($object->getOffsetY());
64+
}
65+
4266
public function testShape()
4367
{
4468
$object = new Note();

tests/PhpPowerpoint/Tests/Writer/ODPresentation/AbstractPartTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PhpOffice\PhpPowerPoint\Shape\Group;
2222
use PhpOffice\PhpPowerpoint\Writer\ODPresentation\Drawing;
2323
use PhpOffice\PhpPowerpoint\Writer\ODPresentation\Manifest;
24+
use PhpOffice\PhpPowerpoint\Writer\ODPresentation;
2425
use PhpOffice\PhpPowerpoint\Writer\PowerPoint2007;
2526

2627
/**
@@ -30,6 +31,13 @@
3031
*/
3132
class AbstractPartTest extends \PHPUnit_Framework_TestCase
3233
{
34+
protected function runProtectedMethod ($obj, $method, $args = array())
35+
{
36+
$method = new \ReflectionMethod(get_class($obj), $method);
37+
$method->setAccessible(true);
38+
return $method->invokeArgs($obj, $args);
39+
}
40+
3341
/**
3442
* Executed before each method of the class
3543
*/
@@ -57,4 +65,25 @@ public function testWriterException()
5765
$oManifest->setParentWriter(new PowerPoint2007());
5866
$oManifest->writePart();
5967
}
68+
69+
/**
70+
* @expectedException \Exception
71+
* @expectedExceptionMessage The $parentWriter is not an instance of \PhpOffice\PhpPowerpoint\Writer\ODPresentation
72+
*/
73+
public function testXMLWriterException()
74+
{
75+
$oManifest = new Manifest();
76+
$oManifest->setParentWriter(new PowerPoint2007());
77+
$this->runProtectedMethod($oManifest, 'getXMLWriter');
78+
}
79+
80+
public function testXMLWriterWithDiskCaching()
81+
{
82+
$oODPresentation = new ODPresentation();
83+
$oODPresentation->setUseDiskCaching(true);
84+
$oManifest = new Manifest();
85+
$oManifest->setParentWriter($oODPresentation);
86+
87+
$this->assertNotEmpty(\PHPUnit_Framework_Assert::readAttribute($this->runProtectedMethod($oManifest, 'getXMLWriter'), 'tempFileName'));
88+
}
6089
}

tests/PhpPowerpoint/Tests/Writer/ODPresentation/ContentTest.php

Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
use PhpOffice\PhpPowerpoint\Style\Color;
2828
use PhpOffice\PhpPowerpoint\Writer\ODPresentation;
2929
use PhpOffice\PhpPowerpoint\Tests\TestHelperDOCX;
30+
use PhpOffice\Common\Drawing;
31+
use PhpOffice\PhpPowerpoint\Style\Fill;
32+
use PhpOffice\PhpPowerpoint\Style\PhpOffice\PhpPowerpoint\Style;
3033

3134
/**
3235
* Test class for PhpOffice\PhpPowerpoint\Writer\ODPresentation\Manifest
@@ -57,6 +60,23 @@ public function testDrawingWithHyperlink()
5760
$this->assertTrue($pres->elementExists($element, 'content.xml'));
5861
$this->assertEquals('https://github.com/PHPOffice/PHPPowerPoint/', $pres->getElementAttribute($element, 'xlink:href', 'content.xml'));
5962
}
63+
64+
public function testGroup()
65+
{
66+
$phpPowerPoint = new PhpPowerpoint();
67+
$oSlide = $phpPowerPoint->getActiveSlide();
68+
$oShapeGroup = $oSlide->createGroup();
69+
$oShape = $oShapeGroup->createDrawingShape();
70+
$oShape->setPath(PHPPOWERPOINT_TESTS_BASE_DIR.'/resources/images/PHPPowerPointLogo.png');
71+
$oShape->getHyperlink()->setUrl('https://github.com/PHPOffice/PHPPowerPoint/');
72+
73+
$pres = TestHelperDOCX::getDocument($phpPowerPoint, 'ODPresentation');
74+
75+
$element = '/office:document-content/office:body/office:presentation/draw:page/draw:g';
76+
$this->assertTrue($pres->elementExists($element, 'content.xml'));
77+
$element = '/office:document-content/office:body/office:presentation/draw:page/draw:g/draw:frame/office:event-listeners/presentation:event-listener';
78+
$this->assertTrue($pres->elementExists($element, 'content.xml'));
79+
}
6080

6181
public function testList()
6282
{
@@ -171,7 +191,7 @@ public function testNote()
171191
$this->assertTrue($pres->elementExists($element, 'content.xml'));
172192
}
173193

174-
public function testRichtextAutoShrink()
194+
public function testRichTextAutoShrink()
175195
{
176196
$phpPowerPoint = new PhpPowerpoint();
177197
$oSlide = $phpPowerPoint->getActiveSlide();
@@ -202,7 +222,7 @@ public function testRichtextAutoShrink()
202222
$this->assertEquals('true', $pres->getElementAttribute($element, 'draw:auto-grow-width', 'content.xml'));
203223
}
204224

205-
public function testRichtextBorder()
225+
public function testRichTextBorder()
206226
{
207227
$phpPowerPoint = new PhpPowerpoint();
208228
$oSlide = $phpPowerPoint->getActiveSlide();
@@ -241,18 +261,49 @@ public function testRichtextBorder()
241261

242262
public function testRichTextShadow()
243263
{
264+
$randAlpha = rand(0,100);
244265
$phpPowerPoint = new PhpPowerpoint();
245266
$oSlide = $phpPowerPoint->getActiveSlide();
246267
$oRichText = $oSlide->createRichTextShape();
247268
$oRichText->createTextRun('AAA');
248-
$oRichText->getShadow()->setVisible(true)->setAlpha(75)->setBlurRadius(2)->setDirection(45);
249-
250-
$pres = TestHelperDOCX::getDocument($phpPowerPoint, 'ODPresentation');
269+
$oRichText->getShadow()->setVisible(true)->setAlpha($randAlpha)->setBlurRadius(2);
251270

252271
$element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'gr1\']/style:graphic-properties';
253-
$this->assertTrue($pres->elementExists($element, 'content.xml'));
254-
$this->assertEquals('visible', $pres->getElementAttribute($element, 'draw:shadow', 'content.xml'));
255-
$this->assertStringStartsWith('#', $pres->getElementAttribute($element, 'draw:shadow-color', 'content.xml'));
272+
for ($inc = 0 ; $inc <= 360 ; $inc += 45) {
273+
$randDistance = rand(0, 100);
274+
$oRichText->getShadow()->setDirection($inc)->setDistance($randDistance);
275+
$pres = TestHelperDOCX::getDocument($phpPowerPoint, 'ODPresentation');
276+
$this->assertTrue($pres->elementExists($element, 'content.xml'));
277+
$this->assertEquals('visible', $pres->getElementAttribute($element, 'draw:shadow', 'content.xml'));
278+
$this->assertEquals('none', $pres->getElementAttribute($element, 'style:mirror', 'content.xml'));
279+
// Opacity
280+
$this->assertStringStartsWith((string)(100 - $randAlpha), $pres->getElementAttribute($element, 'draw:shadow-opacity', 'content.xml'));
281+
$this->assertStringEndsWith('%', $pres->getElementAttribute($element, 'draw:shadow-opacity', 'content.xml'));
282+
// Color
283+
$this->assertStringStartsWith('#', $pres->getElementAttribute($element, 'draw:shadow-color', 'content.xml'));
284+
// X
285+
$xOffset = $pres->getElementAttribute($element, 'draw:shadow-offset-x', 'content.xml');
286+
if ($inc == 90 || $inc == 270) {
287+
$this->assertEquals('0cm', $xOffset);
288+
} else {
289+
if ($inc > 90 && $inc < 270) {
290+
$this->assertEquals('-'.Drawing::pixelsToCentimeters($randDistance).'cm', $xOffset);
291+
} else {
292+
$this->assertEquals(Drawing::pixelsToCentimeters($randDistance).'cm', $xOffset);
293+
}
294+
}
295+
// Y
296+
$yOffset = $pres->getElementAttribute($element, 'draw:shadow-offset-y', 'content.xml');
297+
if ($inc == 0 || $inc == 180 || $inc == 360) {
298+
$this->assertEquals('0cm', $yOffset);
299+
} else {
300+
if (($inc > 0 && $inc < 180) || $inc == 360) {
301+
$this->assertEquals(Drawing::pixelsToCentimeters($randDistance).'cm', $yOffset);
302+
} else {
303+
$this->assertEquals('-'.Drawing::pixelsToCentimeters($randDistance).'cm', $yOffset);
304+
}
305+
}
306+
}
256307
}
257308

258309
public function testStyleAlignment()
@@ -344,6 +395,32 @@ public function testTable()
344395
$this->assertTrue($pres->elementExists($element, 'content.xml'));
345396
}
346397

398+
public function testTableCellFill()
399+
{
400+
$oColor = new Color();
401+
$oColor->setRGB(Color::COLOR_BLUE);
402+
403+
$oFill = new Fill();
404+
$oFill->setFillType(Fill::FILL_SOLID)->setStartColor($oColor);
405+
406+
$phpPowerPoint = new PhpPowerpoint();
407+
$oSlide = $phpPowerPoint->getActiveSlide();
408+
$oShape = $oSlide->createTableShape();
409+
$oRow = $oShape->createRow();
410+
$oCell = $oRow->getCell();
411+
$oCell->setFill($oFill);
412+
413+
$pres = TestHelperDOCX::getDocument($phpPowerPoint, 'ODPresentation');
414+
$element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'gr1r0c0\']';
415+
$this->assertTrue($pres->elementExists($element, 'content.xml'));
416+
$this->assertEquals('table-cell', $pres->getElementAttribute($element, 'style:family', 'content.xml'));
417+
$element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'gr1r0c0\']/style:graphic-properties';
418+
$this->assertTrue($pres->elementExists($element, 'content.xml'));
419+
$this->assertEquals('solid', $pres->getElementAttribute($element, 'draw:fill', 'content.xml'));
420+
$this->assertStringStartsWith('#', $pres->getElementAttribute($element, 'draw:fill-color', 'content.xml'));
421+
$this->assertStringEndsWith($oColor->getRGB(), $pres->getElementAttribute($element, 'draw:fill-color', 'content.xml'));
422+
}
423+
347424
public function testTableWithColspan()
348425
{
349426
$value = rand(2, 100);
@@ -394,11 +471,14 @@ public function testTableWithText()
394471
$oRow = $oShape->createRow();
395472
$oCell = $oRow->getCell();
396473
$oCell->addText($oRun);
474+
$oCell->createBreak();
397475

398476
$pres = TestHelperDOCX::getDocument($phpPowerPoint, 'ODPresentation');
399477
$element = '/office:document-content/office:body/office:presentation/draw:page/draw:frame/table:table/table:table-row/table:table-cell/text:p/text:span';
400478
$this->assertTrue($pres->elementExists($element, 'content.xml'));
401479
$this->assertEquals('Test', $pres->getElement($element, 'content.xml')->nodeValue);
480+
$element = '/office:document-content/office:body/office:presentation/draw:page/draw:frame/table:table/table:table-row/table:table-cell/text:p/text:span/text:line-break';
481+
$this->assertTrue($pres->elementExists($element, 'content.xml'));
402482
}
403483

404484
public function testTransition()

tests/PhpPowerpoint/Tests/Writer/ODPresentationTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ public function testSave()
7878
unlink($filename);
7979
}
8080

81+
/**
82+
* Test get PHPPowerPoint exception
83+
*
84+
* @expectedException Exception
85+
* @expectedExceptionMessage Filename is empty
86+
*/
87+
public function testSaveEmpty()
88+
{
89+
$object = new ODPresentation();
90+
$phpPowerPoint = new PhpPowerpoint();
91+
$object->save('');
92+
}
93+
8194
/**
8295
* Test get writer part null
8396
*/

tests/PhpPowerpoint/Tests/Writer/PowerPoint2007/AbstractPartTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PhpOffice\PhpPowerpoint\Writer\PowerPoint2007\Drawing;
2222
use PhpOffice\PhpPowerpoint\Writer\PowerPoint2007\Rels;
2323
use PhpOffice\PhpPowerpoint\Writer\ODPresentation;
24+
use PhpOffice\PhpPowerpoint\Writer\PowerPoint2007;
2425

2526
/**
2627
* Test class for PhpOffice\PhpPowerpoint\Writer\ODPresentation
@@ -29,6 +30,13 @@
2930
*/
3031
class AbstractPartTest extends \PHPUnit_Framework_TestCase
3132
{
33+
protected function runProtectedMethod ($obj, $method, $args = array())
34+
{
35+
$method = new \ReflectionMethod(get_class($obj), $method);
36+
$method->setAccessible(true);
37+
return $method->invokeArgs($obj, $args);
38+
}
39+
3240
/**
3341
* Executed before each method of the class
3442
*/
@@ -56,4 +64,14 @@ public function testWriterException()
5664
$oManifest->setParentWriter(new ODPresentation());
5765
$oManifest->writeRelationships();
5866
}
67+
68+
public function testXMLWriterWithDiskCaching()
69+
{
70+
$oPowerPoint2007 = new PowerPoint2007();
71+
$oPowerPoint2007->setUseDiskCaching(true);
72+
$oRels = new Rels();
73+
$oRels->setParentWriter($oPowerPoint2007);
74+
75+
$this->assertNotEmpty(\PHPUnit_Framework_Assert::readAttribute($this->runProtectedMethod($oRels, 'getXMLWriter'), 'tempFileName'));
76+
}
5977
}

0 commit comments

Comments
 (0)