Some unit tests for the new features

This commit is contained in:
Ivan Lanin 2014-05-10 23:40:34 +07:00
parent 9ea767ea91
commit 516c13e032
3 changed files with 76 additions and 3 deletions

View File

@ -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;

View File

@ -0,0 +1,65 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Tests\Shared;
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Shared\Html;
/**
* Test class for PhpOffice\PhpWord\Shared\Html
*/
class HtmlTest extends \PHPUnit_Framework_TestCase
{
/**
* Test unit conversion functions with various numbers
*/
public function testAddHtml()
{
$content = '';
// Default
$section = new Section(1);
$this->assertEquals(0, $section->countElements());
// Heading
$styles = array('strong', 'em', 'sup', 'sub');
for ($level = 1; $level <= 6; $level++) {
$content .= "<h{$level}>Heading {$level}</h{$level}>";
}
// Styles
$content .= '<p style="text-decoration: underline; text-decoration: line-through; ' .
'text-align: center; color: #999; background-color: #000;">';
foreach ($styles as $style) {
$content .= "<{$style}>{$style}</{$style}>";
}
$content .= '</p>';
// Add HTML
Html::addHtml($section, $content);
$this->assertEquals(7, $section->countElements());
// Other parts
$section = new Section(1);
$content = '';
$content .= '<table><tr><th>Header</th><td>Content</td></tr></table>';
$content .= '<ul><li>Bullet</li></ul>';
$content .= '<ol><li>Bullet</li></ol>';
Html::addHtml($section, $content, null, array('listdepth' => 2));
}
}

View File

@ -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