diff --git a/Classes/PHPWord/Section.php b/Classes/PHPWord/Section.php index 70dceba2..bdf37f81 100755 --- a/Classes/PHPWord/Section.php +++ b/Classes/PHPWord/Section.php @@ -243,7 +243,9 @@ class PHPWord_Section $this->_elementCollection[] = $object; return $object; } else { - trigger_error('Source does not exist or unsupported object type.'); + throw new PHPWord_Exception( + 'Source does not exist or unsupported object type.' + ); } } @@ -265,7 +267,9 @@ class PHPWord_Section $this->_elementCollection[] = $image; return $image; } else { - trigger_error('Source does not exist or unsupported image type.'); + throw new PHPWord_Exception( + 'Source does not exist or unsupported image type.' + ); } } @@ -286,7 +290,9 @@ class PHPWord_Section $this->_elementCollection[] = $memoryImage; return $memoryImage; } else { - trigger_error('Unsupported image type.'); + throw new PHPWord_Exception( + 'Unsupported image type.' + ); } } diff --git a/Tests/PHPWord/MediaTest.php b/Tests/PHPWord/MediaTest.php index e5722724..1d302444 100644 --- a/Tests/PHPWord/MediaTest.php +++ b/Tests/PHPWord/MediaTest.php @@ -48,4 +48,4 @@ class MediaTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PHPWord_Section_Image', $element); } } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/SectionTest.php b/Tests/PHPWord/SectionTest.php index 7dede374..bbb3014a 100644 --- a/Tests/PHPWord/SectionTest.php +++ b/Tests/PHPWord/SectionTest.php @@ -5,7 +5,11 @@ use PHPUnit_Framework_TestCase; use PHPWord_Section; /** + * Class TOCTest + * + * @package PHPWord\Tests * @covers PHPWord_Section + * @runTestsInSeparateProcesses */ class SectionTest extends \PHPUnit_Framework_TestCase { @@ -44,4 +48,87 @@ class SectionTest extends \PHPUnit_Framework_TestCase $oSection = new PHPWord_Section(0); $this->assertAttributeEquals($oSection->getHeaders(), '_headers', new PHPWord_Section(0)); } + + /** + * @covers PHPWord_Section::setSettings + */ + public function testSetSettings() + { + $expected = 'landscape'; + $object = new PHPWord_Section(0); + $object->setSettings(array('orientation' => $expected)); + $this->assertEquals($expected, $object->getSettings()->getOrientation()); + } + + /** + * @covers PHPWord_Section::addText + * @covers PHPWord_Section::addLink + * @covers PHPWord_Section::addTextBreak + * @covers PHPWord_Section::addPageBreak + * @covers PHPWord_Section::addTable + * @covers PHPWord_Section::addListItem + * @covers PHPWord_Section::addObject + * @covers PHPWord_Section::addImage + * @covers PHPWord_Section::addMemoryImage + * @covers PHPWord_Section::addTOC + * @covers PHPWord_Section::addTitle + * @covers PHPWord_Section::createTextRun + * @covers PHPWord_Section::createFootnote + */ + public function testAddElements() + { + $objectSource = join( + DIRECTORY_SEPARATOR, + array(PHPWORD_TESTS_DIR_ROOT, '_files', 'documents', 'sheet.xls') + ); + $imageSource = join( + DIRECTORY_SEPARATOR, + array(PHPWORD_TESTS_DIR_ROOT, '_files', 'images', 'PHPWord.png') + ); + $imageUrl = 'http://php.net//images/logos/php-med-trans-light.gif'; + + $section = new PHPWord_Section(0); + $section->addText(utf8_decode('ä')); + $section->addLink(utf8_decode('http://äää.com'), utf8_decode('ä')); + $section->addTextBreak(); + $section->addPageBreak(); + $section->addTable(); + $section->addListItem(utf8_decode('ä')); + $section->addObject($objectSource); + $section->addImage($imageSource); + $section->addMemoryImage($imageUrl); + $section->addTOC(); + $section->addTitle(utf8_decode('ä'), 1); + $section->createTextRun(); + $section->createFootnote(); + + $elementCollection = $section->getElements(); + $elementType = 'Link'; + $objectType = "PHPWord_Section_{$elementType}"; + $this->assertInstanceOf($objectType, $elementCollection[1]); + // $elementTypes = array('Text', 'Link', 'TextBreak', 'PageBreak', + // 'Table', 'ListItem', 'Object', 'Image', 'MemoryImage', 'TOC', + // 'Title', 'TextRun'); + // $i = 0; + // foreach ($elementTypes as $elementType) { + // $objectType = "PHPWord_Section_{$elementType}"; + // $this->assertInstanceOf($objectType, $elementCollection[$i]); + // $i++; + // } + } + + /** + * @covers PHPWord_Section::createHeader + * @covers PHPWord_Section::createFooter + */ + public function testCreateHeaderFooter() + { + $object = new PHPWord_Section(0); + $elements = array('Header', 'Footer'); + foreach ($elements as $element) { + $objectType = "PHPWord_Section_{$element}"; + $method = "create{$element}"; + $this->assertInstanceOf($objectType, $object->$method()); + } + } } diff --git a/Tests/PHPWord/Style/TOCTest.php b/Tests/PHPWord/Style/TOCTest.php index 80e34d74..b72d85fc 100644 --- a/Tests/PHPWord/Style/TOCTest.php +++ b/Tests/PHPWord/Style/TOCTest.php @@ -8,6 +8,7 @@ use PHPWord_Style_TOC; * Class TOCTest * * @package PHPWord\Tests + * @covers PHPWord_Style_TOC * @runTestsInSeparateProcesses */ class TOCTest extends \PHPUnit_Framework_TestCase diff --git a/Tests/PHPWord/StyleTest.php b/Tests/PHPWord/StyleTest.php new file mode 100644 index 00000000..36e961ac --- /dev/null +++ b/Tests/PHPWord/StyleTest.php @@ -0,0 +1,45 @@ + 'center'); + $font = array('italic' => true); + $table = array('bgColor' => 'CCCCCC'); + $styles = array('Paragraph' => 'Paragraph', 'Font' => 'Font', + 'Link' => 'Font', 'Table' => 'TableFull', + 'Heading_1' => 'Font', 'Normal' => 'Paragraph'); + $elementCount = 6; + PHPWord_Style::addParagraphStyle('Paragraph', $paragraph); + PHPWord_Style::addFontStyle('Font', $font); + PHPWord_Style::addLinkStyle('Link', $font); + PHPWord_Style::addTableStyle('Table', $table); + PHPWord_Style::addTitleStyle(1, $font); + PHPWord_Style::setDefaultParagraphStyle($paragraph); + + $this->assertEquals($elementCount, count(PHPWord_Style::getStyles())); + foreach ($styles as $name => $style) { + $expected = "PHPWord_Style_{$style}"; + $this->assertInstanceOf($expected, PHPWord_Style::getStyle($name)); + } + $this->assertNull(PHPWord_Style::getStyle('Unknown')); + } +} diff --git a/Tests/PHPWord/TOCTest.php b/Tests/PHPWord/TOCTest.php index 74038ee3..b093f49c 100644 --- a/Tests/PHPWord/TOCTest.php +++ b/Tests/PHPWord/TOCTest.php @@ -6,7 +6,11 @@ use PHPWord_TOC; use PHPWord_Style_TOC; /** - * @covers PHPWord_TOC + * Class TOCTest + * + * @package PHPWord\Tests + * @covers PHPWord_TOC + * @runTestsInSeparateProcesses */ class TOCTest extends PHPUnit_Framework_TestCase { @@ -46,7 +50,7 @@ class TOCTest extends PHPUnit_Framework_TestCase // Prepare variables $titleCount = 3; $anchor = '_Toc' . (252634154 + $titleCount); - $bookmark = $titleCount - 1; // zero based + $bookmark = $titleCount - 1; $titles = array( 'Heading 1' => 1, 'Heading 2' => 2, diff --git a/Tests/PHPWord/Writer/Word2007Test.php b/Tests/PHPWord/Writer/Word2007Test.php index 59bc6387..ae828ec3 100644 --- a/Tests/PHPWord/Writer/Word2007Test.php +++ b/Tests/PHPWord/Writer/Word2007Test.php @@ -66,23 +66,28 @@ class Word2007Test extends \PHPUnit_Framework_TestCase */ public function testCheckContentTypes() { + $images = array( + 'mars_noext_jpg' => '1.jpg', + 'mars.jpg' => '2.jpg', + 'mario.gif' => '3.gif', + 'firefox.png' => '4.png', + 'duke_nukem.bmp' => '5.bmp', + 'angela_merkel.tif' => '6.tif', + ); $phpWord = new PHPWord(); $section = $phpWord->createSection(); - $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg"); - $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars.jpg"); - $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mario.gif"); - $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/firefox.png"); - $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/duke_nukem.bmp"); - $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/angela_merkel.tif"); + foreach ($images as $source => $target) { + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/{$source}"); + } $doc = TestHelperDOCX::getDocument($phpWord); $mediaPath = $doc->getPath() . "/word/media"; - $this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg", $mediaPath . "/section_image1.jpg"); - $this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars.jpg", $mediaPath . "/section_image2.jpg"); - $this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mario.gif", $mediaPath . "/section_image3.gif"); - $this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/firefox.png", $mediaPath . "/section_image4.png"); - $this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/duke_nukem.bmp", $mediaPath . "/section_image5.bmp"); - $this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/angela_merkel.tif", $mediaPath . "/section_image6.tif"); + foreach ($images as $source => $target) { + $this->assertFileEquals( + PHPWORD_TESTS_DIR_ROOT . "/_files/images/{$source}", + $mediaPath . "/section_image{$target}" + ); + } } -} \ No newline at end of file +} diff --git a/Tests/PHPWordTest.php b/Tests/PHPWordTest.php index 54ab4ae0..0aa4f348 100644 --- a/Tests/PHPWordTest.php +++ b/Tests/PHPWordTest.php @@ -8,7 +8,11 @@ use PHPWord_Section; use PHPWord_Style; /** - * @covers PHPWord + * Class PHPWordTest + * + * @package PHPWord\Tests + * @covers PHPWord + * @runTestsInSeparateProcesses */ class PHPWordTest extends PHPUnit_Framework_TestCase { diff --git a/Tests/_files/images/PHPWord.png b/Tests/_files/images/PHPWord.png new file mode 100644 index 00000000..9b7f4930 Binary files /dev/null and b/Tests/_files/images/PHPWord.png differ diff --git a/Tests/_inc/TestHelperDOCX.php b/Tests/_inc/TestHelperDOCX.php index 2d59dcb6..5f8e51a3 100644 --- a/Tests/_inc/TestHelperDOCX.php +++ b/Tests/_inc/TestHelperDOCX.php @@ -67,4 +67,4 @@ class TestHelperDOCX { return self::$file; } -} \ No newline at end of file +}