PHPWord/test/PhpWord/Tests/PhpWordTest.php

156 lines
4.5 KiB
PHP
Raw Normal View History

2014-03-12 19:26:30 +07:00
<?php
namespace PhpOffice\PhpWord\Tests;
2014-03-12 19:26:30 +07:00
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\DocumentProperties;
use PhpOffice\PhpWord\Section;
use PhpOffice\PhpWord\Style;
2014-03-12 19:26:30 +07:00
/**
* @coversDefaultClass \PhpOffice\PhpWord\PhpWord
2014-03-13 01:58:00 +07:00
* @runTestsInSeparateProcesses
2014-03-12 19:26:30 +07:00
*/
class PhpWordTest extends \PHPUnit_Framework_TestCase
2014-03-12 19:26:30 +07:00
{
/**
* @covers ::__construct
* @covers ::getDocumentProperties
* @covers ::getDefaultFontName
* @covers ::getDefaultFontSize
2014-03-12 19:26:30 +07:00
*/
public function testConstruct()
{
$phpWord = new PhpWord();
$this->assertEquals(new DocumentProperties(), $phpWord->getDocumentProperties());
$this->assertEquals(PhpWord::DEFAULT_FONT_NAME, $phpWord->getDefaultFontName());
$this->assertEquals(PhpWord::DEFAULT_FONT_SIZE, $phpWord->getDefaultFontSize());
2014-03-12 19:26:30 +07:00
}
/**
* @covers ::setDocumentProperties
* @covers ::getDocumentProperties
2014-03-12 19:26:30 +07:00
*/
public function testSetGetDocumentProperties()
2014-03-12 19:26:30 +07:00
{
$phpWord = new PhpWord();
$creator = 'PhpWord';
$properties = $phpWord->getDocumentProperties();
2014-03-12 19:26:30 +07:00
$properties->setCreator($creator);
$phpWord->setDocumentProperties($properties);
$this->assertEquals($creator, $phpWord->getDocumentProperties()->getCreator());
2014-03-12 19:26:30 +07:00
}
/**
* @covers ::createSection
* @covers ::getSections
2014-03-12 19:26:30 +07:00
*/
public function testCreateGetSections()
{
$phpWord = new PhpWord();
$this->assertEquals(new Section(1), $phpWord->createSection());
$phpWord->createSection();
$this->assertEquals(2, \count($phpWord->getSections()));
2014-03-12 19:26:30 +07:00
}
/**
* @covers ::setDefaultFontName
* @covers ::getDefaultFontName
2014-03-12 19:26:30 +07:00
*/
public function testSetGetDefaultFontName()
{
$phpWord = new PhpWord();
2014-03-12 19:26:30 +07:00
$fontName = 'Times New Roman';
$this->assertEquals(PhpWord::DEFAULT_FONT_NAME, $phpWord->getDefaultFontName());
$phpWord->setDefaultFontName($fontName);
$this->assertEquals($fontName, $phpWord->getDefaultFontName());
2014-03-12 19:26:30 +07:00
}
/**
* @covers ::setDefaultFontSize
* @covers ::getDefaultFontSize
2014-03-12 19:26:30 +07:00
*/
public function testSetGetDefaultFontSize()
{
$phpWord = new PhpWord();
2014-03-12 19:26:30 +07:00
$fontSize = 16;
$this->assertEquals(PhpWord::DEFAULT_FONT_SIZE, $phpWord->getDefaultFontSize());
$phpWord->setDefaultFontSize($fontSize);
$this->assertEquals($fontSize, $phpWord->getDefaultFontSize());
2014-03-12 19:26:30 +07:00
}
/**
* @covers ::setDefaultParagraphStyle
* @covers ::loadTemplate
2014-03-12 19:26:30 +07:00
*/
public function testSetDefaultParagraphStyle()
{
$phpWord = new PhpWord();
$phpWord->setDefaultParagraphStyle(array());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', Style::getStyle('Normal'));
2014-03-12 19:26:30 +07:00
}
/**
* @covers ::addParagraphStyle
* @covers ::addFontStyle
* @covers ::addTableStyle
* @covers ::addLinkStyle
2014-03-12 19:26:30 +07:00
*/
public function testAddStyles()
{
$phpWord = new PhpWord();
$styles = array(
'Paragraph' => 'Paragraph',
'Font' => 'Font',
'Table' => 'TableFull',
'Link' => 'Font',
);
2014-03-12 19:26:30 +07:00
foreach ($styles as $key => $value) {
$method = "add{$key}Style";
$styleId = "{$key} Style";
$phpWord->$method($styleId, array());
$this->assertInstanceOf("PhpOffice\\PhpWord\\Style\\{$value}", Style::getStyle($styleId));
2014-03-12 19:26:30 +07:00
}
}
/**
* @covers ::addTitleStyle
2014-03-12 19:26:30 +07:00
*/
public function testAddTitleStyle()
{
$phpWord = new PhpWord();
2014-03-12 19:26:30 +07:00
$titleLevel = 1;
$titleName = "Heading_{$titleLevel}";
$phpWord->addTitleStyle($titleLevel, array());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', Style::getStyle($titleName));
2014-03-12 19:26:30 +07:00
}
/**
* @covers ::loadTemplate
2014-03-12 19:26:30 +07:00
*/
public function testLoadTemplate()
{
2014-03-23 12:37:31 -04:00
$templateFqfn = __DIR__ . "/_files/templates/blank.docx";
$phpWord = new PhpWord();
$this->assertInstanceOf(
'PhpOffice\\PhpWord\\Template',
$phpWord->loadTemplate($templateFqfn)
);
2014-03-12 19:26:30 +07:00
}
/**
* @covers ::loadTemplate
* @expectedException \PhpOffice\PhpWord\Exceptions\Exception
2014-03-12 19:26:30 +07:00
*/
public function testLoadTemplateException()
{
$templateFqfn = \join(
\DIRECTORY_SEPARATOR,
array(\PHPWORD_TESTS_BASE_DIR, 'data', 'templates', 'blanks.docx')
2014-03-12 19:26:30 +07:00
);
$phpWord = new PhpWord();
$phpWord->loadTemplate($templateFqfn);
2014-03-12 19:26:30 +07:00
}
}