PHPWord/tests/PhpWord/Element/SectionTest.php

190 lines
5.6 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
*
* @link https://github.com/PHPOffice/PHPWord
2015-12-05 21:25:59 +04:00
* @copyright 2010-2015 PHPWord contributors
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
*/
2015-11-15 13:33:05 +04:00
namespace PhpOffice\PhpWord\Element;
2014-02-24 19:17:06 +01:00
2014-05-04 21:03:28 +04:00
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style;
2014-02-24 19:17:06 +01:00
/**
* Test class for PhpOffice\PhpWord\Element\Section
2014-03-27 23:55:06 +07:00
*
2014-03-13 01:58:00 +07:00
* @runTestsInSeparateProcesses
*/
class SectionTest extends \PHPUnit_Framework_TestCase
{
/**
* Get style
*/
public function testGetStyle()
{
$oSection = new Section(0);
$this->assertAttributeEquals($oSection->getStyle(), 'style', $oSection);
}
2014-02-24 19:17:06 +01:00
/**
2014-03-28 13:50:53 +07:00
* Get elements
*/
public function testGetElements()
{
$oSection = new Section(0);
$this->assertAttributeEquals($oSection->getElements(), 'elements', $oSection);
}
2014-02-24 19:17:06 +01:00
/**
2014-03-28 13:50:53 +07:00
* Get footer
*/
2014-04-05 19:02:49 +07:00
public function testGetFooters()
{
$oSection = new Section(0);
$this->assertAttributeEquals($oSection->getFooters(), 'footers', $oSection);
}
2014-02-24 19:17:06 +01:00
/**
2014-03-28 13:50:53 +07:00
* Get headers
*/
public function testGetHeaders()
{
$oSection = new Section(0);
$this->assertAttributeEquals($oSection->getHeaders(), 'headers', $oSection);
}
2014-03-13 01:58:00 +07:00
/**
2014-03-28 13:50:53 +07:00
* Set settings
2014-03-13 01:58:00 +07:00
*/
public function testSetStyle()
2014-03-13 01:58:00 +07:00
{
$expected = 'landscape';
$object = new Section(0);
$object->setStyle(array('orientation' => $expected, 'foo' => null));
$this->assertEquals($expected, $object->getStyle()->getOrientation());
2014-03-13 01:58:00 +07:00
}
/**
2014-03-28 13:50:53 +07:00
* Add elements
2014-03-13 01:58:00 +07:00
*/
public function testAddElements()
{
$objectSource = __DIR__ . '/../_files/documents/reader.docx';
$imageSource = __DIR__ . '/../_files/images/PhpWord.png';
// $imageUrl = 'http://php.net//images/logos/php-med-trans-light.gif';
2014-03-13 01:58:00 +07:00
$section = new Section(0);
$section->setPhpWord(new PhpWord());
$section->addText(utf8_decode(htmlspecialchars('ä', ENT_COMPAT, 'UTF-8')));
$section->addLink(utf8_decode('http://äää.com'), utf8_decode(htmlspecialchars('ä', ENT_COMPAT, 'UTF-8')));
2014-03-13 01:58:00 +07:00
$section->addTextBreak();
$section->addPageBreak();
$section->addTable();
$section->addListItem(utf8_decode(htmlspecialchars('ä', ENT_COMPAT, 'UTF-8')));
2014-03-13 01:58:00 +07:00
$section->addObject($objectSource);
$section->addImage($imageSource);
$section->addTitle(utf8_decode(htmlspecialchars('ä', ENT_COMPAT, 'UTF-8')), 1);
$section->addTextRun();
$section->addFootnote();
$section->addCheckBox(utf8_decode(htmlspecialchars('chkä', ENT_COMPAT, 'UTF-8')), utf8_decode(htmlspecialchars('Contentä', ENT_COMPAT, 'UTF-8')));
2014-03-28 13:50:53 +07:00
$section->addTOC();
$elementCollection = $section->getElements();
$elementTypes = array(
'Text',
'Link',
'TextBreak',
'PageBreak',
'Table',
'ListItem',
'Object',
'Image',
'Title',
'TextRun',
'Footnote',
'CheckBox',
'TOC',
);
$elmCount = 0;
2014-03-28 13:50:53 +07:00
foreach ($elementTypes as $elementType) {
$this->assertInstanceOf("PhpOffice\\PhpWord\\Element\\{$elementType}", $elementCollection[$elmCount]);
$elmCount++;
2014-03-28 13:50:53 +07:00
}
}
/**
* Test add object exception
*
* @expectedException \PhpOffice\PhpWord\Exception\InvalidObjectException
2014-03-28 13:50:53 +07:00
*/
public function testAddObjectException()
{
$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
/**
* Add title with predefined style
*/
public function testAddTitleWithStyle()
{
Style::addTitleStyle(1, array('size' => 14));
$section = new Section(0);
$section->setPhpWord(new PhpWord());
$section->addTitle(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8'), 1);
2014-03-13 01:58:00 +07:00
$elementCollection = $section->getElements();
2014-03-28 13:50:53 +07:00
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Title', $elementCollection[0]);
2014-03-13 01:58:00 +07:00
}
/**
* Add header footer
2014-03-13 01:58:00 +07:00
*/
public function testAddHeaderFooter()
2014-03-13 01:58:00 +07:00
{
$object = new Section(0);
2014-03-13 01:58:00 +07:00
$elements = array('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}";
$this->assertInstanceOf("PhpOffice\\PhpWord\\Element\\{$element}", $object->$method());
2014-03-13 01:58:00 +07:00
}
2014-03-28 13:50:53 +07:00
$this->assertFalse($object->hasDifferentFirstPage());
2014-03-13 01:58:00 +07:00
}
2014-04-06 15:19:09 +07:00
/**
* Add header has different first page
*/
public function testHasDifferentFirstPage()
{
$object = new Section(1);
$header = $object->addHeader();
$header->setType(Header::FIRST);
$this->assertTrue($object->hasDifferentFirstPage());
}
/**
* Add header exception
*
* @expectedException Exception
* @expectedExceptionMessage Invalid header/footer type.
2014-04-06 15:19:09 +07:00
*/
public function testAddHeaderException()
{
$object = new Section(1);
$object->addHeader('ODD');
2014-04-06 15:19:09 +07:00
}
}