PHPWord/tests/PhpWordTests/Element/SectionTest.php

224 lines
6.7 KiB
PHP
Raw Normal View History

2014-02-24 19:17:06 +01:00
<?php
2014-03-27 23:55:06 +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-27 23:55:06 +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-27 23:55:06 +07:00
*/
2022-09-16 14:09:17 +02:00
namespace PhpOffice\PhpWordTests\Element;
2014-02-24 19:17:06 +01:00
2022-09-16 11:45:45 +02:00
use Exception;
2022-09-16 14:09:17 +02:00
use PhpOffice\PhpWord\Element\Header;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style;
2022-09-16 14:09:17 +02:00
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Style\Section as SectionStyle;
2014-02-24 19:17:06 +01:00
/**
* @covers \PhpOffice\PhpWord\Element\Section
2022-09-16 11:45:45 +02:00
*
* @coversDefaultClass \PhpOffice\PhpWord\Element\Section
2022-09-16 11:45:45 +02:00
*
2014-03-13 01:58:00 +07:00
* @runTestsInSeparateProcesses
*/
class SectionTest extends \PHPUnit\Framework\TestCase
{
2022-09-16 11:45:45 +02:00
public function testConstructorWithDefaultStyle(): void
{
$section = new Section(0);
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Section', $section->getStyle());
}
2022-09-16 11:45:45 +02:00
public function testConstructorWithArrayStyle(): void
{
2022-09-16 11:45:45 +02:00
$section = new Section(0, ['orientation' => 'landscape']);
$style = $section->getStyle();
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Section', $style);
self::assertEquals('landscape', $style->getOrientation());
}
2022-09-16 11:45:45 +02:00
public function testConstructorWithObjectStyle(): void
{
$style = new SectionStyle();
$section = new Section(0, $style);
2022-09-16 11:45:45 +02:00
self::assertSame($style, $section->getStyle());
}
/**
* @covers ::setStyle
2014-03-13 01:58:00 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testSetStyle(): void
2014-03-13 01:58:00 +07:00
{
$expected = 'landscape';
$object = new Section(0);
2022-09-16 11:45:45 +02:00
$object->setStyle(['orientation' => $expected, 'foo' => null]);
self::assertEquals($expected, $object->getStyle()->getOrientation());
2014-03-13 01:58:00 +07:00
}
/**
* @coversNothing
2014-03-13 01:58:00 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddElements(): void
2014-03-13 01:58:00 +07:00
{
$objectSource = __DIR__ . '/../_files/documents/reader.docx';
$imageSource = __DIR__ . '/../_files/images/PhpWord.png';
2014-03-13 01:58:00 +07:00
$section = new Section(0);
$section->setPhpWord(new PhpWord());
2016-06-04 20:06:37 +04:00
$section->addText(utf8_decode('ä'));
$section->addLink(utf8_decode('http://äää.com'), utf8_decode('ä'));
2014-03-13 01:58:00 +07:00
$section->addTextBreak();
$section->addPageBreak();
$section->addTable();
2016-06-04 20:06:37 +04:00
$section->addListItem(utf8_decode('ä'));
2014-03-13 01:58:00 +07:00
$section->addObject($objectSource);
$section->addImage($imageSource);
2016-06-04 20:06:37 +04:00
$section->addTitle(utf8_decode('ä'), 1);
$section->addTextRun();
$section->addFootnote();
2016-06-04 20:06:37 +04:00
$section->addCheckBox(utf8_decode('chkä'), utf8_decode('Contentä'));
2014-03-28 13:50:53 +07:00
$section->addTOC();
$elementCollection = $section->getElements();
2022-09-16 11:45:45 +02:00
$elementTypes = [
'Text',
'Link',
'TextBreak',
'PageBreak',
'Table',
'ListItem',
'OLEObject',
'Image',
'Title',
'TextRun',
'Footnote',
'CheckBox',
'TOC',
2022-09-16 11:45:45 +02:00
];
$elmCount = 0;
2014-03-28 13:50:53 +07:00
foreach ($elementTypes as $elementType) {
2022-09-16 11:45:45 +02:00
self::assertInstanceOf("PhpOffice\\PhpWord\\Element\\{$elementType}", $elementCollection[$elmCount]);
++$elmCount;
2014-03-28 13:50:53 +07:00
}
}
/**
* @coversNothing
2014-03-28 13:50:53 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddObjectException(): void
2014-03-28 13:50:53 +07:00
{
$this->expectException(\PhpOffice\PhpWord\Exception\InvalidObjectException::class);
$source = __DIR__ . '/_files/xsl/passthrough.xsl';
2014-03-28 13:50:53 +07:00
$section = new Section(0);
$section->addObject($source);
}
2014-03-13 01:58:00 +07:00
2014-03-28 13:50:53 +07:00
/**
2022-09-16 11:45:45 +02:00
* Add title with predefined style.
*
* @coversNothing
2014-03-28 13:50:53 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddTitleWithStyle(): void
2014-03-28 13:50:53 +07:00
{
2022-09-16 11:45:45 +02:00
Style::addTitleStyle(1, ['size' => 14]);
2014-03-28 13:50:53 +07:00
$section = new Section(0);
$section->setPhpWord(new PhpWord());
2016-06-04 20:06:37 +04:00
$section->addTitle('Test', 1);
2014-03-13 01:58:00 +07:00
$elementCollection = $section->getElements();
2014-03-28 13:50:53 +07:00
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Title', $elementCollection[0]);
2014-03-13 01:58:00 +07:00
}
/**
* @covers ::addFooter
2022-09-16 11:45:45 +02:00
* @covers ::addHeader
* @covers ::hasDifferentFirstPage
2014-03-13 01:58:00 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddHeaderFooter(): void
2014-03-13 01:58:00 +07:00
{
$object = new Section(0);
2022-09-16 11:45:45 +02:00
$elements = ['Header', 'Footer'];
2014-03-28 13:50:53 +07:00
2014-03-13 01:58:00 +07:00
foreach ($elements as $element) {
$method = "add{$element}";
2022-09-16 11:45:45 +02:00
self::assertInstanceOf("PhpOffice\\PhpWord\\Element\\{$element}", $object->$method());
2014-03-13 01:58:00 +07:00
}
2022-09-16 11:45:45 +02:00
self::assertFalse($object->hasDifferentFirstPage());
2014-03-13 01:58:00 +07:00
}
2014-04-06 15:19:09 +07:00
/**
* @covers ::addHeader
* @covers ::hasDifferentFirstPage
*/
2022-09-16 11:45:45 +02:00
public function testHasDifferentFirstPageFooter(): void
{
$object = new Section(1);
$object->addFooter(Header::FIRST);
2022-09-16 11:45:45 +02:00
self::assertTrue($object->hasDifferentFirstPage());
}
2014-04-06 15:19:09 +07:00
/**
* @covers ::addHeader
* @covers ::hasDifferentFirstPage
2014-04-06 15:19:09 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testHasDifferentFirstPage(): void
2014-04-06 15:19:09 +07:00
{
$object = new Section(1);
$header = $object->addHeader();
$header->setType(Header::FIRST);
2022-09-16 11:45:45 +02:00
self::assertTrue($object->hasDifferentFirstPage());
2014-04-06 15:19:09 +07:00
}
/**
* @covers ::addHeader
2014-04-06 15:19:09 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddHeaderException(): void
2014-04-06 15:19:09 +07:00
{
2022-09-16 11:45:45 +02:00
$this->expectException(Exception::class);
$this->expectExceptionMessage('Invalid header/footer type.');
2014-04-06 15:19:09 +07:00
$object = new Section(1);
$object->addHeader('ODD');
2014-04-06 15:19:09 +07:00
}
/**
* @covers \PhpOffice\PhpWord\Element\AbstractContainer::removeElement
*/
2022-09-16 11:45:45 +02:00
public function testRemoveElementByIndex(): void
{
$section = new Section(1);
$section->addText('firstText');
$section->addText('secondText');
2022-09-16 11:45:45 +02:00
self::assertEquals(2, $section->countElements());
$section->removeElement(1);
2022-09-16 11:45:45 +02:00
self::assertEquals(1, $section->countElements());
}
/**
* @covers \PhpOffice\PhpWord\Element\AbstractContainer::removeElement
*/
2022-09-16 11:45:45 +02:00
public function testRemoveElementByElement(): void
{
$section = new Section(1);
$firstText = $section->addText('firstText');
$secondText = $section->addText('secondText');
2022-09-16 11:45:45 +02:00
self::assertEquals(2, $section->countElements());
$section->removeElement($firstText);
2022-09-16 11:45:45 +02:00
self::assertEquals(1, $section->countElements());
self::assertEquals($secondText->getElementId(), $section->getElement(1)->getElementId());
}
}