PHPWord/tests/PhpWord/Tests/Element/SectionTest.php

172 lines
4.8 KiB
PHP
Raw Normal View History

2014-02-24 19:17:06 +01:00
<?php
2014-03-27 23:55:06 +07:00
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/lgpl.txt LGPL
2014-03-27 23:55:06 +07:00
*/
namespace PhpOffice\PhpWord\Tests\Element;
2014-02-24 19:17:06 +01:00
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style;
2014-04-06 15:19:09 +07:00
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Element\Header;
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
{
/**
2014-03-28 13:50:53 +07:00
* Get settings
*/
public function testGetSettings()
{
$oSection = new Section(0);
2014-03-31 23:10:51 +07:00
$this->assertAttributeEquals($oSection->getSettings(), 'settings', new Section(0));
}
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);
2014-03-31 23:10:51 +07:00
$this->assertAttributeEquals($oSection->getElements(), 'elements', new Section(0));
}
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);
2014-04-05 19:02:49 +07:00
$this->assertAttributeEquals($oSection->getFooters(), 'footers', new Section(0));
}
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);
2014-03-31 23:10:51 +07:00
$this->assertAttributeEquals($oSection->getHeaders(), 'headers', new Section(0));
}
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 testSetSettings()
{
$expected = 'landscape';
$object = new Section(0);
2014-03-13 01:58:00 +07:00
$object->setSettings(array('orientation' => $expected));
$this->assertEquals($expected, $object->getSettings()->getOrientation());
}
/**
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";
2014-03-13 01:58:00 +07:00
$imageUrl = 'http://php.net//images/logos/php-med-trans-light.gif';
$section = new Section(0);
$section->setPhpWord(new PhpWord());
2014-03-13 01:58:00 +07:00
$section->addText(utf8_decode('ä'));
$section->addLink(utf8_decode('http://äää.com'), utf8_decode('ä'));
$section->addTextBreak();
$section->addPageBreak();
$section->addTable();
$section->addListItem(utf8_decode('ä'));
$section->addObject($objectSource);
$section->addImage($imageSource);
$section->addTitle(utf8_decode('ä'), 1);
$section->addTextRun();
$section->addFootnote();
2014-03-30 11:09:14 +07:00
$section->addCheckBox(utf8_decode('chkä'), utf8_decode('Contentä'));
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');
2014-03-28 13:50:53 +07:00
$i = 0;
foreach ($elementTypes as $elementType) {
$this->assertInstanceOf("PhpOffice\\PhpWord\\Element\\{$elementType}", $elementCollection[$i]);
2014-03-28 13:50:53 +07:00
$i++;
}
}
/**
* 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";
$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());
2014-03-28 13:50:53 +07:00
$section->addTitle('Test', 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
}
/**
2014-03-28 13:50:53 +07:00
* Create header footer
2014-03-13 01:58:00 +07:00
*/
public function testCreateHeaderFooter()
{
$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 = "create{$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
* @expectedExceptionMesssage Invalid header/footer type.
*/
public function testAddHeaderException()
{
$object = new Section(1);
$header = $object->addHeader('ODD');
}
}