PHPWord/tests/PhpWordTests/PhpWordTest.php

208 lines
5.9 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
*
* @see https://github.com/PHPOffice/PHPWord
2022-09-16 11:45:45 +02:00
*
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
*/
2022-09-16 14:09:17 +02:00
namespace PhpOffice\PhpWordTests;
2014-03-12 19:26:30 +07:00
2022-09-16 11:45:45 +02:00
use BadMethodCallException;
use PhpOffice\PhpWord\Metadata\DocInfo;
2022-09-16 14:09:17 +02:00
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Style;
2014-03-12 19:26:30 +07:00
/**
2022-09-16 11:45:45 +02: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
{
/**
2022-09-16 11:45:45 +02:00
* Test object creation.
2014-03-12 19:26:30 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testConstruct(): void
2014-03-12 19:26:30 +07:00
{
$phpWord = new PhpWord();
2022-09-16 11:45:45 +02:00
self::assertEquals(new DocInfo(), $phpWord->getDocInfo());
self::assertEquals(Settings::DEFAULT_FONT_NAME, $phpWord->getDefaultFontName());
self::assertEquals(Settings::DEFAULT_FONT_SIZE, $phpWord->getDefaultFontSize());
2014-03-12 19:26:30 +07:00
}
/**
2022-09-16 11:45:45 +02:00
* Test create/get section.
2014-03-12 19:26:30 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testCreateGetSections(): void
2014-03-12 19:26:30 +07:00
{
$phpWord = new PhpWord();
$phpWord->addSection();
2022-09-16 11:45:45 +02:00
self::assertCount(1, $phpWord->getSections());
2014-03-12 19:26:30 +07:00
}
/**
2022-09-16 11:45:45 +02:00
* Test set/get default font name.
2014-03-12 19:26:30 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testSetGetDefaultFontName(): void
2014-03-12 19:26:30 +07:00
{
$phpWord = new PhpWord();
2014-03-12 19:26:30 +07:00
$fontName = 'Times New Roman';
2022-09-16 11:45:45 +02:00
self::assertEquals(Settings::DEFAULT_FONT_NAME, $phpWord->getDefaultFontName());
$phpWord->setDefaultFontName($fontName);
2022-09-16 11:45:45 +02:00
self::assertEquals($fontName, $phpWord->getDefaultFontName());
2014-03-12 19:26:30 +07:00
}
/**
2022-09-16 11:45:45 +02:00
* Test set/get default font size.
2014-03-12 19:26:30 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testSetGetDefaultFontSize(): void
2014-03-12 19:26:30 +07:00
{
$phpWord = new PhpWord();
2014-03-12 19:26:30 +07:00
$fontSize = 16;
2022-09-16 11:45:45 +02:00
self::assertEquals(Settings::DEFAULT_FONT_SIZE, $phpWord->getDefaultFontSize());
$phpWord->setDefaultFontSize($fontSize);
2022-09-16 11:45:45 +02:00
self::assertEquals($fontSize, $phpWord->getDefaultFontSize());
2014-03-12 19:26:30 +07:00
}
/**
2022-09-16 11:45:45 +02:00
* Test set default paragraph style.
2014-03-12 19:26:30 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testSetDefaultParagraphStyle(): void
2014-03-12 19:26:30 +07:00
{
$phpWord = new PhpWord();
2022-09-16 11:45:45 +02:00
$phpWord->setDefaultParagraphStyle([]);
self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', Style::getStyle('Normal'));
2014-03-12 19:26:30 +07:00
}
/**
2022-09-16 11:45:45 +02:00
* Test add styles.
2014-03-12 19:26:30 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddStyles(): void
2014-03-12 19:26:30 +07:00
{
$phpWord = new PhpWord();
2022-09-16 11:45:45 +02:00
$styles = [
'Paragraph' => 'Paragraph',
2022-09-16 11:45:45 +02:00
'Font' => 'Font',
'Table' => 'Table',
'Link' => 'Font',
];
2014-03-12 19:26:30 +07:00
foreach ($styles as $key => $value) {
$method = "add{$key}Style";
$styleId = "{$key} Style";
2022-09-16 11:45:45 +02:00
$phpWord->$method($styleId, []);
self::assertInstanceOf("PhpOffice\\PhpWord\\Style\\{$value}", Style::getStyle($styleId));
2014-03-12 19:26:30 +07:00
}
}
/**
2022-09-16 11:45:45 +02:00
* Test add title style.
2014-03-12 19:26:30 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddTitleStyle(): void
2014-03-12 19:26:30 +07:00
{
$phpWord = new PhpWord();
2014-03-12 19:26:30 +07:00
$titleLevel = 1;
$titleName = "Heading_{$titleLevel}";
2022-09-16 11:45:45 +02:00
$phpWord->addTitleStyle($titleLevel, []);
self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', Style::getStyle($titleName));
2014-03-12 19:26:30 +07:00
}
/**
2022-09-16 11:45:45 +02:00
* Test save.
*/
2022-09-16 11:45:45 +02:00
public function testSave(): void
{
2022-09-16 11:45:45 +02:00
$this->setOutputCallback(function (): void {
});
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addText('Hello world!');
2022-09-16 11:45:45 +02:00
self::assertTrue($phpWord->save('test.docx', 'Word2007', true));
}
2014-06-29 14:10:49 +07:00
/**
2022-09-16 11:45:45 +02:00
* Test calling undefined method.
2014-06-29 14:10:49 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testCallUndefinedMethod(): void
2014-06-29 14:10:49 +07:00
{
2022-09-16 11:45:45 +02:00
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('is not defined');
2014-06-29 14:10:49 +07:00
$phpWord = new PhpWord();
$phpWord->undefinedMethod();
}
/**
* @covers \PhpOffice\PhpWord\PhpWord::getSection
*/
2022-09-16 11:45:45 +02:00
public function testGetNotExistingSection(): void
{
$phpWord = new PhpWord();
$section = $phpWord->getSection(0);
2022-09-16 11:45:45 +02:00
self::assertNull($section);
}
/**
* @covers \PhpOffice\PhpWord\PhpWord::getSection
*/
2022-09-16 11:45:45 +02:00
public function testGetSection(): void
{
$phpWord = new PhpWord();
$phpWord->addSection();
$section = $phpWord->getSection(0);
2022-09-16 11:45:45 +02:00
self::assertNotNull($section);
}
/**
* @covers \PhpOffice\PhpWord\PhpWord::sortSections
*/
2022-09-16 11:45:45 +02:00
public function testSortSections(): void
{
$phpWord = new PhpWord();
$section1 = $phpWord->addSection();
$section1->addText('test1');
$section2 = $phpWord->addSection();
$section2->addText('test2');
$section2->addText('test3');
2022-09-16 11:45:45 +02:00
self::assertEquals(1, $phpWord->getSection(0)->countElements());
self::assertEquals(2, $phpWord->getSection(1)->countElements());
$phpWord->sortSections(function ($a, $b) {
$numElementsInA = $a->countElements();
$numElementsInB = $b->countElements();
if ($numElementsInA === $numElementsInB) {
return 0;
} elseif ($numElementsInA > $numElementsInB) {
return -1;
}
return 1;
});
2022-09-16 11:45:45 +02:00
self::assertEquals(2, $phpWord->getSection(0)->countElements());
self::assertEquals(1, $phpWord->getSection(1)->countElements());
}
2019-10-23 13:44:47 +05:00
/**
* @covers \PhpOffice\PhpWord\PhpWord::getSettings
*/
2022-09-16 11:45:45 +02:00
public function testGetSettings(): void
2019-10-23 13:44:47 +05:00
{
$phpWord = new PhpWord();
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Metadata\\Settings', $phpWord->getSettings());
2019-10-23 13:44:47 +05:00
}
}