From 92cfa12d71696538d09f4a2684c82a530646a28e Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Wed, 12 Mar 2014 13:30:02 +0700 Subject: [PATCH] Code formatting for PSR1 & PSR2 - Part 3 --- .../Exceptions/InvalidImageException.php | 2 +- .../UnsupportedImageTypeException.php | 2 +- Tests/PHPWord/AutoloaderTest.php | 28 +++++- Tests/PHPWord/IOFactoryTest.php | 17 +++- .../Section/Footer/PreserveTextTest.php | 6 +- Tests/PHPWord/Section/FooterTest.php | 4 +- Tests/PHPWord/Section/HeaderTest.php | 4 +- Tests/PHPWord/Section/ImageTest.php | 6 +- Tests/PHPWord/Section/LinkTest.php | 7 +- Tests/PHPWord/Section/ListItemTest.php | 7 +- Tests/PHPWord/Section/Table/CellTest.php | 12 ++- Tests/PHPWord/Section/Table/RowTest.php | 7 +- Tests/PHPWord/Section/TableTest.php | 6 +- Tests/PHPWord/Writer/Word2007/BaseTest.php | 98 +++++++++---------- Tests/_inc/TestHelperDOCX.php | 72 +------------- Tests/_inc/XmlDocument.php | 4 +- 16 files changed, 143 insertions(+), 139 deletions(-) diff --git a/Classes/PHPWord/Exceptions/InvalidImageException.php b/Classes/PHPWord/Exceptions/InvalidImageException.php index 2d5337b4..eda84a8d 100644 --- a/Classes/PHPWord/Exceptions/InvalidImageException.php +++ b/Classes/PHPWord/Exceptions/InvalidImageException.php @@ -12,4 +12,4 @@ use Exception; */ class InvalidImageException extends Exception { -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Exceptions/UnsupportedImageTypeException.php b/Classes/PHPWord/Exceptions/UnsupportedImageTypeException.php index 56bb310e..3a4975fe 100644 --- a/Classes/PHPWord/Exceptions/UnsupportedImageTypeException.php +++ b/Classes/PHPWord/Exceptions/UnsupportedImageTypeException.php @@ -12,4 +12,4 @@ use Exception; */ class UnsupportedImageTypeException extends Exception { -} \ No newline at end of file +} diff --git a/Tests/PHPWord/AutoloaderTest.php b/Tests/PHPWord/AutoloaderTest.php index 45ee6066..d8882f9f 100644 --- a/Tests/PHPWord/AutoloaderTest.php +++ b/Tests/PHPWord/AutoloaderTest.php @@ -16,8 +16,14 @@ class AutoloaderTest extends PHPUnit_Framework_TestCase public function testAutoloadLegacy() { - $this->assertNull(PHPWord_Autoloader::load('Foo'), 'PHPWord_Autoloader::load() is trying to load classes outside of the PHPWord namespace'); - $this->assertTrue(PHPWord_Autoloader::load('PHPWord'), 'PHPWord_Autoloader::load() failed to autoload the PHPWord class'); + $this->assertNull( + PHPWord_Autoloader::load('Foo'), + 'PHPWord_Autoloader::load() is trying to load classes outside of the PHPWord namespace' + ); + $this->assertTrue( + PHPWord_Autoloader::load('PHPWord'), + 'PHPWord_Autoloader::load() failed to autoload the PHPWord class' + ); } public function testAutoload() @@ -25,8 +31,20 @@ class AutoloaderTest extends PHPUnit_Framework_TestCase $declared = get_declared_classes(); $declaredCount = count($declared); Autoloader::autoload('Foo'); - $this->assertEquals($declaredCount, count(get_declared_classes()), 'PhpOffice\\PhpWord\\Autoloader::autoload() is trying to load classes outside of the PhpOffice\\PhpWord namespace'); - Autoloader::autoload('PhpOffice\\PhpWord\\Exceptions\\InvalidStyleException'); // TODO change this class to the main PHPWord class when it is namespaced - $this->assertTrue(in_array('PhpOffice\\PhpWord\\Exceptions\\InvalidStyleException', get_declared_classes()), 'PhpOffice\\PhpWord\\Autoloader::autoload() failed to autoload the PhpOffice\\PhpWord\\Exceptions\\InvalidStyleException class'); + $this->assertEquals( + $declaredCount, + count(get_declared_classes()), + 'PhpOffice\\PhpWord\\Autoloader::autoload() is trying to load classes ' . + 'outside of the PhpOffice\\PhpWord namespace' + ); + // TODO change this class to the main PHPWord class when it is namespaced + Autoloader::autoload( + 'PhpOffice\\PhpWord\\Exceptions\\InvalidStyleException' + ); + $this->assertTrue( + in_array('PhpOffice\\PhpWord\\Exceptions\\InvalidStyleException', get_declared_classes()), + 'PhpOffice\\PhpWord\\Autoloader::autoload() failed to autoload the ' . + 'PhpOffice\\PhpWord\\Exceptions\\InvalidStyleException class' + ); } } diff --git a/Tests/PHPWord/IOFactoryTest.php b/Tests/PHPWord/IOFactoryTest.php index 0c2976b6..c0dfab30 100644 --- a/Tests/PHPWord/IOFactoryTest.php +++ b/Tests/PHPWord/IOFactoryTest.php @@ -16,7 +16,11 @@ class IOFactoryTest extends \PHPUnit_Framework_TestCase { public function testGetSearchLocations() { - $this->assertAttributeEquals(PHPWord_IOFactory::getSearchLocations(), '_searchLocations', 'PHPWord_IOFactory'); + $this->assertAttributeEquals( + PHPWord_IOFactory::getSearchLocations(), + '_searchLocations', + 'PHPWord_IOFactory' + ); } public function testSetSearchLocationsWithArray() @@ -38,7 +42,11 @@ class IOFactoryTest extends \PHPUnit_Framework_TestCase { PHPWord_IOFactory::setSearchLocations(array()); PHPWord_IOFactory::addSearchLocation('type', 'location', 'classname'); - $this->assertAttributeEquals(array(array('type' => 'type', 'path' => 'location', 'class' => 'classname')), '_searchLocations', 'PHPWord_IOFactory'); + $this->assertAttributeEquals( + array(array('type' => 'type', 'path' => 'location', 'class' => 'classname')), + '_searchLocations', + 'PHPWord_IOFactory' + ); } /** @@ -57,6 +65,9 @@ class IOFactoryTest extends \PHPUnit_Framework_TestCase { $oPHPWord = new PHPWord(); - $this->assertEquals(PHPWord_IOFactory::createWriter($oPHPWord, 'Word2007'), new PHPWord_Writer_Word2007($oPHPWord)); + $this->assertEquals( + PHPWord_IOFactory::createWriter($oPHPWord, 'Word2007'), + new PHPWord_Writer_Word2007($oPHPWord) + ); } } diff --git a/Tests/PHPWord/Section/Footer/PreserveTextTest.php b/Tests/PHPWord/Section/Footer/PreserveTextTest.php index c8297738..02e82fa1 100644 --- a/Tests/PHPWord/Section/Footer/PreserveTextTest.php +++ b/Tests/PHPWord/Section/Footer/PreserveTextTest.php @@ -26,7 +26,11 @@ class PreserveTextTest extends \PHPUnit_Framework_TestCase public function testConstructWithArray() { - $oPreserveText = new PHPWord_Section_Footer_PreserveText('text', array('align' => 'center'), array('marginLeft' => 600, 'marginRight' => 600, 'marginTop' => 600, 'marginBottom' => 600)); + $oPreserveText = new PHPWord_Section_Footer_PreserveText( + 'text', + array('align' => 'center'), + array('marginLeft' => 600, 'marginRight' => 600, 'marginTop' => 600, 'marginBottom' => 600) + ); $this->assertInstanceOf('PHPWord_Style_Font', $oPreserveText->getFontStyle()); $this->assertInstanceOf('PHPWord_Style_Paragraph', $oPreserveText->getParagraphStyle()); } diff --git a/Tests/PHPWord/Section/FooterTest.php b/Tests/PHPWord/Section/FooterTest.php index de1fa613..2942f2fb 100644 --- a/Tests/PHPWord/Section/FooterTest.php +++ b/Tests/PHPWord/Section/FooterTest.php @@ -87,7 +87,9 @@ class FooterTest extends \PHPUnit_Framework_TestCase public function testAddMemoryImage() { $oFooter = new PHPWord_Section_Footer(1); - $element = $oFooter->addMemoryImage('https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png'); + $element = $oFooter->addMemoryImage( + 'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png' + ); $this->assertCount(1, $oFooter->getElements()); $this->assertInstanceOf('PHPWord_Section_MemoryImage', $element); diff --git a/Tests/PHPWord/Section/HeaderTest.php b/Tests/PHPWord/Section/HeaderTest.php index f9b05318..f71f9d81 100644 --- a/Tests/PHPWord/Section/HeaderTest.php +++ b/Tests/PHPWord/Section/HeaderTest.php @@ -83,7 +83,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase public function testAddMemoryImage() { $oHeader = new PHPWord_Section_Header(1); - $element = $oHeader->addMemoryImage('https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png'); + $element = $oHeader->addMemoryImage( + 'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png' + ); $this->assertCount(1, $oHeader->getElements()); $this->assertInstanceOf('PHPWord_Section_MemoryImage', $element); diff --git a/Tests/PHPWord/Section/ImageTest.php b/Tests/PHPWord/Section/ImageTest.php index 8083673a..5832b8c3 100644 --- a/Tests/PHPWord/Section/ImageTest.php +++ b/Tests/PHPWord/Section/ImageTest.php @@ -28,7 +28,11 @@ class ImageTest extends \PHPUnit_Framework_TestCase \DIRECTORY_SEPARATOR, array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'images', 'firefox.png') ); - $oImage = new PHPWord_Section_Image($src, array('width' => 210, 'height' => 210, 'align' => 'center', 'wrappingStyle' => \PHPWord_Style_Image::WRAPPING_STYLE_BEHIND)); + $oImage = new PHPWord_Section_Image( + $src, + array('width' => 210, 'height' => 210, 'align' => 'center', + 'wrappingStyle' => \PHPWord_Style_Image::WRAPPING_STYLE_BEHIND) + ); $this->assertInstanceOf('PHPWord_Style_Image', $oImage->getStyle()); } diff --git a/Tests/PHPWord/Section/LinkTest.php b/Tests/PHPWord/Section/LinkTest.php index 81c5788e..340ddc41 100644 --- a/Tests/PHPWord/Section/LinkTest.php +++ b/Tests/PHPWord/Section/LinkTest.php @@ -20,7 +20,12 @@ class LinkTest extends \PHPUnit_Framework_TestCase public function testConstructWithParamsArray() { - $oLink = new PHPWord_Section_Link('http://www.google.com', 'Search Engine', array('color' => '0000FF', 'underline' => PHPWord_Style_Font::UNDERLINE_SINGLE), array('marginLeft' => 600, 'marginRight' => 600, 'marginTop' => 600, 'marginBottom' => 600)); + $oLink = new PHPWord_Section_Link( + 'http://www.google.com', + 'Search Engine', + array('color' => '0000FF', 'underline' => PHPWord_Style_Font::UNDERLINE_SINGLE), + array('marginLeft' => 600, 'marginRight' => 600, 'marginTop' => 600, 'marginBottom' => 600) + ); $this->assertInstanceOf('PHPWord_Section_Link', $oLink); $this->assertEquals($oLink->getLinkSrc(), 'http://www.google.com'); diff --git a/Tests/PHPWord/Section/ListItemTest.php b/Tests/PHPWord/Section/ListItemTest.php index 56973822..28323695 100644 --- a/Tests/PHPWord/Section/ListItemTest.php +++ b/Tests/PHPWord/Section/ListItemTest.php @@ -16,7 +16,12 @@ class ListItemTest extends \PHPUnit_Framework_TestCase public function testStyle() { - $oListItem = new PHPWord_Section_ListItem('text', 1, null, array('listType' => PHPWord_Style_ListItem::TYPE_NUMBER)); + $oListItem = new PHPWord_Section_ListItem( + 'text', + 1, + null, + array('listType' => PHPWord_Style_ListItem::TYPE_NUMBER) + ); $this->assertInstanceOf('PHPWord_Style_ListItem', $oListItem->getStyle()); $this->assertEquals($oListItem->getStyle()->getListType(), PHPWord_Style_ListItem::TYPE_NUMBER); diff --git a/Tests/PHPWord/Section/Table/CellTest.php b/Tests/PHPWord/Section/Table/CellTest.php index 261c9ee0..8d5b99bd 100644 --- a/Tests/PHPWord/Section/Table/CellTest.php +++ b/Tests/PHPWord/Section/Table/CellTest.php @@ -130,7 +130,9 @@ class CellTest extends \PHPUnit_Framework_TestCase public function testAddMemoryImageSection() { $oCell = new PHPWord_Section_Table_Cell('section', 1); - $element = $oCell->addMemoryImage('https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png'); + $element = $oCell->addMemoryImage( + 'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png' + ); $this->assertCount(1, $oCell->getElements()); $this->assertInstanceOf('PHPWord_Section_MemoryImage', $element); @@ -139,7 +141,9 @@ class CellTest extends \PHPUnit_Framework_TestCase public function testAddMemoryImageHeader() { $oCell = new PHPWord_Section_Table_Cell('header', 1); - $element = $oCell->addMemoryImage('https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png'); + $element = $oCell->addMemoryImage( + 'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png' + ); $this->assertCount(1, $oCell->getElements()); $this->assertInstanceOf('PHPWord_Section_MemoryImage', $element); @@ -148,7 +152,9 @@ class CellTest extends \PHPUnit_Framework_TestCase public function testAddMemoryImageFooter() { $oCell = new PHPWord_Section_Table_Cell('footer', 1); - $element = $oCell->addMemoryImage('https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png'); + $element = $oCell->addMemoryImage( + 'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png' + ); $this->assertCount(1, $oCell->getElements()); $this->assertInstanceOf('PHPWord_Section_MemoryImage', $element); diff --git a/Tests/PHPWord/Section/Table/RowTest.php b/Tests/PHPWord/Section/Table/RowTest.php index c6dd762a..115eb1c7 100644 --- a/Tests/PHPWord/Section/Table/RowTest.php +++ b/Tests/PHPWord/Section/Table/RowTest.php @@ -22,7 +22,12 @@ class RowTest extends \PHPUnit_Framework_TestCase { $iVal = rand(1, 1000); $iVal2 = rand(1, 1000); - $oRow = new PHPWord_Section_Table_Row('section', $iVal, $iVal2, array('borderBottomSize' => 18, 'borderBottomColor' => '0000FF', 'bgColor' => '66BBFF')); + $oRow = new PHPWord_Section_Table_Row( + 'section', + $iVal, + $iVal2, + array('borderBottomSize' => 18, 'borderBottomColor' => '0000FF', 'bgColor' => '66BBFF') + ); $this->assertEquals($oRow->getHeight(), $iVal2); $this->assertInstanceOf('PHPWord_Style_Row', $oRow->getStyle()); diff --git a/Tests/PHPWord/Section/TableTest.php b/Tests/PHPWord/Section/TableTest.php index 6c479e36..80cd4296 100644 --- a/Tests/PHPWord/Section/TableTest.php +++ b/Tests/PHPWord/Section/TableTest.php @@ -26,7 +26,11 @@ class TableTest extends \PHPUnit_Framework_TestCase public function testStyleArray() { - $oTable = new PHPWord_Section_Table('section', 1, array('borderSize' => 6, 'borderColor' => '006699', 'cellMargin' => 80)); + $oTable = new PHPWord_Section_Table( + 'section', + 1, + array('borderSize' => 6, 'borderColor' => '006699', 'cellMargin' => 80) + ); $this->assertInstanceOf('PHPWord_Style_Table', $oTable->getStyle()); } diff --git a/Tests/PHPWord/Writer/Word2007/BaseTest.php b/Tests/PHPWord/Writer/Word2007/BaseTest.php index cb8fa578..cd1f6dbd 100644 --- a/Tests/PHPWord/Writer/Word2007/BaseTest.php +++ b/Tests/PHPWord/Writer/Word2007/BaseTest.php @@ -20,29 +20,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase 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() + public function testWriteParagraphStyleAlign() { $PHPWord = new PHPWord(); $section = $PHPWord->createSection(); @@ -55,34 +33,10 @@ class BaseTest extends \PHPUnit_Framework_TestCase $this->assertEquals('right', $element->getAttribute('w:val')); } - public function testWriteCellStyle_CellGridSpan() - { - $PHPWord = new PHPWord(); - $section = $PHPWord->createSection(); - - $table = $section->addTable(); - - $table->addRow(); - $cell = $table->addCell(200); - $cell->getStyle()->setGridSpan(5); - - $table->addRow(); - $table->addCell(40); - $table->addCell(40); - $table->addCell(40); - $table->addCell(40); - $table->addCell(40); - - $doc = TestHelperDOCX::getDocument($PHPWord); - $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 */ - public function testWriteParagraphStyle_Pagination() + public function testWriteParagraphStylePagination() { // Create the doc $PHPWord = new PHPWord(); @@ -109,6 +63,52 @@ class BaseTest extends \PHPUnit_Framework_TestCase } } + public function testWriteCellStyleCellGridSpan() + { + $PHPWord = new PHPWord(); + $section = $PHPWord->createSection(); + + $table = $section->addTable(); + + $table->addRow(); + $cell = $table->addCell(200); + $cell->getStyle()->setGridSpan(5); + + $table->addRow(); + $table->addCell(40); + $table->addCell(40); + $table->addCell(40); + $table->addCell(40); + $table->addCell(40); + + $doc = TestHelperDOCX::getDocument($PHPWord); + $element = $doc->getElement('/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:gridSpan'); + + $this->assertEquals(5, $element->getAttribute('w:val')); + } + + public function testWriteImagePosition() + { + $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 testWritePreserveText() { $PHPWord = new PHPWord(); @@ -123,4 +123,4 @@ class BaseTest extends \PHPUnit_Framework_TestCase $this->assertEquals('PAGE', $preserve->nodeValue); $this->assertEquals('preserve', $preserve->getAttribute('xml:space')); } -} \ No newline at end of file +} diff --git a/Tests/_inc/TestHelperDOCX.php b/Tests/_inc/TestHelperDOCX.php index 965835d0..643cc00a 100644 --- a/Tests/_inc/TestHelperDOCX.php +++ b/Tests/_inc/TestHelperDOCX.php @@ -2,15 +2,15 @@ namespace PHPWord\Tests; use PHPWord; -use DOMDocument; class TestHelperDOCX { + /** @var string $file */ static protected $file; /** * @param \PHPWord $PHPWord - * @return \PHPWord\Tests\Xml_Document + * @return \PHPWord\Tests\XmlDocument */ public static function getDocument(PHPWord $PHPWord) { @@ -29,7 +29,7 @@ class TestHelperDOCX $zip->close(); } - return new Xml_Document(sys_get_temp_dir() . '/PHPWord_Unit_Test/'); + return new XmlDocument(sys_get_temp_dir() . '/PHPWord_Unit_Test/'); } public static function clear() @@ -50,9 +50,9 @@ class TestHelperDOCX foreach (scandir($dir) as $file) { if ($file === '.' || $file === '..') { continue; - } else if (is_file($dir . "/" . $file)) { + } elseif (is_file($dir . "/" . $file)) { unlink($dir . "/" . $file); - } else if (is_dir($dir . "/" . $file)) { + } elseif (is_dir($dir . "/" . $file)) { self::deleteDir($dir . "/" . $file); } } @@ -60,65 +60,3 @@ class TestHelperDOCX rmdir($dir); } } - -class Xml_Document -{ - /** @var string $path */ - private $path; - - /** @var \DOMDocument $dom */ - private $dom; - - /** @var \DOMXpath $xpath */ - private $xpath; - - /** @var string $file */ - private $file; - - /** - * @param string $path - */ - public function __construct($path) - { - $this->path = realpath($path); - } - - /** - * @param string $file - * @return \DOMDocument - */ - public function getFileDom($file = 'word/document.xml') - { - if (null !== $this->dom && $file === $this->file) { - return $this->dom; - } - - $this->xpath = null; - $this->file = $file; - - $file = $this->path . '/' . $file; - $this->dom = new DOMDocument(); - $this->dom->load($file); - return $this->dom; - } - - /** - * @param string $path - * @param string $file - * @return \DOMElement - */ - public function getElement($path, $file = 'word/document.xml') - { - if ($this->dom === null || $file !== $this->file) { - $this->getFileDom($file); - } - - if (null === $this->xpath) { - $this->xpath = new \DOMXpath($this->dom); - - } - - $elements = $this->xpath->query($path); - return $elements->item(0); - } -} \ No newline at end of file diff --git a/Tests/_inc/XmlDocument.php b/Tests/_inc/XmlDocument.php index 2ec27a3b..69569aa6 100644 --- a/Tests/_inc/XmlDocument.php +++ b/Tests/_inc/XmlDocument.php @@ -36,9 +36,9 @@ class XmlDocument } $this->xpath = null; - $this->file = $file; + $this->file = $file; - $file = $this->path . '/' . $file; + $file = $this->path . '/' . $file; $this->dom = new DOMDocument(); $this->dom->load($file); return $this->dom;