diff --git a/CHANGELOG.md b/CHANGELOG.md index f9bcdd19..806c9c1c 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ This is the changelog between releases of PHPWord. Releases are listed in revers ## 0.10.0 - Not yet released -This release marked heavy refactorings on internal code structure with the creation of some abstract classes to reduce code duplication. `Element` subnamespace is introduced in this release to replace `Section`. Word2007 reader capability is greatly enhanced. Endnote is introduced. List numbering is now customizable. Basic HTML writer is initiated. +This release marked heavy refactorings on internal code structure with the creation of some abstract classes to reduce code duplication. `Element` subnamespace is introduced in this release to replace `Section`. Word2007 reader capability is greatly enhanced. Endnote is introduced. List numbering is now customizable. Basic HTML support is enabled. ### Features @@ -31,11 +31,13 @@ This release marked heavy refactorings on internal code structure with the creat - Endnote: Ability to add endnotes - @ivanlanin - ListItem: Ability to create custom list and reset list number - @ivanlanin GH-10 GH-198 - ODT Writer: Basic table writing support - @ivanlanin +- Image: Keep image aspect ratio if only 1 dimension styled - @japonicus GH-194 - HTML Writer: Basic HTML writer initiated - @ivanlanin ### Bugfixes - Footnote: Footnote content doesn't show footnote reference number - @ivanlanin GH-170 +- Documentation : Error in a fonction - @theBeerNut GH-195 ### Deprecated @@ -64,6 +66,8 @@ This release marked heavy refactorings on internal code structure with the creat - Style: New `Style\AbstractStyle` abstract class - @ivanlanin GH-187 - Writer: New 'ODText\Base` class - @ivanlanin GH-187 - General: Rename `Footnote` to `Footnotes` to reflect the nature of collection - @ivanlanin +- General: Add some unit tests for Shared & Element (100%!) - @Progi1984 +- Test: Add some samples and tests for image wrapping style - @brunocasado GH-59 ## 0.9.1 - 27 Mar 2014 diff --git a/docs/general.rst b/docs/general.rst index 40c8da7e..c7e55d5a 100644 --- a/docs/general.rst +++ b/docs/general.rst @@ -108,7 +108,7 @@ name. Use the following functions: .. code-block:: php - $properties = $phpWord->getProperties(); + $properties = $phpWord->getDocumentProperties(); $properties->setCreator('My name'); $properties->setCompany('My factory'); $properties->setTitle('My title'); diff --git a/samples/Sample_13_Images.php b/samples/Sample_13_Images.php index c8199cce..65e38bf5 100644 --- a/samples/Sample_13_Images.php +++ b/samples/Sample_13_Images.php @@ -10,15 +10,27 @@ $section = $phpWord->addSection(); $section->addText('Local image without any styles:'); $section->addImage('resources/_mars.jpg'); $section->addTextBreak(2); -// + $section->addText('Local image with styles:'); $section->addImage('resources/_earth.jpg', array('width' => 210, 'height' => 210, 'align' => 'center')); $section->addTextBreak(2); +// Remote image $source = 'http://php.net/images/logos/php-med-trans-light.gif'; $section->addText("Remote image from: {$source}"); $section->addImage($source); +//Wrapping style +$text = str_repeat('Hello World! ', 15); +$wrappingStyles = array('inline', 'behind', 'infront', 'square', 'tight'); +foreach ($wrappingStyles as $wrappingStyle) { + $section->addTextBreak(5); + $section->addText('Wrapping style ' . $wrappingStyle); + $section->addImage('resources/_earth.jpg', array('marginTop' => -1, 'marginLeft' => 1, + 'width' => 80, 'height' => 80, 'wrappingStyle' => $wrappingStyle)); + $section->addText($text); +} + // Save file echo write($phpWord, basename(__FILE__, '.php'), $writers); if (!CLI) { diff --git a/samples/Sample_Header.php b/samples/Sample_Header.php index ca3d3689..e3057db7 100644 --- a/samples/Sample_Header.php +++ b/samples/Sample_Header.php @@ -40,8 +40,9 @@ if ($handle = opendir('.')) { /** * Get results * - * @param array $writers + * @param \PhpOffice\PhpWord\PhpWord $phpWord * @param string $filename + * @param array $writers * @return string */ function write($phpWord, $filename, $writers) diff --git a/src/PhpWord/Element/Image.php b/src/PhpWord/Element/Image.php index 942ae224..ce4b1609 100755 --- a/src/PhpWord/Element/Image.php +++ b/src/PhpWord/Element/Image.php @@ -131,9 +131,18 @@ class Image extends AbstractElement $this->source = $source; $this->isWatermark = $isWatermark; $this->style = $this->setStyle(new ImageStyle(), $style, true); - if ($this->style->getWidth() == null && $this->style->getHeight() == null) { - $this->style->setWidth($imgData[0]); - $this->style->setHeight($imgData[1]); + $styleWidth = $this->style->getWidth(); + $styleHeight = $this->style->getHeight(); + list($actualWidth, $actualHeight) = $imgData; + if (!($styleWidth && $styleHeight)) { + if ($styleWidth == null && $styleHeight == null) { + $this->style->setWidth($actualWidth); + $this->style->setHeight($actualHeight); + } elseif ($styleWidth) { + $this->style->setHeight($actualHeight * ($styleWidth / $actualWidth)); + } else { + $this->style->setWidth($actualWidth * ($styleHeight / $actualHeight)); + } } $this->setImageFunctions(); } diff --git a/src/PhpWord/Reader/Word2007.php b/src/PhpWord/Reader/Word2007.php index 5901046c..9d74e540 100644 --- a/src/PhpWord/Reader/Word2007.php +++ b/src/PhpWord/Reader/Word2007.php @@ -128,7 +128,7 @@ class Word2007 extends AbstractReader implements ReaderInterface if ($zip->open($filename) === true) { for ($i = 0; $i < $zip->numFiles; $i++) { $xmlFile = $zip->getNameIndex($i); - if ((substr($xmlFile, 0, strlen($wordRelsPath))) == $wordRelsPath) { + if ((substr($xmlFile, 0, strlen($wordRelsPath))) == $wordRelsPath && (substr($xmlFile, -1)) != '/') { $docPart = str_replace('.xml.rels', '', str_replace($wordRelsPath, '', $xmlFile)); $this->rels[$docPart] = $this->getRels($filename, $xmlFile, 'word/'); } diff --git a/src/PhpWord/Shared/ZipArchive.php b/src/PhpWord/Shared/ZipArchive.php index d8519aca..21d13cfe 100644 --- a/src/PhpWord/Shared/ZipArchive.php +++ b/src/PhpWord/Shared/ZipArchive.php @@ -218,8 +218,7 @@ class ZipArchive public function getNameIndex($index) { $list = $this->zip->listContent(); - $listCount = count($list); - if ($index <= $listCount) { + if (isset($list[$index])) { return $list[$index]['filename']; } else { return false; diff --git a/src/PhpWord/Style/AbstractStyle.php b/src/PhpWord/Style/AbstractStyle.php index 45ba17a7..ea77dc70 100644 --- a/src/PhpWord/Style/AbstractStyle.php +++ b/src/PhpWord/Style/AbstractStyle.php @@ -105,13 +105,12 @@ abstract class AbstractStyle /** * Set integer value * - * @param integer|null $value + * @param mixed $value * @param integer|null $default * @return integer|null */ protected function setIntVal($value, $default = null) { - $value = intval($value); if (!is_int($value)) { $value = $default; } @@ -128,7 +127,6 @@ abstract class AbstractStyle */ protected function setFloatVal($value, $default = null) { - $value = floatval($value); if (!is_float($value)) { $value = $default; } diff --git a/src/PhpWord/Style/ListItem.php b/src/PhpWord/Style/ListItem.php index 3c762920..a4f4933d 100644 --- a/src/PhpWord/Style/ListItem.php +++ b/src/PhpWord/Style/ListItem.php @@ -100,6 +100,8 @@ class ListItem extends AbstractStyle /** * Set numbering style name + * + * @param string $value */ public function setNumStyle($value) { diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index b74211a5..de6eb3d8 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -427,25 +427,22 @@ class Base extends AbstractWriterPart $marginTop = $style->getMarginTop(); $marginLeft = $style->getMarginLeft(); $wrappingStyle = $style->getWrappingStyle(); + $w10wrapType = null; if (!$withoutP) { $xmlWriter->startElement('w:p'); - if (!is_null($align)) { $xmlWriter->startElement('w:pPr'); $xmlWriter->startElement('w:jc'); $xmlWriter->writeAttribute('w:val', $align); - $xmlWriter->endElement(); - $xmlWriter->endElement(); + $xmlWriter->endElement(); // w:jc + $xmlWriter->endElement(); // w:pPr } } $xmlWriter->startElement('w:r'); - $xmlWriter->startElement('w:pict'); - $xmlWriter->startElement('v:shape'); $xmlWriter->writeAttribute('type', '#_x0000_t75'); - $imgStyle = ''; if (null !== $width) { $imgStyle .= 'width:' . $width . 'px;'; @@ -459,33 +456,38 @@ class Base extends AbstractWriterPart if (null !== $marginLeft) { $imgStyle .= 'margin-left:' . $marginLeft . 'in;'; } - switch ($wrappingStyle) { case ImageStyle::WRAPPING_STYLE_BEHIND: $imgStyle .= 'position:absolute;z-index:-251658752;'; break; - case ImageStyle::WRAPPING_STYLE_SQUARE: + case ImageStyle::WRAPPING_STYLE_INFRONT: $imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; break; - case ImageStyle::WRAPPING_STYLE_TIGHT: - $imgStyle .= 'position:absolute;z-index:251659264;mso-wrap-edited:f;mso-position-horizontal:absolute;mso-position-vertical:absolute'; + case ImageStyle::WRAPPING_STYLE_SQUARE: + $imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; + $w10wrapType = 'square'; break; - case ImageStyle::WRAPPING_STYLE_INFRONT: - $imgStyle .= 'position:absolute;zz-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; + case ImageStyle::WRAPPING_STYLE_TIGHT: + $imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; + $w10wrapType = 'tight'; break; } - $xmlWriter->writeAttribute('style', $imgStyle); $xmlWriter->startElement('v:imagedata'); $xmlWriter->writeAttribute('r:id', 'rId' . $rId); $xmlWriter->writeAttribute('o:title', ''); - $xmlWriter->endElement(); - $xmlWriter->endElement(); + $xmlWriter->endElement(); // v:imagedata - $xmlWriter->endElement(); + if (!is_null($w10wrapType)) { + $xmlWriter->startElement('w10:wrap'); + $xmlWriter->writeAttribute('type', $w10wrapType); + $xmlWriter->endElement(); // w10:wrap + } - $xmlWriter->endElement(); + $xmlWriter->endElement(); // v:shape + $xmlWriter->endElement(); // w:pict + $xmlWriter->endElement(); // w:r if (!$withoutP) { $xmlWriter->endElement(); // w:p diff --git a/tests/PhpWord/Tests/Element/AbstractElementTest.php b/tests/PhpWord/Tests/Element/AbstractElementTest.php new file mode 100644 index 00000000..8dbfb5b3 --- /dev/null +++ b/tests/PhpWord/Tests/Element/AbstractElementTest.php @@ -0,0 +1,39 @@ +getMockForAbstractClass('\PhpOffice\PhpWord\Element\AbstractElement'); + $ival = rand(0, 100); + $stub->setElementIndex($ival); + $this->assertEquals($stub->getElementIndex(), $ival); + } + + /** + * Test set/get element unique Id + */ + public function testElementId() + { + $stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Element\AbstractElement'); + $stub->setElementId(); + $this->assertEquals(strlen($stub->getElementId()), 6); + } +} diff --git a/tests/PhpWord/Tests/Element/ImageTest.php b/tests/PhpWord/Tests/Element/ImageTest.php index 5b9aaa45..eae9fc5f 100644 --- a/tests/PhpWord/Tests/Element/ImageTest.php +++ b/tests/PhpWord/Tests/Element/ImageTest.php @@ -94,6 +94,14 @@ class ImageTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Image', $oImage->getStyle()); } + /** + * Test set wrapping style + */ + public function testStyleWrappingStyle() + { + + } + /** * Get relation Id */ @@ -121,7 +129,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase public function testPNG() { $src = __DIR__ . "/../_files/images/firefox.png"; - $oImage = new Image($src); + $oImage = new Image($src, array('width' => 100)); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $oImage); $this->assertEquals($oImage->getSource(), $src); @@ -138,7 +146,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase public function testGIF() { $src = __DIR__ . "/../_files/images/mario.gif"; - $oImage = new Image($src); + $oImage = new Image($src, array('height' => 100)); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $oImage); $this->assertEquals($oImage->getSource(), $src); @@ -202,4 +210,14 @@ class ImageTest extends \PHPUnit_Framework_TestCase { $object = new Image('test.php'); } + + /** + * Test PCX Image and Memory + * + * @expectedException \PhpOffice\PhpWord\Exception\UnsupportedImageTypeException + */ + public function testPcxImage() + { + $object = new Image('http://samples.libav.org/image-samples/RACECAR.BMP'); + } } diff --git a/tests/PhpWord/Tests/Element/TableTest.php b/tests/PhpWord/Tests/Element/TableTest.php index 0d3a74b9..3fc51f98 100644 --- a/tests/PhpWord/Tests/Element/TableTest.php +++ b/tests/PhpWord/Tests/Element/TableTest.php @@ -48,11 +48,11 @@ class TableTest extends \PHPUnit_Framework_TestCase */ public function testStyleArray() { - $oTable = new Table( - 'section', - 1, - array('borderSize' => 6, 'borderColor' => '006699', 'cellMargin' => 80) - ); + $oTable = new Table('section', 1, array( + 'borderSize' => 6, + 'borderColor' => '006699', + 'cellMargin' => 80 + )); $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Table', $oTable->getStyle()); } @@ -63,7 +63,7 @@ class TableTest extends \PHPUnit_Framework_TestCase public function testWidth() { $oTable = new Table('section', 1); - $iVal = rand(1, 1000); + $iVal = rand(1, 1000); $oTable->setWidth($iVal); $this->assertEquals($oTable->getWidth(), $iVal); } @@ -73,7 +73,7 @@ class TableTest extends \PHPUnit_Framework_TestCase */ public function testRow() { - $oTable = new Table('section', 1); + $oTable = new Table('section', 1); $element = $oTable->addRow(); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Row', $element); $this->assertCount(1, $oTable->getRows()); @@ -89,4 +89,18 @@ class TableTest extends \PHPUnit_Framework_TestCase $element = $oTable->addCell(); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Cell', $element); } + + /** + * Add cell + */ + public function testCountColumns() + { + $oTable = new Table('section', 1); + $oTable->addRow(); + $element = $oTable->addCell(); + $this->assertEquals($oTable->countColumns(), 1); + $element = $oTable->addCell(); + $element = $oTable->addCell(); + $this->assertEquals($oTable->countColumns(), 3); + } } diff --git a/tests/PhpWord/Tests/Shared/XMLReaderTest.php b/tests/PhpWord/Tests/Shared/XMLReaderTest.php new file mode 100644 index 00000000..759bf580 --- /dev/null +++ b/tests/PhpWord/Tests/Shared/XMLReaderTest.php @@ -0,0 +1,54 @@ +assertFalse($object->getDomFromZip($filename, 'yadayadaya')); + } + + /** + * Test get elements returns empty + */ + public function testGetElementsReturnsEmpty() + { + $object = new XMLReader(); + $this->assertEquals(array(), $object->getElements('w:document')); + } + + /** + * Test get element returns null + */ + public function testGetElementReturnsNull() + { + $filename = __DIR__ . "/../_files/documents/reader.docx.zip"; + + $object = new XMLReader(); + $object->getDomFromZip($filename, '[Content_Types].xml'); + $element = $object->getElements('*')->item(0); + + $this->assertNull($object->getElement('yadayadaya', $element)); + } +} diff --git a/tests/PhpWord/Tests/Shared/ZipArchiveTest.php b/tests/PhpWord/Tests/Shared/ZipArchiveTest.php index ba47ade1..40bdde68 100644 --- a/tests/PhpWord/Tests/Shared/ZipArchiveTest.php +++ b/tests/PhpWord/Tests/Shared/ZipArchiveTest.php @@ -24,14 +24,51 @@ class ZipArchiveTest extends \PHPUnit_Framework_TestCase public function testAdd() { $existingFile = __DIR__ . "/../_files/documents/sheet.xls"; - $zipFile = __DIR__ . "/../_files/documents/ziptest.zip"; - $object = new ZipArchive(); + $zipFile = __DIR__ . "/../_files/documents/ziptest.zip"; + $object = new ZipArchive(); $object->open($zipFile); $object->addFile($existingFile, 'xls/new.xls'); $object->addFromString('content/string.txt', 'Test'); $this->assertTrue($object->locateName('xls/new.xls')); $this->assertEquals('Test', $object->getFromName('content/string.txt')); + $this->assertEquals('Test', $object->getFromName('/content/string.txt')); + + unlink($zipFile); + } + + /** + * Test find if a given name exists in the archive + */ + public function testLocate() + { + $existingFile = __DIR__ . "/../_files/documents/sheet.xls"; + $zipFile = __DIR__ . "/../_files/documents/ziptest.zip"; + $object = new ZipArchive(); + $object->open($zipFile); + $object->addFile($existingFile, 'xls/new.xls'); + $object->addFromString('content/string.txt', 'Test'); + + $this->assertEquals(1, $object->locateName('content/string.txt')); + $this->assertFalse($object->locateName('blablabla')); + + unlink($zipFile); + } + + /** + * Test returns the name of an entry using its index + */ + public function testNameIndex() + { + $existingFile = __DIR__ . "/../_files/documents/sheet.xls"; + $zipFile = __DIR__ . "/../_files/documents/ziptest.zip"; + $object = new ZipArchive(); + $object->open($zipFile); + $object->addFile($existingFile, 'xls/new.xls'); + $object->addFromString('content/string.txt', 'Test'); + + $this->assertFalse($object->getNameIndex(-1)); + $this->assertEquals('content/string.txt', $object->getNameIndex(1)); unlink($zipFile); } diff --git a/tests/PhpWord/Tests/Style/AbstractStyleTest.php b/tests/PhpWord/Tests/Style/AbstractStyleTest.php new file mode 100644 index 00000000..a9eace9d --- /dev/null +++ b/tests/PhpWord/Tests/Style/AbstractStyleTest.php @@ -0,0 +1,66 @@ +getMockForAbstractClass('\PhpOffice\PhpWord\Style\AbstractStyle'); + $stub->setStyleByArray(array('index' => 1)); + + $this->assertEquals(1, $stub->getIndex()); + } + + /** + * Test setBoolVal, setIntVal, setFloatVal, setEnumVal with normal value + */ + public function testSetValNormal() + { + $stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Style\AbstractStyle'); + + $this->assertEquals(true, self::callProtectedMethod($stub, 'setBoolVal', array(true, false))); + $this->assertEquals(12, self::callProtectedMethod($stub, 'setIntVal', array(12, 200))); + $this->assertEquals(871.1, self::callProtectedMethod($stub, 'setFloatVal', array(871.1, 2.1))); + $this->assertEquals('a', self::callProtectedMethod($stub, 'setEnumVal', array('a', array('a', 'b'), 'b'))); + } + + /** + * Test setBoolVal, setIntVal, setFloatVal, setEnumVal with default value + */ + public function testSetValDefault() + { + $stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Style\AbstractStyle'); + + $this->assertEquals(false, self::callProtectedMethod($stub, 'setBoolVal', array('a', false))); + $this->assertEquals(200, self::callProtectedMethod($stub, 'setIntVal', array('foo', 200))); + $this->assertEquals(2.1, self::callProtectedMethod($stub, 'setFloatVal', array('foo', 2.1))); + $this->assertEquals('b', self::callProtectedMethod($stub, 'setEnumVal', array('z', array('a', 'b'), 'b'))); + } + + /** + * Helper function to call protected method + */ + public static function callProtectedMethod($object, $method, array $args = array()) + { + $class = new \ReflectionClass(get_class($object)); + $method = $class->getMethod($method); + $method->setAccessible(true); + return $method->invokeArgs($object, $args); + } +} diff --git a/tests/PhpWord/Tests/Style/ImageTest.php b/tests/PhpWord/Tests/Style/ImageTest.php index b35c8cb2..fd74d73c 100644 --- a/tests/PhpWord/Tests/Style/ImageTest.php +++ b/tests/PhpWord/Tests/Style/ImageTest.php @@ -32,7 +32,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase 'align' => 'left', 'marginTop' => 240, 'marginLeft' => 240, - 'wrappingStyle' => 'inline', + 'wrappingStyle' => 'inline' ); foreach ($properties as $key => $value) { $set = "set{$key}"; @@ -54,7 +54,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase 'height' => 200, 'align' => 'left', 'marginTop' => 240, - 'marginLeft' => 240, + 'marginLeft' => 240 ); foreach ($properties as $key => $value) { $get = "get{$key}"; diff --git a/tests/PhpWord/Tests/Style/ListItemTest.php b/tests/PhpWord/Tests/Style/ListItemTest.php index 0fb67da3..6eef720c 100644 --- a/tests/PhpWord/Tests/Style/ListItemTest.php +++ b/tests/PhpWord/Tests/Style/ListItemTest.php @@ -53,4 +53,16 @@ class ListItemTest extends \PHPUnit_Framework_TestCase $object->setListType($value); $this->assertEquals($value, $object->getListType()); } + + /** + * Test set/get numbering style name + */ + public function testSetGetNumStyle() + { + $expected = 'List Name'; + + $object = new ListItem(); + $object->setNumStyle($expected); + $this->assertEquals($expected, $object->getNumStyle()); + } } diff --git a/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php b/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php index d9d1b3c5..904f45bd 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php @@ -28,7 +28,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase } /** - * covers ::_writeText + * Test write text element */ public function testWriteText() { @@ -49,7 +49,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase } /** - * covers ::_writeTextRun + * Test write textrun element */ public function testWriteTextRun() { @@ -74,7 +74,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase } /** - * covers ::_writeLink + * Test write link element */ public function testWriteLink() { @@ -97,7 +97,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase } /** - * covers ::_writePreserveText + * Test write preserve text element */ public function testWritePreserveText() { @@ -121,7 +121,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase } /** - * covers ::_writeTextBreak + * Test write text break */ public function testWriteTextBreak() { @@ -146,30 +146,93 @@ class BaseTest extends \PHPUnit_Framework_TestCase } /** - * covers ::_writeParagraphStyle + * covers ::_writeImage */ - public function testWriteParagraphStyleAlign() + public function testWriteImage() { $phpWord = new PhpWord(); + $styles = array('align' => 'left', 'width' => 40, 'height' => 40, 'marginTop' => -1, 'marginLeft' => -1); + $wraps = array('inline', 'behind', 'infront', 'square', 'tight'); $section = $phpWord->addSection(); - - $section->addText('This is my text', null, array('align' => 'right')); + foreach ($wraps as $wrap) { + $styles['wrappingStyle'] = $wrap; + $section->addImage(__DIR__ . "/../../_files/images/earth.jpg", $styles); + } $doc = TestHelperDOCX::getDocument($phpWord); - $element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:jc'); - $this->assertEquals('right', $element->getAttribute('w:val')); + // behind + $element = $doc->getElement('/w:document/w:body/w:p[2]/w:r/w:pict/v:shape'); + $style = $element->getAttribute('style'); + $this->assertRegExp('/z\-index:\-[0-9]*/', $style); + + // square + $element = $doc->getElement('/w:document/w:body/w:p[4]/w:r/w:pict/v:shape/w10:wrap'); + $this->assertEquals('square', $element->getAttribute('type')); + } + + /** + * covers ::_writeWatermark + */ + public function testWriteWatermark() + { + $imageSrc = __DIR__ . "/../../_files/images/earth.jpg"; + + $phpWord = new PhpWord(); + $section = $phpWord->addSection(); + $header = $section->addHeader(); + $header->addWatermark($imageSrc); + $doc = TestHelperDOCX::getDocument($phpWord); + + $element = $doc->getElement("/w:document/w:body/w:sectPr/w:headerReference"); + $this->assertStringStartsWith("rId", $element->getAttribute('r:id')); + } + + /** + * covers ::_writeTitle + */ + public function testWriteTitle() + { + $phpWord = new PhpWord(); + $phpWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240)); + $phpWord->addSection()->addTitle('Test', 1); + $doc = TestHelperDOCX::getDocument($phpWord); + + $element = "/w:document/w:body/w:p/w:pPr/w:pStyle"; + $this->assertEquals('Heading1', $doc->getElementAttribute($element, 'w:val')); + $element = "/w:document/w:body/w:p/w:r/w:fldChar"; + $this->assertEquals('end', $doc->getElementAttribute($element, 'w:fldCharType')); + } + + /** + * covers ::_writeCheckbox + */ + public function testWriteCheckbox() + { + $rStyle = 'rStyle'; + $pStyle = 'pStyle'; + + $phpWord = new PhpWord(); + $phpWord->addFontStyle($rStyle, array('bold' => true)); + $phpWord->addParagraphStyle($pStyle, array('hanging' => 120, 'indent' => 120)); + $section = $phpWord->addSection(); + $section->addCheckbox('Check1', 'Test', $rStyle, $pStyle); + $doc = TestHelperDOCX::getDocument($phpWord); + + $element = '/w:document/w:body/w:p/w:r/w:fldChar/w:ffData/w:name'; + $this->assertEquals('Check1', $doc->getElementAttribute($element, 'w:val')); } /** * covers ::_writeParagraphStyle */ - public function testWriteParagraphStylePagination() + public function testWriteParagraphStyle() { // Create the doc $phpWord = new PhpWord(); $section = $phpWord->addSection(); $attributes = array( + 'align' => 'right', 'widowControl' => false, 'keepNext' => true, 'keepLines' => true, @@ -184,10 +247,13 @@ class BaseTest extends \PHPUnit_Framework_TestCase $i = 0; foreach ($attributes as $key => $value) { $i++; - $path = "/w:document/w:body/w:p[{$i}]/w:pPr/w:{$key}"; + $nodeName = ($key == 'align') ? 'jc' : $key; + $path = "/w:document/w:body/w:p[{$i}]/w:pPr/w:{$nodeName}"; + if ($key != 'align') { + $value = $value ? 1 : 0; + } $element = $doc->getElement($path); - $expected = $value ? 1 : 0; - $this->assertEquals($expected, $element->getAttribute('w:val')); + $this->assertEquals($value, $element->getAttribute('w:val')); } } @@ -316,81 +382,4 @@ class BaseTest extends \PHPUnit_Framework_TestCase $this->assertEquals(5, $element->getAttribute('w:val')); } - - /** - * covers ::_writeImage - */ - public function testWriteImagePosition() - { - $phpWord = new PhpWord(); - $section = $phpWord->addSection(); - $section->addImage( - __DIR__ . "/../../_files/images/earth.jpg", - array( - 'marginTop' => -1, - 'marginLeft' => -1, - 'wrappingStyle' => 'behind' - ) - ); - - $doc = TestHelperDOCX::getDocument($phpWord); - $element = $doc->getElement('/w:document/w:body/w:p/w:r/w:pict/v:shape'); - - $style = $element->getAttribute('style'); - - $this->assertRegExp('/z\-index:\-[0-9]*/', $style); - $this->assertRegExp('/position:absolute;/', $style); - } - - /** - * covers ::_writeWatermark - */ - public function testWriteWatermark() - { - $imageSrc = __DIR__ . "/../../_files/images/earth.jpg"; - - $phpWord = new PhpWord(); - $section = $phpWord->addSection(); - $header = $section->addHeader(); - $header->addWatermark($imageSrc); - $doc = TestHelperDOCX::getDocument($phpWord); - - $element = $doc->getElement("/w:document/w:body/w:sectPr/w:headerReference"); - $this->assertStringStartsWith("rId", $element->getAttribute('r:id')); - } - - /** - * covers ::_writeTitle - */ - public function testWriteTitle() - { - $phpWord = new PhpWord(); - $phpWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240)); - $phpWord->addSection()->addTitle('Test', 1); - $doc = TestHelperDOCX::getDocument($phpWord); - - $element = "/w:document/w:body/w:p/w:pPr/w:pStyle"; - $this->assertEquals('Heading1', $doc->getElementAttribute($element, 'w:val')); - $element = "/w:document/w:body/w:p/w:r/w:fldChar"; - $this->assertEquals('end', $doc->getElementAttribute($element, 'w:fldCharType')); - } - - /** - * covers ::_writeCheckbox - */ - public function testWriteCheckbox() - { - $rStyle = 'rStyle'; - $pStyle = 'pStyle'; - - $phpWord = new PhpWord(); - $phpWord->addFontStyle($rStyle, array('bold' => true)); - $phpWord->addParagraphStyle($pStyle, array('hanging' => 120, 'indent' => 120)); - $section = $phpWord->addSection(); - $section->addCheckbox('Check1', 'Test', $rStyle, $pStyle); - $doc = TestHelperDOCX::getDocument($phpWord); - - $element = '/w:document/w:body/w:p/w:r/w:fldChar/w:ffData/w:name'; - $this->assertEquals('Check1', $doc->getElementAttribute($element, 'w:val')); - } } diff --git a/tests/PhpWord/Tests/_files/documents/reader.docx b/tests/PhpWord/Tests/_files/documents/reader.docx index 2143c628..5f37ef4b 100644 Binary files a/tests/PhpWord/Tests/_files/documents/reader.docx and b/tests/PhpWord/Tests/_files/documents/reader.docx differ