From 516c13e032fde63ffadbe00bd6a34015bcd8b75c Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sat, 10 May 2014 23:40:34 +0700 Subject: [PATCH] Some unit tests for the new features --- src/PhpWord/Shared/Html.php | 8 ++- tests/PhpWord/Tests/Shared/HtmlTest.php | 65 +++++++++++++++++++ .../Writer/Word2007/Part/DocumentTest.php | 6 ++ 3 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 tests/PhpWord/Tests/Shared/HtmlTest.php diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index b09afddb..1df56e68 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -37,7 +37,7 @@ class Html * @todo parse $stylesheet for default styles. Should result in an array based on id, class and element, * which could be applied when such an element occurs in the parseNode function. */ - $html = str_replace(array("\n","\r"), '', $html); + $html = str_replace(array("\n", "\r"), '', $html); $dom = new \DOMDocument(); $dom->preserveWhiteSpace = true; @@ -102,7 +102,7 @@ class Html * @param \DOMNode $node node to parse * @param \PhpOffice\PhpWord\Element\AbstractElement $object object to add an element corresponding with the node * @param array $styles Array with all styles - * @param $data array to transport data to a next level in the DOM tree, for example level of listitems + * @param array $data Array to transport data to a next level in the DOM tree, for example level of listitems * */ protected static function parseNode( @@ -148,7 +148,9 @@ class Html break; case '#text': $styles['fontStyle'] = self::parseInlineStyle($node, $styles['fontStyle']); - $object->AddText($node->nodeValue, $styles['fontStyle'], $styles['paragraphStyle']); + if (method_exists($object, 'addText')) { + $object->addText($node->nodeValue, $styles['fontStyle'], $styles['paragraphStyle']); + } break; case 'strong': $styles['fontStyle']['bold'] = true; diff --git a/tests/PhpWord/Tests/Shared/HtmlTest.php b/tests/PhpWord/Tests/Shared/HtmlTest.php new file mode 100644 index 00000000..81714432 --- /dev/null +++ b/tests/PhpWord/Tests/Shared/HtmlTest.php @@ -0,0 +1,65 @@ +assertEquals(0, $section->countElements()); + + // Heading + $styles = array('strong', 'em', 'sup', 'sub'); + for ($level = 1; $level <= 6; $level++) { + $content .= "Heading {$level}"; + } + + // Styles + $content .= '

'; + foreach ($styles as $style) { + $content .= "<{$style}>{$style}"; + } + $content .= '

'; + + // Add HTML + Html::addHtml($section, $content); + $this->assertEquals(7, $section->countElements()); + + // Other parts + $section = new Section(1); + $content = ''; + $content .= '
HeaderContent
'; + $content .= ''; + $content .= '
  1. Bullet
'; + Html::addHtml($section, $content, null, array('listdepth' => 2)); + } +} diff --git a/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php b/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php index 8971133e..b036c21b 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php @@ -75,6 +75,12 @@ class DocumentTest extends \PHPUnit_Framework_TestCase $section = $phpWord->addSection(); $section->addTitle('Title 2', 2); $section->addObject($objectSrc); + $section->addTextBox(array()); + $section->addTextBox(array('wrappingStyle' => 'square', 'positioning' => 'relative', + 'posHorizontalRel' => 'margin', 'posVerticalRel' => 'margin', + 'innerMargin' => 10, 'borderSize' => 1, 'borderColor' => '#FF0')); + $section->addTextBox(array('wrappingStyle' => 'tight', 'positioning' => 'absolute', 'align' => 'center')); + $doc = TestHelperDOCX::getDocument($phpWord); // TOC