Skip to content

Commit 516c13e

Browse files
committed
Some unit tests for the new features
1 parent 9ea767e commit 516c13e

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

src/PhpWord/Shared/Html.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function addHtml($object, $html)
3737
* @todo parse $stylesheet for default styles. Should result in an array based on id, class and element,
3838
* which could be applied when such an element occurs in the parseNode function.
3939
*/
40-
$html = str_replace(array("\n","\r"), '', $html);
40+
$html = str_replace(array("\n", "\r"), '', $html);
4141

4242
$dom = new \DOMDocument();
4343
$dom->preserveWhiteSpace = true;
@@ -102,7 +102,7 @@ protected static function parseInlineStyle($node, $style = array())
102102
* @param \DOMNode $node node to parse
103103
* @param \PhpOffice\PhpWord\Element\AbstractElement $object object to add an element corresponding with the node
104104
* @param array $styles Array with all styles
105-
* @param $data array to transport data to a next level in the DOM tree, for example level of listitems
105+
* @param array $data Array to transport data to a next level in the DOM tree, for example level of listitems
106106
*
107107
*/
108108
protected static function parseNode(
@@ -148,7 +148,9 @@ protected static function parseNode(
148148
break;
149149
case '#text':
150150
$styles['fontStyle'] = self::parseInlineStyle($node, $styles['fontStyle']);
151-
$object->AddText($node->nodeValue, $styles['fontStyle'], $styles['paragraphStyle']);
151+
if (method_exists($object, 'addText')) {
152+
$object->addText($node->nodeValue, $styles['fontStyle'], $styles['paragraphStyle']);
153+
}
152154
break;
153155
case 'strong':
154156
$styles['fontStyle']['bold'] = true;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2014 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Tests\Shared;
19+
20+
use PhpOffice\PhpWord\Element\Section;
21+
use PhpOffice\PhpWord\Shared\Html;
22+
23+
/**
24+
* Test class for PhpOffice\PhpWord\Shared\Html
25+
*/
26+
class HtmlTest extends \PHPUnit_Framework_TestCase
27+
{
28+
/**
29+
* Test unit conversion functions with various numbers
30+
*/
31+
public function testAddHtml()
32+
{
33+
$content = '';
34+
35+
// Default
36+
$section = new Section(1);
37+
$this->assertEquals(0, $section->countElements());
38+
39+
// Heading
40+
$styles = array('strong', 'em', 'sup', 'sub');
41+
for ($level = 1; $level <= 6; $level++) {
42+
$content .= "<h{$level}>Heading {$level}</h{$level}>";
43+
}
44+
45+
// Styles
46+
$content .= '<p style="text-decoration: underline; text-decoration: line-through; ' .
47+
'text-align: center; color: #999; background-color: #000;">';
48+
foreach ($styles as $style) {
49+
$content .= "<{$style}>{$style}</{$style}>";
50+
}
51+
$content .= '</p>';
52+
53+
// Add HTML
54+
Html::addHtml($section, $content);
55+
$this->assertEquals(7, $section->countElements());
56+
57+
// Other parts
58+
$section = new Section(1);
59+
$content = '';
60+
$content .= '<table><tr><th>Header</th><td>Content</td></tr></table>';
61+
$content .= '<ul><li>Bullet</li></ul>';
62+
$content .= '<ol><li>Bullet</li></ol>';
63+
Html::addHtml($section, $content, null, array('listdepth' => 2));
64+
}
65+
}

tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ public function testElements()
7575
$section = $phpWord->addSection();
7676
$section->addTitle('Title 2', 2);
7777
$section->addObject($objectSrc);
78+
$section->addTextBox(array());
79+
$section->addTextBox(array('wrappingStyle' => 'square', 'positioning' => 'relative',
80+
'posHorizontalRel' => 'margin', 'posVerticalRel' => 'margin',
81+
'innerMargin' => 10, 'borderSize' => 1, 'borderColor' => '#FF0'));
82+
$section->addTextBox(array('wrappingStyle' => 'tight', 'positioning' => 'absolute', 'align' => 'center'));
83+
7884
$doc = TestHelperDOCX::getDocument($phpWord);
7985

8086
// TOC

0 commit comments

Comments
 (0)