PHPWord/Tests/PHPWordTest.php

172 lines
4.7 KiB
PHP
Raw Normal View History

2014-03-12 19:26:30 +07:00
<?php
namespace PHPWord\Tests;
use PhpOffice\PHPWord;
use PhpOffice\PhpWord\DocumentProperties;
use PhpOffice\PhpWord\Section;
use PhpOffice\PhpWord\Style;
2014-03-12 19:26:30 +07:00
/**
* @package PHPWord\Tests
* @coversDefaultClass PhpOffice\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
{
/**
* @var PhpOffice\PHPWord
2014-03-12 19:26:30 +07:00
*/
protected $object;
/**
* @covers ::__construct
* @covers ::getProperties
* @covers ::getDefaultFontName
* @covers ::getDefaultFontSize
2014-03-12 19:26:30 +07:00
*/
public function testConstruct()
{
$object = new PHPWord();
$this->assertEquals(new DocumentProperties(), $object->getProperties());
2014-03-12 19:26:30 +07:00
$this->assertEquals(
PHPWord::DEFAULT_FONT_NAME,
$object->getDefaultFontName()
);
$this->assertEquals(
PHPWord::DEFAULT_FONT_SIZE,
$object->getDefaultFontSize()
);
}
/**
* @covers ::setProperties
* @covers ::getProperties
2014-03-12 19:26:30 +07:00
*/
public function testSetGetProperties()
{
$object = new PHPWord();
$creator = 'PHPWord';
$properties = $object->getProperties();
$properties->setCreator($creator);
$object->setProperties($properties);
$this->assertEquals($creator, $object->getProperties()->getCreator());
}
/**
* @covers ::createSection
* @covers ::getSections
2014-03-12 19:26:30 +07:00
*/
public function testCreateGetSections()
{
$object = new PHPWord();
$this->assertEquals(new Section(1), $object->createSection());
2014-03-12 19:26:30 +07:00
$object->createSection();
$this->assertEquals(2, count($object->getSections()));
}
/**
* @covers ::setDefaultFontName
* @covers ::getDefaultFontName
2014-03-12 19:26:30 +07:00
*/
public function testSetGetDefaultFontName()
{
$object = new PHPWord();
$fontName = 'Times New Roman';
$this->assertEquals(
PHPWord::DEFAULT_FONT_NAME,
$object->getDefaultFontName()
);
$object->setDefaultFontName($fontName);
$this->assertEquals($fontName, $object->getDefaultFontName());
}
/**
* @covers ::setDefaultFontSize
* @covers ::getDefaultFontSize
2014-03-12 19:26:30 +07:00
*/
public function testSetGetDefaultFontSize()
{
$object = new PHPWord();
$fontSize = 16;
$this->assertEquals(
PHPWord::DEFAULT_FONT_SIZE,
$object->getDefaultFontSize()
);
$object->setDefaultFontSize($fontSize);
$this->assertEquals($fontSize, $object->getDefaultFontSize());
}
/**
* @covers ::setDefaultParagraphStyle
* @covers ::loadTemplate
2014-03-12 19:26:30 +07:00
*/
public function testSetDefaultParagraphStyle()
{
$object = new PHPWord();
$object->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()
{
$object = 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";
$object->$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()
{
$object = new PHPWord();
$titleLevel = 1;
$titleName = "Heading_{$titleLevel}";
$object->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()
{
$file = join(
DIRECTORY_SEPARATOR,
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'blank.docx')
);
$object = new PHPWord();
$this->assertInstanceOf('PhpOffice\\PhpWord\\Template', $object->loadTemplate($file));
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()
{
$file = join(
DIRECTORY_SEPARATOR,
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'blanks.docx')
);
$object = new PHPWord();
$object->loadTemplate($file);
}
}