From 7abd5be62b076e4b30cf30dd182b0c7a1468a334 Mon Sep 17 00:00:00 2001 From: Gabriel Bull Date: Sun, 2 Mar 2014 14:40:58 -0500 Subject: [PATCH 1/3] Reformatted Tests to PSR-2 coding standards --- Classes/PHPWord/Style/Font.php | 29 +++--- Classes/PHPWord/Style/Paragraph.php | 16 +-- Tests/PHPWord/IOFactoryTest.php | 94 +++++++++--------- Tests/PHPWord/MediaTest.php | 39 ++++---- Tests/PHPWord/SectionTest.php | 56 +++++------ Tests/PHPWord/Shared/FontTest.php | 4 +- Tests/PHPWord/Style/CellTest.php | 6 +- Tests/PHPWord/Style/FontTest.php | 4 +- Tests/PHPWord/Style/ParagraphTest.php | 4 +- Tests/PHPWord/TemplateTest.php | 12 +-- Tests/PHPWord/Writer/Word2007/BaseTest.php | 98 +++++++++---------- .../PHPWord/Writer/Word2007/DocumentTest.php | 38 +++---- Tests/PHPWord/Writer/Word2007/StylesTest.php | 3 +- Tests/_inc/TestHelperDOCX.php | 2 +- Tests/bootstrap.php | 4 +- 15 files changed, 200 insertions(+), 209 deletions(-) diff --git a/Classes/PHPWord/Style/Font.php b/Classes/PHPWord/Style/Font.php index 43495cdc2f..987553c244 100755 --- a/Classes/PHPWord/Style/Font.php +++ b/Classes/PHPWord/Style/Font.php @@ -30,7 +30,6 @@ */ class PHPWord_Style_Font { - const UNDERLINE_NONE = 'none'; const UNDERLINE_DASH = 'dash'; const UNDERLINE_DASHHEAVY = 'dashHeavy'; @@ -153,8 +152,8 @@ class PHPWord_Style_Font /** * New font style * - * @param string $type Type of font - * @param array $styleParagraph Paragraph styles definition + * @param string $type Type of font + * @param array $styleParagraph Paragraph styles definition */ public function __construct($type = 'text', $styleParagraph = null) { @@ -187,8 +186,8 @@ public function __construct($type = 'text', $styleParagraph = null) /** * Set style value * - * @param string $key - * @param mixed $value + * @param string $key + * @param mixed $value */ public function setStyleValue($key, $value) { @@ -211,7 +210,7 @@ public function getName() /** * Set font name * - * @param string $pValue + * @param string $pValue * @return PHPWord_Style_Font */ public function setName($pValue = PHPWord::DEFAULT_FONT_NAME) @@ -236,7 +235,7 @@ public function getSize() /** * Set font size * - * @param int|float $pValue + * @param int|float $pValue * @return PHPWord_Style_Font */ public function setSize($pValue = PHPWord::DEFAULT_FONT_SIZE) @@ -261,7 +260,7 @@ public function getBold() /** * Set bold * - * @param bool $pValue + * @param bool $pValue * @return PHPWord_Style_Font */ public function setBold($pValue = false) @@ -286,7 +285,7 @@ public function getItalic() /** * Set italics * - * @param bool $pValue + * @param bool $pValue * @return PHPWord_Style_Font */ public function setItalic($pValue = false) @@ -311,7 +310,7 @@ public function getSuperScript() /** * Set superscript * - * @param bool $pValue + * @param bool $pValue * @return PHPWord_Style_Font */ public function setSuperScript($pValue = false) @@ -337,7 +336,7 @@ public function getSubScript() /** * Set subscript * - * @param bool $pValue + * @param bool $pValue * @return PHPWord_Style_Font */ public function setSubScript($pValue = false) @@ -363,7 +362,7 @@ public function getUnderline() /** * Set underline * - * @param string $pValue + * @param string $pValue * @return PHPWord_Style_Font */ public function setUnderline($pValue = PHPWord_Style_Font::UNDERLINE_NONE) @@ -388,7 +387,7 @@ public function getStrikethrough() /** * Set strikethrough * - * @param bool $pValue + * @param bool $pValue * @return PHPWord_Style_Font */ public function setStrikethrough($pValue = false) @@ -413,7 +412,7 @@ public function getColor() /** * Set font color * - * @param string $pValue + * @param string $pValue * @return PHPWord_Style_Font */ public function setColor($pValue = PHPWord::DEFAULT_FONT_COLOR) @@ -438,7 +437,7 @@ public function getFgColor() /** * Set foreground/highlight color * - * @param string $pValue + * @param string $pValue * @return PHPWord_Style_Font */ public function setFgColor($pValue = null) diff --git a/Classes/PHPWord/Style/Paragraph.php b/Classes/PHPWord/Style/Paragraph.php index c7104e8e15..c64d6346ac 100755 --- a/Classes/PHPWord/Style/Paragraph.php +++ b/Classes/PHPWord/Style/Paragraph.php @@ -145,8 +145,8 @@ public function __construct() /** * Set Style value * - * @param string $key - * @param mixed $value + * @param string $key + * @param mixed $value */ public function setStyleValue($key, $value) { @@ -335,7 +335,7 @@ public function getBasedOn() /** * Set parent style ID * - * @param string $pValue + * @param string $pValue * @return PHPWord_Style_Paragraph */ public function setBasedOn($pValue = 'Normal') @@ -357,7 +357,7 @@ public function getNext() /** * Set style for next paragraph * - * @param string $pValue + * @param string $pValue * @return PHPWord_Style_Paragraph */ public function setNext($pValue = null) @@ -379,7 +379,7 @@ public function getWidowControl() /** * Set keep paragraph with next paragraph setting * - * @param bool $pValue + * @param bool $pValue * @return PHPWord_Style_Paragraph */ public function setWidowControl($pValue = true) @@ -404,7 +404,7 @@ public function getKeepNext() /** * Set keep paragraph with next paragraph setting * - * @param bool $pValue + * @param bool $pValue * @return PHPWord_Style_Paragraph */ public function setKeepNext($pValue = false) @@ -429,7 +429,7 @@ public function getKeepLines() /** * Set keep all lines on one page setting * - * @param bool $pValue + * @param bool $pValue * @return PHPWord_Style_Paragraph */ public function setKeepLines($pValue = false) @@ -454,7 +454,7 @@ public function getPageBreakBefore() /** * Set start paragraph on next page setting * - * @param bool $pValue + * @param bool $pValue * @return PHPWord_Style_Paragraph */ public function setPageBreakBefore($pValue = false) diff --git a/Tests/PHPWord/IOFactoryTest.php b/Tests/PHPWord/IOFactoryTest.php index f6f0aba8ce..9bee51a3e7 100644 --- a/Tests/PHPWord/IOFactoryTest.php +++ b/Tests/PHPWord/IOFactoryTest.php @@ -11,49 +11,51 @@ * @package PHPWord\Tests * @runTestsInSeparateProcesses */ -class PHPWord_IOFactoryTest extends \PHPUnit_Framework_TestCase { - public function testGetSearchLocations() - { - $this->assertAttributeEquals(PHPWord_IOFactory::getSearchLocations(), '_searchLocations','PHPWord_IOFactory'); - } - - public function testSetSearchLocationsWithArray() - { - PHPWord_IOFactory::setSearchLocations(array()); - $this->assertAttributeEquals(array(), '_searchLocations','PHPWord_IOFactory'); - } - - /** - * @expectedException Exception - * @expectedExceptionMessage Invalid parameter passed. - */ - public function testSetSearchLocationsWithNotArray() - { - PHPWord_IOFactory::setSearchLocations('String'); - } - - public function testAddSearchLocation() - { - PHPWord_IOFactory::setSearchLocations(array()); - PHPWord_IOFactory::addSearchLocation('type', 'location', 'classname'); - $this->assertAttributeEquals(array(array('type' => 'type', 'path' => 'location', 'class' => 'classname')), '_searchLocations','PHPWord_IOFactory'); - } - - /** - * @expectedException Exception - * @expectedExceptionMessage No IWriter found for type - */ - public function testCreateWriterException(){ - $oPHPWord = new PHPWord(); - - PHPWord_IOFactory::setSearchLocations(array()); - PHPWord_IOFactory::createWriter($oPHPWord); - } - - public function testCreateWriter(){ - $oPHPWord = new PHPWord(); - - $this->assertEquals(PHPWord_IOFactory::createWriter($oPHPWord, 'Word2007'), new PHPWord_Writer_Word2007($oPHPWord)); - } -} - \ No newline at end of file +class PHPWord_IOFactoryTest extends \PHPUnit_Framework_TestCase +{ + public function testGetSearchLocations() + { + $this->assertAttributeEquals(PHPWord_IOFactory::getSearchLocations(), '_searchLocations', 'PHPWord_IOFactory'); + } + + public function testSetSearchLocationsWithArray() + { + PHPWord_IOFactory::setSearchLocations(array()); + $this->assertAttributeEquals(array(), '_searchLocations', 'PHPWord_IOFactory'); + } + + /** + * @expectedException Exception + * @expectedExceptionMessage Invalid parameter passed. + */ + public function testSetSearchLocationsWithNotArray() + { + PHPWord_IOFactory::setSearchLocations('String'); + } + + public function testAddSearchLocation() + { + PHPWord_IOFactory::setSearchLocations(array()); + PHPWord_IOFactory::addSearchLocation('type', 'location', 'classname'); + $this->assertAttributeEquals(array(array('type' => 'type', 'path' => 'location', 'class' => 'classname')), '_searchLocations', 'PHPWord_IOFactory'); + } + + /** + * @expectedException Exception + * @expectedExceptionMessage No IWriter found for type + */ + public function testCreateWriterException() + { + $oPHPWord = new PHPWord(); + + PHPWord_IOFactory::setSearchLocations(array()); + PHPWord_IOFactory::createWriter($oPHPWord); + } + + public function testCreateWriter() + { + $oPHPWord = new PHPWord(); + + $this->assertEquals(PHPWord_IOFactory::createWriter($oPHPWord, 'Word2007'), new PHPWord_Writer_Word2007($oPHPWord)); + } +} \ No newline at end of file diff --git a/Tests/PHPWord/MediaTest.php b/Tests/PHPWord/MediaTest.php index 40460deace..36438e65ec 100644 --- a/Tests/PHPWord/MediaTest.php +++ b/Tests/PHPWord/MediaTest.php @@ -4,26 +4,25 @@ use PHPUnit_Framework_TestCase; use PHPWord_Media; -class PHPWord_MediaTest extends \PHPUnit_Framework_TestCase { +class PHPWord_MediaTest extends \PHPUnit_Framework_TestCase +{ + public function testGetSectionMediaElementsWithNull() + { + $this->assertEquals(PHPWord_Media::getSectionMediaElements(), array()); + } - public function testGetSectionMediaElementsWithNull() - { - $this->assertEquals(PHPWord_Media::getSectionMediaElements(), array()); - } + public function testCountSectionMediaElementsWithNull() + { + $this->assertEquals(PHPWord_Media::countSectionMediaElements(), 0); + } - public function testCountSectionMediaElementsWithNull() - { - $this->assertEquals(PHPWord_Media::countSectionMediaElements(), 0); - } + public function testGetHeaderMediaElements() + { + $this->assertAttributeEquals(PHPWord_Media::getHeaderMediaElements(), '_headerMedia', 'PHPWord_Media'); + } - public function testGetHeaderMediaElements() - { - $this->assertAttributeEquals(PHPWord_Media::getHeaderMediaElements(), '_headerMedia','PHPWord_Media'); - } - - public function testGetFooterMediaElements() - { - $this->assertAttributeEquals(PHPWord_Media::getFooterMediaElements(), '_footerMedia','PHPWord_Media'); - } -} - \ No newline at end of file + public function testGetFooterMediaElements() + { + $this->assertAttributeEquals(PHPWord_Media::getFooterMediaElements(), '_footerMedia', 'PHPWord_Media'); + } +} \ No newline at end of file diff --git a/Tests/PHPWord/SectionTest.php b/Tests/PHPWord/SectionTest.php index 0e90961de1..dd5a5b57ad 100644 --- a/Tests/PHPWord/SectionTest.php +++ b/Tests/PHPWord/SectionTest.php @@ -4,35 +4,35 @@ use PHPUnit_Framework_TestCase; use PHPWord_Section; -class PHPWord_SectionTest extends \PHPUnit_Framework_TestCase { - public function testGetSettings() - { - $oSection = new PHPWord_Section(0); - $this->assertAttributeEquals($oSection->getSettings(), '_settings', new PHPWord_Section(0)); - } +class PHPWord_SectionTest extends \PHPUnit_Framework_TestCase +{ + public function testGetSettings() + { + $oSection = new PHPWord_Section(0); + $this->assertAttributeEquals($oSection->getSettings(), '_settings', new PHPWord_Section(0)); + } - public function testGetElementss() - { - $oSection = new PHPWord_Section(0); - $this->assertAttributeEquals($oSection->getElements(), '_elementCollection',new PHPWord_Section(0)); - } + public function testGetElementss() + { + $oSection = new PHPWord_Section(0); + $this->assertAttributeEquals($oSection->getElements(), '_elementCollection', new PHPWord_Section(0)); + } - public function testGetFooter() - { - $oSection = new PHPWord_Section(0); - $this->assertAttributeEquals($oSection->getFooter(), '_footer',new PHPWord_Section(0)); - } + public function testGetFooter() + { + $oSection = new PHPWord_Section(0); + $this->assertAttributeEquals($oSection->getFooter(), '_footer', new PHPWord_Section(0)); + } - public function testGetHeaders() - { - $oSection = new PHPWord_Section(0); - $this->assertAttributeEquals($oSection->getHeaders(), '_headers',new PHPWord_Section(0)); - } + public function testGetHeaders() + { + $oSection = new PHPWord_Section(0); + $this->assertAttributeEquals($oSection->getHeaders(), '_headers', new PHPWord_Section(0)); + } - public function testGetElements() - { - $oSection = new PHPWord_Section(0); - $this->assertAttributeEquals($oSection->getElements(), '_elementCollection',new PHPWord_Section(0)); - } -} - \ No newline at end of file + public function testGetElements() + { + $oSection = new PHPWord_Section(0); + $this->assertAttributeEquals($oSection->getElements(), '_elementCollection', new PHPWord_Section(0)); + } +} \ No newline at end of file diff --git a/Tests/PHPWord/Shared/FontTest.php b/Tests/PHPWord/Shared/FontTest.php index 09603d5fa0..cb18b06f0d 100644 --- a/Tests/PHPWord/Shared/FontTest.php +++ b/Tests/PHPWord/Shared/FontTest.php @@ -13,7 +13,6 @@ */ class PHPWord_Writer_Shared_FontTest extends \PHPUnit_Framework_TestCase { - /** * Test various conversions */ @@ -44,5 +43,4 @@ public function testConversions() $result = PHPWord_Shared_Font::pointSizeToTwips($original); $this->assertEquals($original * 20, $result); } - -} +} \ No newline at end of file diff --git a/Tests/PHPWord/Style/CellTest.php b/Tests/PHPWord/Style/CellTest.php index 4db86f3580..6a461211dd 100644 --- a/Tests/PHPWord/Style/CellTest.php +++ b/Tests/PHPWord/Style/CellTest.php @@ -12,7 +12,6 @@ */ class PHPWord_Style_CellTest extends \PHPUnit_Framework_TestCase { - /** * Test setting style with normal value */ @@ -35,7 +34,7 @@ public function testSetGetNormal() 'gridSpan' => 2, 'vMerge' => 2, ); - //'defaultBorderColor' => null, + //'defaultBorderColor' => null, foreach ($attributes as $key => $value) { $set = "set{$key}"; $get = "get{$key}"; @@ -76,5 +75,4 @@ public function testBorderSize() $object->setStyleValue('_borderSize', $value); $this->assertEquals($expected, $object->getBorderSize()); } - -} +} \ No newline at end of file diff --git a/Tests/PHPWord/Style/FontTest.php b/Tests/PHPWord/Style/FontTest.php index a3fa2086ab..366391ce74 100644 --- a/Tests/PHPWord/Style/FontTest.php +++ b/Tests/PHPWord/Style/FontTest.php @@ -13,7 +13,6 @@ */ class PHPWord_Style_FontTest extends \PHPUnit_Framework_TestCase { - /** * Test initiation for style type and paragraph style */ @@ -78,5 +77,4 @@ public function testSetStyleValueNormal() $this->assertEquals($value, $object->$get()); } } - -} +} \ No newline at end of file diff --git a/Tests/PHPWord/Style/ParagraphTest.php b/Tests/PHPWord/Style/ParagraphTest.php index 23d0b713c8..309cb9758b 100644 --- a/Tests/PHPWord/Style/ParagraphTest.php +++ b/Tests/PHPWord/Style/ParagraphTest.php @@ -13,7 +13,6 @@ */ class PHPWord_Style_ParagraphTest extends \PHPUnit_Framework_TestCase { - /** * Test setting style values with null or empty value */ @@ -86,5 +85,4 @@ public function testTabs() )); $this->assertInstanceOf('PHPWord_Style_Tabs', $object->getTabs()); } - -} +} \ No newline at end of file diff --git a/Tests/PHPWord/TemplateTest.php b/Tests/PHPWord/TemplateTest.php index 1cc30639a9..1b2fd57fb8 100644 --- a/Tests/PHPWord/TemplateTest.php +++ b/Tests/PHPWord/TemplateTest.php @@ -20,7 +20,7 @@ final public function testXslStyleSheetCanBeApplied() array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'with_table_macros.docx') ) ); - + $xslDOMDocument = new \DOMDocument(); $xslDOMDocument->load( \join( @@ -28,31 +28,31 @@ final public function testXslStyleSheetCanBeApplied() array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'xsl', 'remove_tables_by_needle.xsl') ) ); - + foreach (array('${employee.', '${scoreboard.') as $needle) { $template->applyXslStyleSheet($xslDOMDocument, array('needle' => $needle)); } - + $actualDocument = $template->save(); $expectedDocument = \join( \DIRECTORY_SEPARATOR, array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'documents', 'without_table_macros.docx') ); - + $actualZip = new \ZipArchive(); $actualZip->open($actualDocument); $actualXml = $actualZip->getFromName('word/document.xml'); if ($actualZip->close() === false) { throw new \Exception('Could not close zip file "' . $actualDocument . '".'); } - + $expectedZip = new \ZipArchive(); $expectedZip->open($expectedDocument); $expectedXml = $expectedZip->getFromName('word/document.xml'); if ($expectedZip->close() === false) { throw new \Exception('Could not close zip file "' . $expectedDocument . '".'); } - + $this->assertXmlStringEqualsXmlString($expectedXml, $actualXml); } diff --git a/Tests/PHPWord/Writer/Word2007/BaseTest.php b/Tests/PHPWord/Writer/Word2007/BaseTest.php index 95ca94e159..67b53b042a 100644 --- a/Tests/PHPWord/Writer/Word2007/BaseTest.php +++ b/Tests/PHPWord/Writer/Word2007/BaseTest.php @@ -11,52 +11,53 @@ * @package PHPWord\Tests * @runTestsInSeparateProcesses */ -class PHPWord_Writer_Word2007_BaseTest extends \PHPUnit_Framework_TestCase { - /** - * Executed before each method of the class - */ - public function tearDown() - { - TestHelperDOCX::clear(); - } - - public function testWriteImage_Position() - { - $PHPWord = new PHPWord(); - $section = $PHPWord->createSection(); - $section->addImage( - PHPWORD_TESTS_DIR_ROOT . '/_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); - } - - public function testWriteParagraphStyle_Align() - { - $PHPWord = new PHPWord(); - $section = $PHPWord->createSection(); - - $section->addText('This is my text', null, array('align' => 'right')); - - $doc = TestHelperDOCX::getDocument($PHPWord); - $element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:jc'); - - $this->assertEquals('right', $element->getAttribute('w:val')); - } - - public function testWriteCellStyle_CellGridSpan() - { +class PHPWord_Writer_Word2007_BaseTest extends \PHPUnit_Framework_TestCase +{ + /** + * Executed before each method of the class + */ + public function tearDown() + { + TestHelperDOCX::clear(); + } + + public function testWriteImage_Position() + { + $PHPWord = new PHPWord(); + $section = $PHPWord->createSection(); + $section->addImage( + PHPWORD_TESTS_DIR_ROOT . '/_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); + } + + public function testWriteParagraphStyle_Align() + { + $PHPWord = new PHPWord(); + $section = $PHPWord->createSection(); + + $section->addText('This is my text', null, array('align' => 'right')); + + $doc = TestHelperDOCX::getDocument($PHPWord); + $element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:jc'); + + $this->assertEquals('right', $element->getAttribute('w:val')); + } + + public function testWriteCellStyle_CellGridSpan() + { $PHPWord = new PHPWord(); $section = $PHPWord->createSection(); @@ -77,7 +78,7 @@ public function testWriteCellStyle_CellGridSpan() $element = $doc->getElement('/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:gridSpan'); $this->assertEquals(5, $element->getAttribute('w:val')); - } + } /** * Test write paragraph pagination @@ -108,5 +109,4 @@ public function testWriteParagraphStyle_Pagination() $this->assertEquals($expected, $element->getAttribute('w:val')); } } - -} +} \ No newline at end of file diff --git a/Tests/PHPWord/Writer/Word2007/DocumentTest.php b/Tests/PHPWord/Writer/Word2007/DocumentTest.php index 13d233cce9..b21a559be5 100644 --- a/Tests/PHPWord/Writer/Word2007/DocumentTest.php +++ b/Tests/PHPWord/Writer/Word2007/DocumentTest.php @@ -11,25 +11,25 @@ * @package PHPWord\Tests * @runTestsInSeparateProcesses */ -class PHPWord_Writer_Word2007_DocumentTest extends \PHPUnit_Framework_TestCase { - /** - * Executed before each method of the class - */ - public function tearDown() - { - TestHelperDOCX::clear(); - } +class PHPWord_Writer_Word2007_DocumentTest extends \PHPUnit_Framework_TestCase +{ + /** + * Executed before each method of the class + */ + public function tearDown() + { + TestHelperDOCX::clear(); + } - public function testWriteEndSection_PageNumbering() - { - $PHPWord = new PHPWord(); - $section = $PHPWord->createSection(); - $section->getSettings()->setPageNumberingStart(2); + public function testWriteEndSection_PageNumbering() + { + $PHPWord = new PHPWord(); + $section = $PHPWord->createSection(); + $section->getSettings()->setPageNumberingStart(2); - $doc = TestHelperDOCX::getDocument($PHPWord); - $element = $doc->getElement('/w:document/w:body/w:sectPr/w:pgNumType'); + $doc = TestHelperDOCX::getDocument($PHPWord); + $element = $doc->getElement('/w:document/w:body/w:sectPr/w:pgNumType'); - $this->assertEquals(2, $element->getAttribute('w:start')); - } -} - \ No newline at end of file + $this->assertEquals(2, $element->getAttribute('w:start')); + } +} \ No newline at end of file diff --git a/Tests/PHPWord/Writer/Word2007/StylesTest.php b/Tests/PHPWord/Writer/Word2007/StylesTest.php index 42bde30f5a..7052296631 100644 --- a/Tests/PHPWord/Writer/Word2007/StylesTest.php +++ b/Tests/PHPWord/Writer/Word2007/StylesTest.php @@ -51,5 +51,4 @@ public function testWriteStyles() $element = $doc->getElement($path, $file); $this->assertEquals('Normal', $element->getAttribute('w:val')); } - -} +} \ No newline at end of file diff --git a/Tests/_inc/TestHelperDOCX.php b/Tests/_inc/TestHelperDOCX.php index 32f1af08bc..5c603b0b0e 100644 --- a/Tests/_inc/TestHelperDOCX.php +++ b/Tests/_inc/TestHelperDOCX.php @@ -25,7 +25,7 @@ public static function getDocument(PHPWord $PHPWord) $zip = new \ZipArchive; $res = $zip->open(self::$file); if ($res === true) { - $zip->extractTo(sys_get_temp_dir().'/PHPWord_Unit_Test/'); + $zip->extractTo(sys_get_temp_dir() . '/PHPWord_Unit_Test/'); $zip->close(); } diff --git a/Tests/bootstrap.php b/Tests/bootstrap.php index 554dc38f22..916d3503c2 100755 --- a/Tests/bootstrap.php +++ b/Tests/bootstrap.php @@ -3,8 +3,8 @@ date_default_timezone_set('UTC'); // Constantes -if(!defined('PHPWORD_TESTS_DIR_ROOT')){ - define('PHPWORD_TESTS_DIR_ROOT', __DIR__); +if (!defined('PHPWORD_TESTS_DIR_ROOT')) { + define('PHPWORD_TESTS_DIR_ROOT', __DIR__); } // Includes From 35194775359a5c52724122f5aae153fe45c18a2a Mon Sep 17 00:00:00 2001 From: Gabriel Bull Date: Sun, 9 Mar 2014 14:18:49 -0400 Subject: [PATCH 2/3] Removed fake namespace from tests class names because we use real namespacing --- .../PHPWord/Exceptions/InvalidStyleException.php | 15 +++++++++++++++ Tests/PHPWord/IOFactoryTest.php | 9 +++++---- Tests/PHPWord/MediaTest.php | 2 +- Tests/PHPWord/SectionTest.php | 2 +- Tests/PHPWord/Shared/FontTest.php | 6 +++--- Tests/PHPWord/Style/CellTest.php | 6 +++--- Tests/PHPWord/Style/FontTest.php | 6 +++--- Tests/PHPWord/Style/ParagraphTest.php | 6 +++--- Tests/PHPWord/TemplateTest.php | 2 +- Tests/PHPWord/Writer/Word2007/BaseTest.php | 11 +++++------ Tests/PHPWord/Writer/Word2007/DocumentTest.php | 7 ++++--- Tests/PHPWord/Writer/Word2007/StylesTest.php | 6 +++--- changelog.txt | 2 +- 13 files changed, 48 insertions(+), 32 deletions(-) create mode 100644 Classes/PHPWord/Exceptions/InvalidStyleException.php diff --git a/Classes/PHPWord/Exceptions/InvalidStyleException.php b/Classes/PHPWord/Exceptions/InvalidStyleException.php new file mode 100644 index 0000000000..fb5706252a --- /dev/null +++ b/Classes/PHPWord/Exceptions/InvalidStyleException.php @@ -0,0 +1,15 @@ +assertEquals($expected, $element->getAttribute('w:val')); } } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Writer/Word2007/DocumentTest.php b/Tests/PHPWord/Writer/Word2007/DocumentTest.php index b21a559be5..5d91ebabaa 100644 --- a/Tests/PHPWord/Writer/Word2007/DocumentTest.php +++ b/Tests/PHPWord/Writer/Word2007/DocumentTest.php @@ -1,17 +1,18 @@ Date: Sun, 9 Mar 2014 14:26:32 -0400 Subject: [PATCH 3/3] Added line height methods to mirror the line height settings in Word in the paragraph styling --- Classes/PHPWord/Section/Text.php | 17 ++++-- Classes/PHPWord/Style/Paragraph.php | 80 +++++++++++++++++---------- Tests/PHPWord/Style/ParagraphTest.php | 38 +++++++++++++ 3 files changed, 101 insertions(+), 34 deletions(-) diff --git a/Classes/PHPWord/Section/Text.php b/Classes/PHPWord/Section/Text.php index 2b37e7d401..a4e95c9fbd 100755 --- a/Classes/PHPWord/Section/Text.php +++ b/Classes/PHPWord/Section/Text.php @@ -48,7 +48,7 @@ class PHPWord_Section_Text /** * Paragraph style * - * @var PHPWord_Style_Font + * @var \PHPWord_Style_Paragraph */ private $_styleParagraph; @@ -116,7 +116,9 @@ public function getParagraphStyle() /** * Set Paragraph style * - * @return PHPWord_Style_Paragraph + * @param array|\PHPWord_Style_Paragraph $styleParagraph + * @return \PHPWord_Style_Paragraph + * @throws \Exception */ public function setParagraphStyle($styleParagraph) { @@ -124,14 +126,19 @@ public function setParagraphStyle($styleParagraph) $this->_styleParagraph = new PHPWord_Style_Paragraph(); foreach ($styleParagraph as $key => $value) { - if (substr($key, 0, 1) != '_') { + if ($key === 'line-height') { + null; + } elseif (substr($key, 0, 1) != '_') { $key = '_' . $key; } $this->_styleParagraph->setStyleValue($key, $value); } - } else { + } elseif ($styleParagraph instanceof PHPWord_Style_Paragraph) { $this->_styleParagraph = $styleParagraph; + } else { + throw new Exception('Expected array or PHPWord_Style_Paragraph'); } + return $this->_styleParagraph; } /** @@ -143,4 +150,4 @@ public function getText() { return $this->_text; } -} +} \ No newline at end of file diff --git a/Classes/PHPWord/Style/Paragraph.php b/Classes/PHPWord/Style/Paragraph.php index c64d6346ac..574a28e87b 100755 --- a/Classes/PHPWord/Style/Paragraph.php +++ b/Classes/PHPWord/Style/Paragraph.php @@ -25,11 +25,21 @@ * @version 0.7.0 */ +use PHPWord\Exceptions\InvalidStyleException; + /** * PHPWord_Style_Paragraph */ class PHPWord_Style_Paragraph { + const LINE_HEIGHT = 240; + + /* + * Text line height + * + * @var int + */ + private $lineHeight; /** * Paragraph alignment @@ -85,7 +95,7 @@ class PHPWord_Style_Paragraph * * @var string */ - private $_basedOn; + private $_basedOn = 'Normal'; /** * Style for next paragraph @@ -99,63 +109,46 @@ class PHPWord_Style_Paragraph * * @var bool */ - private $_widowControl; + private $_widowControl = true; /** * Keep paragraph with next paragraph * * @var bool */ - private $_keepNext; + private $_keepNext = false; /** * Keep all lines on one page * * @var bool */ - private $_keepLines; + private $_keepLines = false; /** * Start paragraph on next page * * @var bool */ - private $_pageBreakBefore; - - /** - * New Paragraph Style - */ - public function __construct() - { - $this->_align = null; - $this->_spaceBefore = null; - $this->_spaceAfter = null; - $this->_spacing = null; - $this->_tabs = null; - $this->_indent = null; - $this->_hanging = null; - $this->_basedOn = 'Normal'; - $this->_next = null; - $this->_widowControl = true; - $this->_keepNext = false; - $this->_keepLines = false; - $this->_pageBreakBefore = false; - } + private $_pageBreakBefore = false; /** * Set Style value * - * @param string $key - * @param mixed $value + * @param string $key + * @param mixed $value */ public function setStyleValue($key, $value) { if ($key == '_indent' || $key == '_hanging') { $value = $value * 720; - } - if ($key == '_spacing') { + } elseif ($key == '_spacing') { $value += 240; // because line height of 1 matches 240 twips + } elseif ($key === 'line-height') { + $this->setLineHeight($value); + return; } + $this->$key = $value; $method = 'set' . substr($key, 1); if (method_exists($this, $method)) { $this->$method($value); @@ -466,4 +459,33 @@ public function setPageBreakBefore($pValue = false) return $this; } + /** + * Set the line height + * + * @param int|float|string $lineHeight + * @return $this + * @throws \PHPWord\Exceptions\InvalidStyleException + */ + public function setLineHeight($lineHeight) + { + if (is_string($lineHeight)) { + $lineHeight = floatval(preg_replace('/[^0-9\.\,]/', '', $lineHeight)); + } + + if ((!is_integer($lineHeight) && !is_float($lineHeight)) || !$lineHeight) { + throw new InvalidStyleException('Line height must be a valid number'); + } + + $this->lineHeight = $lineHeight; + $this->setSpacing($lineHeight * self::LINE_HEIGHT); + return $this; + } + + /** + * @return int + */ + public function getLineHeight() + { + return $this->lineHeight; + } } \ No newline at end of file diff --git a/Tests/PHPWord/Style/ParagraphTest.php b/Tests/PHPWord/Style/ParagraphTest.php index 7265ee23da..9220923b8c 100644 --- a/Tests/PHPWord/Style/ParagraphTest.php +++ b/Tests/PHPWord/Style/ParagraphTest.php @@ -2,8 +2,10 @@ namespace PHPWord\Tests\Style; use PHPUnit_Framework_TestCase; +use PHPWord; use PHPWord_Style_Paragraph; use PHPWord_Style_Tab; +use PHPWord\Tests\TestHelperDOCX; /** * Class ParagraphTest @@ -13,6 +15,11 @@ */ class ParagraphTest extends \PHPUnit_Framework_TestCase { + public function tearDown() + { + TestHelperDOCX::clear(); + } + /** * Test setting style values with null or empty value */ @@ -85,4 +92,35 @@ public function testTabs() )); $this->assertInstanceOf('PHPWord_Style_Tabs', $object->getTabs()); } + + public function testLineHeight() + { + $PHPWord = new PHPWord(); + $section = $PHPWord->createSection(); + + // Test style array + $text = $section->addText('This is a test', array(), array( + 'line-height' => 2.0 + )); + + $doc = TestHelperDOCX::getDocument($PHPWord); + $element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:spacing'); + + $lineHeight = $element->getAttribute('w:line'); + $lineRule = $element->getAttribute('w:lineRule'); + + $this->assertEquals(480, $lineHeight); + $this->assertEquals('auto', $lineRule); + + // Test setter + $text->getParagraphStyle()->setLineHeight(3.0); + $doc = TestHelperDOCX::getDocument($PHPWord); + $element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:spacing'); + + $lineHeight = $element->getAttribute('w:line'); + $lineRule = $element->getAttribute('w:lineRule'); + + $this->assertEquals(720, $lineHeight); + $this->assertEquals('auto', $lineRule); + } } \ No newline at end of file