More unit tests

This commit is contained in:
Ivan Lanin 2014-03-14 04:25:09 +07:00
parent b3458b9c0a
commit b11cd40bba
5 changed files with 109 additions and 50 deletions

View File

@ -1,7 +1,6 @@
<?php <?php
namespace PHPWord\Tests; namespace PHPWord\Tests;
use PHPUnit_Framework_TestCase;
use PHPWord_Style; use PHPWord_Style;
/** /**
@ -11,7 +10,7 @@ use PHPWord_Style;
* @covers PHPWord_Style * @covers PHPWord_Style
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class StyleTest extends PHPUnit_Framework_TestCase class StyleTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @covers PHPWord_Style::addParagraphStyle * @covers PHPWord_Style::addParagraphStyle

View File

@ -21,6 +21,9 @@ class BaseTest extends \PHPUnit_Framework_TestCase
TestHelperDOCX::clear(); TestHelperDOCX::clear();
} }
/**
* covers ::_writeText
*/
public function testWriteText() public function testWriteText()
{ {
$rStyle = 'rStyle'; $rStyle = 'rStyle';
@ -40,7 +43,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* Write text run * covers ::_writeTextRun
*/ */
public function testWriteTextRun() public function testWriteTextRun()
{ {
@ -67,7 +70,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* Write link * covers ::_writeLink
*/ */
public function testWriteLink() public function testWriteLink()
{ {
@ -84,7 +87,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* Write preserve text * covers ::_writePreserveText
*/ */
public function testWritePreserveText() public function testWritePreserveText()
{ {
@ -102,7 +105,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* Write paragraph style: Alignment * covers ::_writeParagraphStyle
*/ */
public function testWriteParagraphStyleAlign() public function testWriteParagraphStyleAlign()
{ {
@ -118,7 +121,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* Write paragraph style: Pagination * covers ::_writeParagraphStyle
*/ */
public function testWriteParagraphStylePagination() public function testWriteParagraphStylePagination()
{ {
@ -180,7 +183,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* Write table * covers ::_writeTableStyle
*/ */
public function testWriteTableStyle() public function testWriteTableStyle()
{ {
@ -238,7 +241,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* Write cell style * covers ::_writeCellStyle
*/ */
public function testWriteCellStyleCellGridSpan() public function testWriteCellStyleCellGridSpan()
{ {
@ -265,7 +268,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* Write image * covers ::_writeImage
*/ */
public function testWriteImagePosition() public function testWriteImagePosition()
{ {
@ -290,7 +293,27 @@ class BaseTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* Write title * covers ::_writeWatermark
*/
public function testWriteWatermark()
{
$imageSrc = join(
DIRECTORY_SEPARATOR,
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'images', 'earth.jpg')
);
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$header = $section->createHeader();
$header->addWatermark($imageSrc);
$doc = TestHelperDOCX::getDocument($PHPWord);
$element = $doc->getElement("/w:document/w:body/w:sectPr/w:headerReference");
$this->assertStringStartsWith("rId", $element->getAttribute('r:id'));
}
/**
* covers ::_writeTitle
*/ */
public function testWriteTitle() public function testWriteTitle()
{ {

View File

@ -32,4 +32,56 @@ class DocumentTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(2, $element->getAttribute('w:start')); $this->assertEquals(2, $element->getAttribute('w:start'));
} }
/**
* covers ::_writeTOC
* covers ::_writePageBreak
* covers ::_writeListItem
* covers ::_writeTitle
* covers ::_writeObject
*/
public function testElements()
{
$objectSrc = join(
DIRECTORY_SEPARATOR,
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'documents', 'sheet.xls')
);
$PHPWord = new PHPWord();
$PHPWord->addTitleStyle(1, array('color' => '333333', 'bold'=>true));
$PHPWord->addTitleStyle(2, array('color'=>'666666'));
$section = $PHPWord->createSection();
$section->addTOC();
$section->addPageBreak();
$section->addTitle('Title 1', 1);
$section->addListItem('List Item 1', 0);
$section->addListItem('List Item 2', 0);
$section->addListItem('List Item 3', 0);
$section = $PHPWord->createSection();
$section->addTitle('Title 2', 2);
$section->addObject($objectSrc);
$doc = TestHelperDOCX::getDocument($PHPWord);
// TOC
$element = $doc->getElement('/w:document/w:body/w:p[1]/w:pPr/w:tabs/w:tab');
$this->assertEquals('right', $element->getAttribute('w:val'));
$this->assertEquals('dot', $element->getAttribute('w:leader'));
$this->assertEquals(9062, $element->getAttribute('w:pos'));
// Page break
$element = $doc->getElement('/w:document/w:body/w:p[4]/w:r/w:br');
$this->assertEquals('page', $element->getAttribute('w:type'));
// Title
$element = $doc->getElement('/w:document/w:body/w:p[5]/w:pPr/w:pStyle');
$this->assertEquals('Heading1', $element->getAttribute('w:val'));
// List item
$element = $doc->getElement('/w:document/w:body/w:p[6]/w:pPr/w:numPr/w:numId');
$this->assertEquals(3, $element->getAttribute('w:val'));
// Object
$element = $doc->getElement('/w:document/w:body/w:p[11]/w:r/w:object/o:OLEObject');
$this->assertEquals('Embed', $element->getAttribute('Type'));
}
} }

View File

@ -26,13 +26,31 @@ class StylesTest extends \PHPUnit_Framework_TestCase
{ {
$PHPWord = new PHPWord(); $PHPWord = new PHPWord();
$defaultStyle = array('align' => 'both'); $pStyle = array('align' => 'both');
$baseStyle = array('basedOn' => 'Normal'); $pBase = array('basedOn' => 'Normal');
$newStyle = array('basedOn' => 'Base Style', 'next' => 'Normal'); $pNew = array('basedOn' => 'Base Style', 'next' => 'Normal');
$PHPWord->setDefaultParagraphStyle($defaultStyle); $rStyle = array('size' => 20);
$PHPWord->addParagraphStyle('Base Style', $baseStyle); $tStyle = array(
$PHPWord->addParagraphStyle('New Style', $newStyle); 'bgColor' => 'FF0000',
'cellMarginTop' => 120,
'cellMarginBottom' => 120,
'cellMarginLeft' => 120,
'cellMarginRight' => 120,
'borderTopSize' => 120,
'borderBottomSize' => 120,
'borderLeftSize' => 120,
'borderRightSize' => 120,
'borderInsideHSize' => 120,
'borderInsideVSize' => 120,
);
$PHPWord->setDefaultParagraphStyle($pStyle);
$PHPWord->addParagraphStyle('Base Style', $pBase);
$PHPWord->addParagraphStyle('New Style', $pNew);
$PHPWord->addFontStyle('New Style', $rStyle, $pStyle);
$PHPWord->addTableStyle('Table Style', $tStyle, $tStyle);
$PHPWord->addTitleStyle(1, $rStyle, $pStyle);
$doc = TestHelperDOCX::getDocument($PHPWord); $doc = TestHelperDOCX::getDocument($PHPWord);
$file = 'word/styles.xml'; $file = 'word/styles.xml';
// Normal style generated? // Normal style generated?

View File

@ -1,33 +0,0 @@
<?php
/**
* Generic template for creating PHPWord samples
*/
// Init
error_reporting(E_ALL);
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
require_once '../Classes/PHPWord.php';
// New Word document
echo date('H:i:s'), " Create new PHPWord object", EOL;
$PHPWord = new PHPWord();
// Begin code
// End code
// Save file
$name = basename(__FILE__, '.php');
$writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf');
foreach ($writers as $writer => $extension) {
echo date('H:i:s'), " Write to {$writer} format", EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, $writer);
$objWriter->save("{$name}.{$extension}");
rename("{$name}.{$extension}", "results/{$name}.{$extension}");
}
// Done
echo date('H:i:s'), " Done writing file(s)", EOL;
echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL;