From 05a4b952557f4d6fedda95362474f8a8d8c95715 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 30 Mar 2014 11:09:14 +0700 Subject: [PATCH] Unit test enhancements --- src/PhpWord/Section/CheckBox.php | 17 --- src/PhpWord/Writer/Word2007/Base.php | 12 -- tests/PhpWord/Tests/Section/CheckBoxTest.php | 11 ++ tests/PhpWord/Tests/Section/FooterTest.php | 62 ++++++++-- tests/PhpWord/Tests/Section/FootnoteTest.php | 18 --- tests/PhpWord/Tests/Section/HeaderTest.php | 59 ++++++++- tests/PhpWord/Tests/Section/ImageTest.php | 50 +++++++- .../PhpWord/Tests/Section/Table/CellTest.php | 115 ++++++++++++++++-- tests/PhpWord/Tests/Section/TextRunTest.php | 39 +++++- tests/PhpWord/Tests/Section/TextTest.php | 24 +++- tests/PhpWord/Tests/SectionTest.php | 2 +- .../Tests/Writer/Word2007/DocumentTest.php | 42 ++++++- .../Tests/Writer/Word2007/FootnotesTest.php | 10 +- tests/PhpWord/Tests/Writer/Word2007Test.php | 2 + 14 files changed, 378 insertions(+), 85 deletions(-) diff --git a/src/PhpWord/Section/CheckBox.php b/src/PhpWord/Section/CheckBox.php index f39ad841..57671b84 100644 --- a/src/PhpWord/Section/CheckBox.php +++ b/src/PhpWord/Section/CheckBox.php @@ -45,13 +45,6 @@ class CheckBox */ private $paragraphStyle; - /** - * Cell Element Collection - * - * @var array - */ - private $_elementCollection = array(); - /** * Create a new Text Element * @@ -174,14 +167,4 @@ class CheckBox { return $this->text; } - - /** - * Get all Elements - * - * @return array - */ - public function getElements() - { - return $this->_elementCollection; - } } diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index 5703a5a9..c32eede3 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -1223,11 +1223,6 @@ class Base extends WriterPart */ protected function _writeCheckBox(XMLWriter $xmlWriter, CheckBox $checkbox, $withoutP = false, $checkState = false) { - $count = 1; - $_elements = $checkbox->getElements(); - if (count($_elements) > 1) { - $count = $count + 1; - } $name = htmlspecialchars($checkbox->getName()); $name = String::controlCharacterPHP2OOXML($name); $text = htmlspecialchars($checkbox->getText()); @@ -1271,9 +1266,6 @@ class Base extends WriterPart $xmlWriter->endElement(); // w:fldChar $xmlWriter->endElement(); // w:r - $xmlWriter->startElement('w:bookmarkStart'); - $xmlWriter->writeAttribute('w:name', $name); - $xmlWriter->writeAttribute('w:id', $count); $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:instrText'); $xmlWriter->writeAttribute('xml:space', 'preserve'); @@ -1290,10 +1282,6 @@ class Base extends WriterPart $xmlWriter->writeAttribute('w:fldCharType', 'end'); $xmlWriter->endElement();// w:fldChar $xmlWriter->endElement(); // w:r - $xmlWriter->endElement(); // w:bookmarkStart - $xmlWriter->startElement('w:bookmarkEnd'); - $xmlWriter->writeAttribute('w:id', $count); - $xmlWriter->endElement();// w:bookmarkEnd $xmlWriter->startElement('w:r'); if ($sfIsObject) { diff --git a/tests/PhpWord/Tests/Section/CheckBoxTest.php b/tests/PhpWord/Tests/Section/CheckBoxTest.php index b69217a5..d07a0b69 100644 --- a/tests/PhpWord/Tests/Section/CheckBoxTest.php +++ b/tests/PhpWord/Tests/Section/CheckBoxTest.php @@ -10,6 +10,7 @@ namespace PhpOffice\PhpWord\Tests\Section; use PhpOffice\PhpWord\Section\CheckBox; +use PhpOffice\PhpWord\Style\Font; /** * Test class for PhpOffice\PhpWord\Section\CheckBox @@ -54,6 +55,16 @@ class CheckBoxTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', $oCheckBox->getFontStyle()); } + /** + * Font style as object + */ + public function testFontObject() + { + $font = new Font(); + $oCheckBox = new CheckBox('chkBox', 'CheckBox', $font); + $this->assertEquals($oCheckBox->getFontStyle(), $font); + } + /** * Get paragraph style */ diff --git a/tests/PhpWord/Tests/Section/FooterTest.php b/tests/PhpWord/Tests/Section/FooterTest.php index 6ae3bc60..d14f125c 100644 --- a/tests/PhpWord/Tests/Section/FooterTest.php +++ b/tests/PhpWord/Tests/Section/FooterTest.php @@ -14,11 +14,13 @@ use PhpOffice\PhpWord\Section\Footer; /** * Test class for PhpOffice\PhpWord\Section\Footer * - * @coversDefaultClass \PhpOffice\PhpWord\Section\Footer * @runTestsInSeparateProcesses */ class FooterTest extends \PHPUnit_Framework_TestCase { + /** + * New instance + */ public function testConstruct() { $iVal = rand(1, 1000); @@ -28,15 +30,9 @@ class FooterTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oFooter->getFooterCount(), $iVal); } - public function testRelationID() - { - $oFooter = new Footer(0); - - $iVal = rand(1, 1000); - $oFooter->setRelationId($iVal); - $this->assertEquals($oFooter->getRelationId(), $iVal); - } - + /** + * Add text + */ public function testAddText() { $oFooter = new Footer(1); @@ -46,6 +42,9 @@ class FooterTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Text', $element); } + /** + * Add text non-UTF8 + */ public function testAddTextNotUTF8() { $oFooter = new Footer(1); @@ -56,6 +55,9 @@ class FooterTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getText(), 'ééé'); } + /** + * Add text break + */ public function testAddTextBreak() { $oFooter = new Footer(1); @@ -65,6 +67,9 @@ class FooterTest extends \PHPUnit_Framework_TestCase $this->assertCount($iVal, $oFooter->getElements()); } + /** + * Add text run + */ public function testCreateTextRun() { $oFooter = new Footer(1); @@ -74,6 +79,9 @@ class FooterTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\TextRun', $element); } + /** + * Add table + */ public function testAddTable() { $oFooter = new Footer(1); @@ -83,16 +91,23 @@ class FooterTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Table', $element); } + /** + * Add image + */ public function testAddImage() { $src = __DIR__ . "/../_files/images/earth.jpg"; $oFooter = new Footer(1); - $element = $oFooter->addImage($src); + $element1 = $oFooter->addImage($src); + $element2 = $oFooter->addMemoryImage($src); // @deprecated - $this->assertCount(1, $oFooter->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); + $this->assertCount(2, $oFooter->getElements()); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element1); } + /** + * Add image by URL + */ public function testAddImageByUrl() { $oFooter = new Footer(1); @@ -104,6 +119,9 @@ class FooterTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); } + /** + * Add preserve text + */ public function testAddPreserveText() { $oFooter = new Footer(1); @@ -113,6 +131,9 @@ class FooterTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Footer\\PreserveText', $element); } + /** + * Add preserve text non-UTF8 + */ public function testAddPreserveTextNotUTF8() { $oFooter = new Footer(1); @@ -123,10 +144,25 @@ class FooterTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getText(), array('ééé')); } + /** + * Get elements + */ public function testGetElements() { $oFooter = new Footer(1); $this->assertInternalType('array', $oFooter->getElements()); } + + /** + * Set/get relation Id + */ + public function testRelationID() + { + $oFooter = new Footer(0); + + $iVal = rand(1, 1000); + $oFooter->setRelationId($iVal); + $this->assertEquals($oFooter->getRelationId(), $iVal); + } } diff --git a/tests/PhpWord/Tests/Section/FootnoteTest.php b/tests/PhpWord/Tests/Section/FootnoteTest.php index ed2e36de..fc537ab8 100644 --- a/tests/PhpWord/Tests/Section/FootnoteTest.php +++ b/tests/PhpWord/Tests/Section/FootnoteTest.php @@ -14,15 +14,12 @@ use PhpOffice\PhpWord\Section\Footnote; /** * Test class for PhpOffice\PhpWord\Section\Footnote * - * @coversDefaultClass \PhpOffice\PhpWord\Section\Footnote * @runTestsInSeparateProcesses */ class FootnoteTest extends \PHPUnit_Framework_TestCase { /** * New instance without parameter - * - * @covers ::__construct */ public function testConstruct() { @@ -35,8 +32,6 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase /** * New instance with string parameter - * - * @covers ::__construct */ public function testConstructString() { @@ -47,8 +42,6 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase /** * New instance with array parameter - * - * @covers ::__construct */ public function testConstructArray() { @@ -62,8 +55,6 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase /** * Add text element - * - * @covers ::addText */ public function testAddText() { @@ -76,8 +67,6 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase /** * Add text break element - * - * @covers ::addTextBreak */ public function testAddTextBreak() { @@ -89,8 +78,6 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase /** * Add link element - * - * @covers ::addLink */ public function testAddLink() { @@ -103,9 +90,6 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase /** * Set/get reference Id - * - * @covers ::setReferenceId - * @covers ::getReferenceId */ public function testReferenceId() { @@ -118,8 +102,6 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase /** * Get elements - * - * @covers ::getElements */ public function testGetElements() { diff --git a/tests/PhpWord/Tests/Section/HeaderTest.php b/tests/PhpWord/Tests/Section/HeaderTest.php index 795b49c3..edc5d2c6 100644 --- a/tests/PhpWord/Tests/Section/HeaderTest.php +++ b/tests/PhpWord/Tests/Section/HeaderTest.php @@ -14,11 +14,13 @@ use PhpOffice\PhpWord\Section\Header; /** * Test class for PhpOffice\PhpWord\Section\Header * - * @coversDefaultClass \PhpOffice\PhpWord\Section\Header * @runTestsInSeparateProcesses */ class HeaderTest extends \PHPUnit_Framework_TestCase { + /** + * New instance + */ public function testConstructDefault() { $iVal = rand(1, 1000); @@ -29,6 +31,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oHeader->getType(), Header::AUTO); } + /** + * Add text + */ public function testAddText() { $oHeader = new Header(1); @@ -39,6 +44,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getText(), 'text'); } + /** + * Add text non-UTF8 + */ public function testAddTextNotUTF8() { $oHeader = new Header(1); @@ -49,6 +57,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getText(), 'ééé'); } + /** + * Add text break + */ public function testAddTextBreak() { $oHeader = new Header(1); @@ -56,6 +67,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertCount(1, $oHeader->getElements()); } + /** + * Add text break with params + */ public function testAddTextBreakWithParams() { $oHeader = new Header(1); @@ -64,6 +78,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertCount($iVal, $oHeader->getElements()); } + /** + * Add text run + */ public function testCreateTextRun() { $oHeader = new Header(1); @@ -72,6 +89,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertCount(1, $oHeader->getElements()); } + /** + * Add table + */ public function testAddTable() { $oHeader = new Header(1); @@ -80,16 +100,23 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertCount(1, $oHeader->getElements()); } + /** + * Add image + */ public function testAddImage() { $src = __DIR__ . "/../_files/images/earth.jpg"; $oHeader = new Header(1); - $element = $oHeader->addImage($src); + $element1 = $oHeader->addImage($src); + $element2 = $oHeader->addMemoryImage($src); // @deprecated - $this->assertCount(1, $oHeader->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); + $this->assertCount(2, $oHeader->getElements()); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element1); } + /** + * Add image by URL + */ public function testAddImageByUrl() { $oHeader = new Header(1); @@ -101,6 +128,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); } + /** + * Add preserve text + */ public function testAddPreserveText() { $oHeader = new Header(1); @@ -110,6 +140,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Footer\\PreserveText', $element); } + /** + * Add preserve text non-UTF8 + */ public function testAddPreserveTextNotUTF8() { $oHeader = new Header(1); @@ -120,6 +153,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getText(), array('ééé')); } + /** + * Add watermark + */ public function testAddWatermark() { $src = __DIR__ . "/../_files/images/earth.jpg"; @@ -130,6 +166,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); } + /** + * Get elements + */ public function testGetElements() { $oHeader = new Header(1); @@ -137,6 +176,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertInternalType('array', $oHeader->getElements()); } + /** + * Set/get relation Id + */ public function testRelationId() { $oHeader = new Header(1); @@ -146,6 +188,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oHeader->getRelationId(), $iVal); } + /** + * Reset type + */ public function testResetType() { $oHeader = new Header(1); @@ -155,6 +200,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oHeader->getType(), Header::AUTO); } + /** + * First page + */ public function testFirstPage() { $oHeader = new Header(1); @@ -163,6 +211,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oHeader->getType(), Header::FIRST); } + /** + * Even page + */ public function testEvenPage() { $oHeader = new Header(1); diff --git a/tests/PhpWord/Tests/Section/ImageTest.php b/tests/PhpWord/Tests/Section/ImageTest.php index 7e4a25b4..bd4bf394 100644 --- a/tests/PhpWord/Tests/Section/ImageTest.php +++ b/tests/PhpWord/Tests/Section/ImageTest.php @@ -14,11 +14,13 @@ use PhpOffice\PhpWord\Section\Image; /** * Test class for PhpOffice\PhpWord\Section\Image * - * @coversDefaultClass \PhpOffice\PhpWord\Section\Image * @runTestsInSeparateProcesses */ class ImageTest extends \PHPUnit_Framework_TestCase { + /** + * New instance + */ public function testConstruct() { $src = __DIR__ . "/../_files/images/firefox.png"; @@ -31,6 +33,9 @@ class ImageTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Image', $oImage->getStyle()); } + /** + * New instance with style + */ public function testConstructWithStyle() { $src = __DIR__ . "/../_files/images/firefox.png"; @@ -44,7 +49,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase } /** - * @covers ::__construct + * Valid image types */ public function testValidImageTypes() { @@ -57,8 +62,9 @@ class ImageTest extends \PHPUnit_Framework_TestCase } /** + * Image not found + * * @expectedException \PhpOffice\PhpWord\Exceptions\InvalidImageException - * @covers ::__construct */ public function testImageNotFound() { @@ -66,14 +72,18 @@ class ImageTest extends \PHPUnit_Framework_TestCase } /** + * Invalid image types + * * @expectedException \PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException - * @covers ::__construct */ public function testInvalidImageTypes() { new Image(__DIR__ . "/../_files/images/alexz-johnson.pcx"); } + /** + * Get style + */ public function testStyle() { $oImage = new Image( @@ -84,6 +94,9 @@ class ImageTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Image', $oImage->getStyle()); } + /** + * Get relation Id + */ public function testRelationID() { $oImage = new Image(__DIR__ . "/../_files/images/earth.jpg"); @@ -92,12 +105,19 @@ class ImageTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oImage->getRelationId(), $iVal); } + /** + * Get is watermark + */ public function testWatermark() { $oImage = new Image(__DIR__ . "/../_files/images/earth.jpg"); $oImage->setIsWatermark(true); $this->assertEquals($oImage->getIsWatermark(), true); } + + /** + * Test PNG + */ public function testPNG() { $src = __DIR__ . "/../_files/images/firefox.png"; @@ -112,6 +132,9 @@ class ImageTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oImage->getImageType(), 'image/png'); } + /** + * Test GIF + */ public function testGIF() { $src = __DIR__ . "/../_files/images/mario.gif"; @@ -126,6 +149,9 @@ class ImageTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oImage->getImageType(), 'image/gif'); } + /** + * Test JPG + */ public function testJPG() { $src = __DIR__ . "/../_files/images/earth.jpg"; @@ -140,6 +166,9 @@ class ImageTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oImage->getImageType(), 'image/jpeg'); } + /** + * Test BMP + */ public function testBMP() { $oImage = new Image(__DIR__ . "/../_files/images/duke_nukem.bmp"); @@ -150,4 +179,17 @@ class ImageTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oImage->getImageExtension(), 'bmp'); $this->assertEquals($oImage->getImageType(), 'image/bmp'); } + + /** + * Test TIFF + */ + public function testTIFF() + { + $oImage = new Image(__DIR__ . "/../_files/images/angela_merkel.tif"); + + $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $oImage); + $this->assertEquals($oImage->getImageCreateFunction(), null); + $this->assertEquals($oImage->getImageFunction(), null); + $this->assertEquals($oImage->getImageType(), 'image/tiff'); + } } diff --git a/tests/PhpWord/Tests/Section/Table/CellTest.php b/tests/PhpWord/Tests/Section/Table/CellTest.php index fc0c0a97..6f38d986 100644 --- a/tests/PhpWord/Tests/Section/Table/CellTest.php +++ b/tests/PhpWord/Tests/Section/Table/CellTest.php @@ -14,11 +14,13 @@ use PhpOffice\PhpWord\Section\Table\Cell; /** * Test class for PhpOffice\PhpWord\Section\Table\Cell * - * @coversDefaultClass \PhpOffice\PhpWord\Section\Table\Cell * @runTestsInSeparateProcesses */ class CellTest extends \PHPUnit_Framework_TestCase { + /** + * New instance + */ public function testConstruct() { $iVal = rand(1, 1000); @@ -28,6 +30,9 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oCell->getWidth(), null); } + /** + * New instance with array + */ public function testConstructWithStyleArray() { $iVal = rand(1, 1000); @@ -37,6 +42,9 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oCell->getWidth(), null); } + /** + * New instance with string + */ public function testConstructWithStyleString() { $iVal = rand(1, 1000); @@ -45,6 +53,9 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oCell->getStyle(), 'cellStyle'); } + /** + * Add text + */ public function testAddText() { $oCell = new Cell('section', 1); @@ -54,6 +65,9 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Text', $element); } + /** + * Add non-UTF8 + */ public function testAddTextNotUTF8() { $oCell = new Cell('section', 1); @@ -64,15 +78,31 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getText(), 'ééé'); } + /** + * Add link + */ public function testAddLink() { $oCell = new Cell('section', 1); - $element = $oCell->addLink('http://www.google.fr', 'Nom'); + $element = $oCell->addLink(utf8_decode('ééé'), utf8_decode('ééé')); $this->assertCount(1, $oCell->getElements()); $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Link', $element); } + /** + * Add link exception + * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + */ + public function testAddLinkException() + { + $oCell = new Cell('header', 1); + $element = $oCell->addLink('http://google.com', 'Google'); + } + + /** + * Add text break + */ public function testAddTextBreak() { $oCell = new Cell('section', 1); @@ -81,6 +111,9 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertCount(1, $oCell->getElements()); } + /** + * Add list item + */ public function testAddListItem() { $oCell = new Cell('section', 1); @@ -91,6 +124,9 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getTextObject()->getText(), 'text'); } + /** + * Add list item non-UTF8 + */ public function testAddListItemNotUTF8() { $oCell = new Cell('section', 1); @@ -101,16 +137,23 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getTextObject()->getText(), 'ééé'); } + /** + * Add image section + */ public function testAddImageSection() { $src = __DIR__ . "/../../_files/images/earth.jpg"; $oCell = new Cell('section', 1); - $element = $oCell->addImage($src); + $element1 = $oCell->addImage($src); + $element2 = $oCell->addMemoryImage($src); // @deprecated - $this->assertCount(1, $oCell->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); + $this->assertCount(2, $oCell->getElements()); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element1); } + /** + * Add image header + */ public function testAddImageHeader() { $src = __DIR__ . "/../../_files/images/earth.jpg"; @@ -121,6 +164,9 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); } + /** + * Add image footer + */ public function testAddImageFooter() { $src = __DIR__ . "/../../_files/images/earth.jpg"; @@ -131,7 +177,10 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); } - public function testAddSectionImageByUrl() + /** + * Add image section by URL + */ + public function testAddImageSectionByUrl() { $oCell = new Cell('section', 1); $element = $oCell->addImage( @@ -142,7 +191,10 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); } - public function testAddHeaderImageByUrl() + /** + * Add image header by URL + */ + public function testAddImageHeaderByUrl() { $oCell = new Cell('header', 1); $element = $oCell->addImage( @@ -153,7 +205,10 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); } - public function testAddFooterImageByUrl() + /** + * Add image footer by URL + */ + public function testAddImageFooterByUrl() { $oCell = new Cell('footer', 1); $element = $oCell->addImage( @@ -164,6 +219,9 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); } + /** + * Add object + */ public function testAddObjectXLS() { $src = __DIR__ . "/../../_files/documents/sheet.xls"; @@ -174,6 +232,21 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Object', $element); } + /** + * Test add object exception + * + * @expectedException \PhpOffice\PhpWord\Exceptions\InvalidObjectException + */ + public function testAddObjectException() + { + $src = __DIR__ . "/_files/xsl/passthrough.xsl"; + $oCell = new Cell('section', 1); + $element = $oCell->addObject($src); + } + + /** + * Add preserve text + */ public function testAddPreserveText() { $oCell = new Cell('header', 1); @@ -183,6 +256,9 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Footer\\PreserveText', $element); } + /** + * Add preserve text non-UTF8 + */ public function testAddPreserveTextNotUTF8() { $oCell = new Cell('header', 1); @@ -193,6 +269,20 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getText(), array('ééé')); } + /** + * Add preserve text exception + * + * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + */ + public function testAddPreserveTextException() + { + $oCell = new Cell('section', 1); + $element = $oCell->addPreserveText('text'); + } + + /** + * Add text run + */ public function testCreateTextRun() { $oCell = new Cell('section', 1); @@ -202,16 +292,21 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\TextRun', $element); } + /** + * Add check box + */ public function testAddCheckBox() { $oCell = new Cell('section', 1); - $element = $oCell->addCheckBox('check1', 'text'); + $element = $oCell->addCheckBox(utf8_decode('ééé'), utf8_decode('ééé')); $this->assertCount(1, $oCell->getElements()); $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\CheckBox', $element); } - + /** + * Get elements + */ public function testGetElements() { $oCell = new Cell('section', 1); diff --git a/tests/PhpWord/Tests/Section/TextRunTest.php b/tests/PhpWord/Tests/Section/TextRunTest.php index dea3fcdd..32b6d4dc 100644 --- a/tests/PhpWord/Tests/Section/TextRunTest.php +++ b/tests/PhpWord/Tests/Section/TextRunTest.php @@ -14,11 +14,13 @@ use PhpOffice\PhpWord\Section\TextRun; /** * Test class for PhpOffice\PhpWord\Section\TextRun * - * @coversDefaultClass \PhpOffice\PhpWord\Section\TextRun * @runTestsInSeparateProcesses */ class TextRunTest extends \PHPUnit_Framework_TestCase { + /** + * New instance + */ public function testConstructNull() { $oTextRun = new TextRun(); @@ -28,6 +30,9 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oTextRun->getParagraphStyle(), null); } + /** + * New instance with string + */ public function testConstructString() { $oTextRun = new TextRun('pStyle'); @@ -37,6 +42,9 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oTextRun->getParagraphStyle(), 'pStyle'); } + /** + * New instance with array + */ public function testConstructArray() { $oTextRun = new TextRun(array('spacing' => 100)); @@ -46,6 +54,9 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oTextRun->getParagraphStyle()); } + /** + * Add text + */ public function testAddText() { $oTextRun = new TextRun(); @@ -56,6 +67,9 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getText(), 'text'); } + /** + * Add text non-UTF8 + */ public function testAddTextNotUTF8() { $oTextRun = new TextRun(); @@ -66,6 +80,9 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getText(), 'ééé'); } + /** + * Add link + */ public function testAddLink() { $oTextRun = new TextRun(); @@ -76,6 +93,9 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getLinkSrc(), 'http://www.google.fr'); } + /** + * Add link with name + */ public function testAddLinkWithName() { $oTextRun = new TextRun(); @@ -87,6 +107,20 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getLinkName(), 'ééé'); } + /** + * Add text break + */ + public function testAddTextBreak() + { + $oTextRun = new TextRun(); + $element = $oTextRun->addTextBreak(2); + + $this->assertCount(2, $oTextRun->getElements()); + } + + /** + * Add image + */ public function testAddImage() { $src = __DIR__ . "/../_files/images/earth.jpg"; @@ -98,6 +132,9 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $this->assertCount(1, $oTextRun->getElements()); } + /** + * Add footnote + */ public function testCreateFootnote() { $oTextRun = new TextRun(); diff --git a/tests/PhpWord/Tests/Section/TextTest.php b/tests/PhpWord/Tests/Section/TextTest.php index 4124f27a..5811c735 100644 --- a/tests/PhpWord/Tests/Section/TextTest.php +++ b/tests/PhpWord/Tests/Section/TextTest.php @@ -10,15 +10,18 @@ namespace PhpOffice\PhpWord\Tests\Section; use PhpOffice\PhpWord\Section\Text; +use PhpOffice\PhpWord\Style\Font; /** * Test class for PhpOffice\PhpWord\Section\Text * - * @coversDefaultClass \PhpOffice\PhpWord\Section\Text * @runTestsInSeparateProcesses */ class TextTest extends \PHPUnit_Framework_TestCase { + /** + * New instance + */ public function testConstruct() { $oText = new Text(); @@ -29,6 +32,9 @@ class TextTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oText->getParagraphStyle()); } + /** + * Get text + */ public function testText() { $oText = new Text('text'); @@ -36,6 +42,9 @@ class TextTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oText->getText(), 'text'); } + /** + * Get font style + */ public function testFont() { $oText = new Text('text', 'fontStyle'); @@ -45,6 +54,19 @@ class TextTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', $oText->getFontStyle()); } + /** + * Get font style as object + */ + public function testFontObject() + { + $font = new Font(); + $oText = new Text('text', $font); + $this->assertEquals($oText->getFontStyle(), $font); + } + + /** + * Get paragraph style + */ public function testParagraph() { $oText = new Text('text', 'fontStyle', 'paragraphStyle'); diff --git a/tests/PhpWord/Tests/SectionTest.php b/tests/PhpWord/Tests/SectionTest.php index e6205db3..48b5bb73 100644 --- a/tests/PhpWord/Tests/SectionTest.php +++ b/tests/PhpWord/Tests/SectionTest.php @@ -88,7 +88,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase $section->addTitle(utf8_decode('ä'), 1); $section->createTextRun(); $section->createFootnote(); - $section->addCheckBox('check1', utf8_decode('ä')); + $section->addCheckBox(utf8_decode('chkä'), utf8_decode('Contentä')); $section->addTOC(); $elementCollection = $section->getElements(); diff --git a/tests/PhpWord/Tests/Writer/Word2007/DocumentTest.php b/tests/PhpWord/Tests/Writer/Word2007/DocumentTest.php index f0b44e0b..5318ac81 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/DocumentTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/DocumentTest.php @@ -9,6 +9,7 @@ namespace PhpOffice\PhpWord\Tests\Writer\Word2007; use PhpOffice\PhpWord\PhpWord; +use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Tests\TestHelperDOCX; /** @@ -33,7 +34,11 @@ class DocumentTest extends \PHPUnit_Framework_TestCase { $phpWord = new PhpWord(); $section = $phpWord->createSection(); - $section->getSettings()->setPageNumberingStart(2); + $settings = $section->getSettings(); + $settings->setLandscape(); + $settings->setPageNumberingStart(2); + $settings->setBorderSize(240); + $settings->setBreakType('nextPage'); $doc = TestHelperDOCX::getDocument($phpWord); $element = $doc->getElement('/w:document/w:body/w:sectPr/w:pgNumType'); @@ -85,4 +90,39 @@ class DocumentTest extends \PHPUnit_Framework_TestCase $element = $doc->getElement('/w:document/w:body/w:p[11]/w:r/w:object/o:OLEObject'); $this->assertEquals('Embed', $element->getAttribute('Type')); } + + /** + * Write element with some styles + */ + public function testElementStyles() + { + $objectSrc = __DIR__ . "/../../_files/documents/sheet.xls"; + + $phpWord = new PhpWord(); + $phpWord->addParagraphStyle('pStyle', array('align' => 'center')); + $phpWord->addFontStyle('fStyle', array('size' => '20')); + $phpWord->addTitleStyle(1, array('color' => '333333', 'bold' => true)); + $fontStyle = new Font('text', array('align' => 'center')); + $section = $phpWord->createSection(); + $section->addListItem('List Item', 0, null, null, 'pStyle'); + $section->addObject($objectSrc, array('align' => 'center')); + $section->addTOC($fontStyle); + $section->addTitle('Title 1', 1); + $section->addTOC('fStyle'); + $doc = TestHelperDOCX::getDocument($phpWord); + + // List item + $element = $doc->getElement('/w:document/w:body/w:p[1]/w:pPr/w:numPr/w:numId'); + $this->assertEquals(3, $element->getAttribute('w:val')); + + // Object + $element = $doc->getElement('/w:document/w:body/w:p[2]/w:r/w:object/o:OLEObject'); + $this->assertEquals('Embed', $element->getAttribute('Type')); + + // TOC + $element = $doc->getElement('/w:document/w:body/w:p[3]/w:pPr/w:tabs/w:tab'); + $this->assertEquals('right', $element->getAttribute('w:val')); + $this->assertEquals('dot', $element->getAttribute('w:leader')); + $this->assertEquals(9062, $element->getAttribute('w:pos')); + } } diff --git a/tests/PhpWord/Tests/Writer/Word2007/FootnotesTest.php b/tests/PhpWord/Tests/Writer/Word2007/FootnotesTest.php index 06906b41..a1220d26 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/FootnotesTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/FootnotesTest.php @@ -30,11 +30,15 @@ class FootnotesTest extends \PHPUnit_Framework_TestCase public function testWriteFootnotes() { $phpWord = new PhpWord(); + $phpWord->addParagraphStyle('pStyle', array('align' => 'left')); $section = $phpWord->createSection(); $section->addText('Text'); - $footnote = $section->createFootnote(); - $footnote->addText('Footnote'); - $footnote->addLink('http://google.com'); + $footnote1 = $section->createFootnote('pStyle'); + $footnote1->addText('Footnote'); + $footnote1->addTextBreak(); + $footnote1->addLink('http://google.com'); + $footnote2 = $section->createFootnote(array('align' => 'left')); + $footnote2->addText('Footnote'); $doc = TestHelperDOCX::getDocument($phpWord); $this->assertTrue($doc->elementExists("/w:document/w:body/w:p/w:r/w:footnoteReference")); diff --git a/tests/PhpWord/Tests/Writer/Word2007Test.php b/tests/PhpWord/Tests/Writer/Word2007Test.php index 22a1c0df..fa4e301c 100644 --- a/tests/PhpWord/Tests/Writer/Word2007Test.php +++ b/tests/PhpWord/Tests/Writer/Word2007Test.php @@ -99,6 +99,8 @@ class Word2007Test extends \PHPUnit_Framework_TestCase $phpWord = new PhpWord(); $section = $phpWord->createSection(); $section->addText('Test'); + $footnote = $section->createFootnote(); + $footnote->addText('Test'); $writer = new Word2007($phpWord); $writer->setUseDiskCaching(true);