PHPWord/tests/PhpWord/Tests/PhpWordTest.php

161 lines
4.7 KiB
PHP
Raw Normal View History

2014-03-12 19:26:30 +07:00
<?php
2014-03-26 17:21:23 +07:00
/**
* 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.
2014-03-26 17:21:23 +07:00
*
2014-03-27 23:55:06 +07:00
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
2014-05-04 21:03:28 +04:00
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
2014-03-26 17:21:23 +07:00
*/
namespace PhpOffice\PhpWord\Tests;
2014-03-12 19:26:30 +07:00
use PhpOffice\PhpWord\DocumentProperties;
2014-05-04 21:03:28 +04:00
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style;
2014-03-12 19:26:30 +07:00
/**
2014-03-27 23:55:06 +07:00
* Test class for PhpOffice\PhpWord\PhpWord
2014-03-26 17:21:23 +07:00
*
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
{
/**
2014-03-26 17:21:23 +07:00
* Test object creation
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
}
/**
2014-03-26 17:21:23 +07:00
* Test set/get document properties
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
}
/**
2014-03-26 17:21:23 +07:00
* Test create/get section
2014-03-12 19:26:30 +07:00
*/
public function testCreateGetSections()
{
$phpWord = new PhpWord();
$phpWord->addSection();
$this->assertEquals(1, count($phpWord->getSections()));
2014-03-12 19:26:30 +07:00
}
/**
2014-03-26 17:21:23 +07:00
* Test set/get default font name
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
}
/**
2014-03-26 17:21:23 +07:00
* Test set/get default font size
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
}
/**
2014-03-26 17:21:23 +07:00
* Test set default paragraph style
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
}
/**
2014-03-26 17:21:23 +07:00
* Test add styles
2014-03-12 19:26:30 +07:00
*/
public function testAddStyles()
{
$phpWord = new PhpWord();
$styles = array(
'Paragraph' => 'Paragraph',
'Font' => 'Font',
2014-03-24 11:33:20 +07:00
'Table' => 'Table',
'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
}
}
/**
2014-03-26 17:21:23 +07:00
* Test add title style
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
}
/**
2014-03-26 17:21:23 +07:00
* Test load template
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
}
/**
2014-03-26 17:21:23 +07:00
* Test load template exception
*
* @expectedException \PhpOffice\PhpWord\Exception\Exception
2014-03-12 19:26:30 +07:00
*/
public function testLoadTemplateException()
{
$templateFqfn = join(
DIRECTORY_SEPARATOR,
array(PHPWORD_TESTS_BASE_DIR, 'PhpWord', 'Tests', '_files', '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
}
}