Code formatting for PSR1 & PSR2 - Part 3

This commit is contained in:
Ivan Lanin 2014-03-12 13:30:02 +07:00
parent 46a0768d29
commit 92cfa12d71
16 changed files with 143 additions and 139 deletions

View File

@ -12,4 +12,4 @@ use Exception;
*/ */
class InvalidImageException extends Exception class InvalidImageException extends Exception
{ {
} }

View File

@ -12,4 +12,4 @@ use Exception;
*/ */
class UnsupportedImageTypeException extends Exception class UnsupportedImageTypeException extends Exception
{ {
} }

View File

@ -16,8 +16,14 @@ class AutoloaderTest extends PHPUnit_Framework_TestCase
public function testAutoloadLegacy() public function testAutoloadLegacy()
{ {
$this->assertNull(PHPWord_Autoloader::load('Foo'), 'PHPWord_Autoloader::load() is trying to load classes outside of the PHPWord namespace'); $this->assertNull(
$this->assertTrue(PHPWord_Autoloader::load('PHPWord'), 'PHPWord_Autoloader::load() failed to autoload the PHPWord class'); 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() public function testAutoload()
@ -25,8 +31,20 @@ class AutoloaderTest extends PHPUnit_Framework_TestCase
$declared = get_declared_classes(); $declared = get_declared_classes();
$declaredCount = count($declared); $declaredCount = count($declared);
Autoloader::autoload('Foo'); 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'); $this->assertEquals(
Autoloader::autoload('PhpOffice\\PhpWord\\Exceptions\\InvalidStyleException'); // TODO change this class to the main PHPWord class when it is namespaced $declaredCount,
$this->assertTrue(in_array('PhpOffice\\PhpWord\\Exceptions\\InvalidStyleException', get_declared_classes()), 'PhpOffice\\PhpWord\\Autoloader::autoload() failed to autoload the PhpOffice\\PhpWord\\Exceptions\\InvalidStyleException class'); 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'
);
} }
} }

View File

@ -16,7 +16,11 @@ class IOFactoryTest extends \PHPUnit_Framework_TestCase
{ {
public function testGetSearchLocations() public function testGetSearchLocations()
{ {
$this->assertAttributeEquals(PHPWord_IOFactory::getSearchLocations(), '_searchLocations', 'PHPWord_IOFactory'); $this->assertAttributeEquals(
PHPWord_IOFactory::getSearchLocations(),
'_searchLocations',
'PHPWord_IOFactory'
);
} }
public function testSetSearchLocationsWithArray() public function testSetSearchLocationsWithArray()
@ -38,7 +42,11 @@ class IOFactoryTest extends \PHPUnit_Framework_TestCase
{ {
PHPWord_IOFactory::setSearchLocations(array()); PHPWord_IOFactory::setSearchLocations(array());
PHPWord_IOFactory::addSearchLocation('type', 'location', 'classname'); 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(); $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)
);
} }
} }

View File

@ -26,7 +26,11 @@ class PreserveTextTest extends \PHPUnit_Framework_TestCase
public function testConstructWithArray() 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_Font', $oPreserveText->getFontStyle());
$this->assertInstanceOf('PHPWord_Style_Paragraph', $oPreserveText->getParagraphStyle()); $this->assertInstanceOf('PHPWord_Style_Paragraph', $oPreserveText->getParagraphStyle());
} }

View File

@ -87,7 +87,9 @@ class FooterTest extends \PHPUnit_Framework_TestCase
public function testAddMemoryImage() public function testAddMemoryImage()
{ {
$oFooter = new PHPWord_Section_Footer(1); $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->assertCount(1, $oFooter->getElements());
$this->assertInstanceOf('PHPWord_Section_MemoryImage', $element); $this->assertInstanceOf('PHPWord_Section_MemoryImage', $element);

View File

@ -83,7 +83,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase
public function testAddMemoryImage() public function testAddMemoryImage()
{ {
$oHeader = new PHPWord_Section_Header(1); $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->assertCount(1, $oHeader->getElements());
$this->assertInstanceOf('PHPWord_Section_MemoryImage', $element); $this->assertInstanceOf('PHPWord_Section_MemoryImage', $element);

View File

@ -28,7 +28,11 @@ class ImageTest extends \PHPUnit_Framework_TestCase
\DIRECTORY_SEPARATOR, \DIRECTORY_SEPARATOR,
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'images', 'firefox.png') 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()); $this->assertInstanceOf('PHPWord_Style_Image', $oImage->getStyle());
} }

View File

@ -20,7 +20,12 @@ class LinkTest extends \PHPUnit_Framework_TestCase
public function testConstructWithParamsArray() 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->assertInstanceOf('PHPWord_Section_Link', $oLink);
$this->assertEquals($oLink->getLinkSrc(), 'http://www.google.com'); $this->assertEquals($oLink->getLinkSrc(), 'http://www.google.com');

View File

@ -16,7 +16,12 @@ class ListItemTest extends \PHPUnit_Framework_TestCase
public function testStyle() 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->assertInstanceOf('PHPWord_Style_ListItem', $oListItem->getStyle());
$this->assertEquals($oListItem->getStyle()->getListType(), PHPWord_Style_ListItem::TYPE_NUMBER); $this->assertEquals($oListItem->getStyle()->getListType(), PHPWord_Style_ListItem::TYPE_NUMBER);

View File

@ -130,7 +130,9 @@ class CellTest extends \PHPUnit_Framework_TestCase
public function testAddMemoryImageSection() public function testAddMemoryImageSection()
{ {
$oCell = new PHPWord_Section_Table_Cell('section', 1); $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->assertCount(1, $oCell->getElements());
$this->assertInstanceOf('PHPWord_Section_MemoryImage', $element); $this->assertInstanceOf('PHPWord_Section_MemoryImage', $element);
@ -139,7 +141,9 @@ class CellTest extends \PHPUnit_Framework_TestCase
public function testAddMemoryImageHeader() public function testAddMemoryImageHeader()
{ {
$oCell = new PHPWord_Section_Table_Cell('header', 1); $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->assertCount(1, $oCell->getElements());
$this->assertInstanceOf('PHPWord_Section_MemoryImage', $element); $this->assertInstanceOf('PHPWord_Section_MemoryImage', $element);
@ -148,7 +152,9 @@ class CellTest extends \PHPUnit_Framework_TestCase
public function testAddMemoryImageFooter() public function testAddMemoryImageFooter()
{ {
$oCell = new PHPWord_Section_Table_Cell('footer', 1); $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->assertCount(1, $oCell->getElements());
$this->assertInstanceOf('PHPWord_Section_MemoryImage', $element); $this->assertInstanceOf('PHPWord_Section_MemoryImage', $element);

View File

@ -22,7 +22,12 @@ class RowTest extends \PHPUnit_Framework_TestCase
{ {
$iVal = rand(1, 1000); $iVal = rand(1, 1000);
$iVal2 = 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->assertEquals($oRow->getHeight(), $iVal2);
$this->assertInstanceOf('PHPWord_Style_Row', $oRow->getStyle()); $this->assertInstanceOf('PHPWord_Style_Row', $oRow->getStyle());

View File

@ -26,7 +26,11 @@ class TableTest extends \PHPUnit_Framework_TestCase
public function testStyleArray() 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()); $this->assertInstanceOf('PHPWord_Style_Table', $oTable->getStyle());
} }

View File

@ -20,29 +20,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase
TestHelperDOCX::clear(); TestHelperDOCX::clear();
} }
public function testWriteImage_Position() public function testWriteParagraphStyleAlign()
{
$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(); $PHPWord = new PHPWord();
$section = $PHPWord->createSection(); $section = $PHPWord->createSection();
@ -55,34 +33,10 @@ class BaseTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('right', $element->getAttribute('w:val')); $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 * Test write paragraph pagination
*/ */
public function testWriteParagraphStyle_Pagination() public function testWriteParagraphStylePagination()
{ {
// Create the doc // Create the doc
$PHPWord = new PHPWord(); $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() public function testWritePreserveText()
{ {
$PHPWord = new PHPWord(); $PHPWord = new PHPWord();
@ -123,4 +123,4 @@ class BaseTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('PAGE', $preserve->nodeValue); $this->assertEquals('PAGE', $preserve->nodeValue);
$this->assertEquals('preserve', $preserve->getAttribute('xml:space')); $this->assertEquals('preserve', $preserve->getAttribute('xml:space'));
} }
} }

View File

@ -2,15 +2,15 @@
namespace PHPWord\Tests; namespace PHPWord\Tests;
use PHPWord; use PHPWord;
use DOMDocument;
class TestHelperDOCX class TestHelperDOCX
{ {
/** @var string $file */
static protected $file; static protected $file;
/** /**
* @param \PHPWord $PHPWord * @param \PHPWord $PHPWord
* @return \PHPWord\Tests\Xml_Document * @return \PHPWord\Tests\XmlDocument
*/ */
public static function getDocument(PHPWord $PHPWord) public static function getDocument(PHPWord $PHPWord)
{ {
@ -29,7 +29,7 @@ class TestHelperDOCX
$zip->close(); $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() public static function clear()
@ -50,9 +50,9 @@ class TestHelperDOCX
foreach (scandir($dir) as $file) { foreach (scandir($dir) as $file) {
if ($file === '.' || $file === '..') { if ($file === '.' || $file === '..') {
continue; continue;
} else if (is_file($dir . "/" . $file)) { } elseif (is_file($dir . "/" . $file)) {
unlink($dir . "/" . $file); unlink($dir . "/" . $file);
} else if (is_dir($dir . "/" . $file)) { } elseif (is_dir($dir . "/" . $file)) {
self::deleteDir($dir . "/" . $file); self::deleteDir($dir . "/" . $file);
} }
} }
@ -60,65 +60,3 @@ class TestHelperDOCX
rmdir($dir); 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);
}
}

View File

@ -36,9 +36,9 @@ class XmlDocument
} }
$this->xpath = null; $this->xpath = null;
$this->file = $file; $this->file = $file;
$file = $this->path . '/' . $file; $file = $this->path . '/' . $file;
$this->dom = new DOMDocument(); $this->dom = new DOMDocument();
$this->dom->load($file); $this->dom->load($file);
return $this->dom; return $this->dom;