PHPWord/tests/PhpWord/Element/HeaderTest.php

252 lines
6.4 KiB
PHP
Raw Normal View History

2014-03-09 19:09:22 +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
2018-03-08 23:52:25 +01:00
* @copyright 2010-2018 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-03-09 19:09:22 +01:00
use PhpOffice\PhpWord\AbstractWebServerEmbeddedTest;
2014-03-27 23:55:06 +07:00
/**
* Test class for PhpOffice\PhpWord\Element\Header
2014-03-27 23:55:06 +07:00
*
* @runTestsInSeparateProcesses
*/
class HeaderTest extends AbstractWebServerEmbeddedTest
2014-03-09 15:04:23 -04:00
{
2014-03-30 11:09:14 +07:00
/**
* New instance
*/
2014-03-09 15:04:23 -04:00
public function testConstructDefault()
{
$iVal = rand(1, 1000);
$oHeader = new Header($iVal);
2014-03-09 15:04:23 -04:00
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Header', $oHeader);
$this->assertEquals($iVal, $oHeader->getSectionId());
$this->assertEquals(Header::AUTO, $oHeader->getType());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Add text
*/
2014-03-09 15:04:23 -04:00
public function testAddText()
{
$oHeader = new Header(1);
2016-06-04 20:06:37 +04:00
$element = $oHeader->addText('text');
2014-03-09 15:04:23 -04:00
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element);
2014-03-09 15:04:23 -04:00
$this->assertCount(1, $oHeader->getElements());
2016-06-04 20:06:37 +04:00
$this->assertEquals('text', $element->getText());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Add text non-UTF8
*/
2014-03-09 15:04:23 -04:00
public function testAddTextNotUTF8()
{
$oHeader = new Header(1);
2016-06-04 20:06:37 +04:00
$element = $oHeader->addText(utf8_decode('ééé'));
2014-03-09 15:04:23 -04:00
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element);
2014-03-09 15:04:23 -04:00
$this->assertCount(1, $oHeader->getElements());
2016-06-04 20:06:37 +04:00
$this->assertEquals('ééé', $element->getText());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Add text break
*/
2014-03-09 15:04:23 -04:00
public function testAddTextBreak()
{
$oHeader = new Header(1);
2014-03-09 15:04:23 -04:00
$oHeader->addTextBreak();
$this->assertCount(1, $oHeader->getElements());
}
2014-03-30 11:09:14 +07:00
/**
* Add text break with params
*/
2014-03-09 15:04:23 -04:00
public function testAddTextBreakWithParams()
{
$oHeader = new Header(1);
2014-03-09 15:04:23 -04:00
$iVal = rand(1, 1000);
$oHeader->addTextBreak($iVal);
$this->assertCount($iVal, $oHeader->getElements());
}
2014-03-30 11:09:14 +07:00
/**
* Add text run
*/
2014-03-09 15:04:23 -04:00
public function testCreateTextRun()
{
$oHeader = new Header(1);
$element = $oHeader->addTextRun();
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $element);
2014-03-09 15:04:23 -04:00
$this->assertCount(1, $oHeader->getElements());
}
2014-03-30 11:09:14 +07:00
/**
* Add table
*/
2014-03-09 15:04:23 -04:00
public function testAddTable()
{
$oHeader = new Header(1);
2014-03-09 15:04:23 -04:00
$element = $oHeader->addTable();
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Table', $element);
2014-03-09 15:04:23 -04:00
$this->assertCount(1, $oHeader->getElements());
}
2014-03-30 11:09:14 +07:00
/**
* Add image
*/
2014-03-09 15:04:23 -04:00
public function testAddImage()
{
$src = __DIR__ . '/../_files/images/earth.jpg';
$oHeader = new Header(1);
$element = $oHeader->addImage($src);
2014-03-09 15:04:23 -04:00
$this->assertCount(1, $oHeader->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Add image by URL
*/
public function testAddImageByUrl()
2014-03-09 15:04:23 -04:00
{
$oHeader = new Header(1);
$element = $oHeader->addImage(self::getRemoteGifImageUrl());
2014-03-09 15:04:23 -04:00
$this->assertCount(1, $oHeader->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Add preserve text
*/
2014-03-09 15:04:23 -04:00
public function testAddPreserveText()
{
$oHeader = new Header(1);
2016-06-04 20:06:37 +04:00
$element = $oHeader->addPreserveText('text');
2014-03-09 15:04:23 -04:00
$this->assertCount(1, $oHeader->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\PreserveText', $element);
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Add preserve text non-UTF8
*/
2014-03-09 15:04:23 -04:00
public function testAddPreserveTextNotUTF8()
{
$oHeader = new Header(1);
2016-06-04 20:06:37 +04:00
$element = $oHeader->addPreserveText(utf8_decode('ééé'));
2014-03-09 15:04:23 -04:00
$this->assertCount(1, $oHeader->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\PreserveText', $element);
2016-06-04 20:06:37 +04:00
$this->assertEquals(array('ééé'), $element->getText());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Add watermark
*/
2014-03-09 15:04:23 -04:00
public function testAddWatermark()
{
$src = __DIR__ . '/../_files/images/earth.jpg';
$oHeader = new Header(1);
2014-03-09 15:04:23 -04:00
$element = $oHeader->addWatermark($src);
$this->assertCount(1, $oHeader->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Get elements
*/
2014-03-09 15:04:23 -04:00
public function testGetElements()
{
$oHeader = new Header(1);
2014-03-09 15:04:23 -04:00
$this->assertIsArray($oHeader->getElements());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Set/get relation Id
*/
2014-03-09 15:04:23 -04:00
public function testRelationId()
{
$oHeader = new Header(1);
2014-03-09 15:04:23 -04:00
$iVal = rand(1, 1000);
$oHeader->setRelationId($iVal);
$this->assertEquals($iVal, $oHeader->getRelationId());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Reset type
*/
2014-03-09 15:04:23 -04:00
public function testResetType()
{
$oHeader = new Header(1);
2014-03-09 15:04:23 -04:00
$oHeader->firstPage();
$oHeader->resetType();
$this->assertEquals(Header::AUTO, $oHeader->getType());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* First page
*/
2014-03-09 15:04:23 -04:00
public function testFirstPage()
{
$oHeader = new Header(1);
2014-03-09 15:04:23 -04:00
$oHeader->firstPage();
$this->assertEquals(Header::FIRST, $oHeader->getType());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Even page
*/
2014-03-09 15:04:23 -04:00
public function testEvenPage()
{
$oHeader = new Header(1);
2014-03-09 15:04:23 -04:00
$oHeader->evenPage();
$this->assertEquals(Header::EVEN, $oHeader->getType());
2014-03-09 15:04:23 -04:00
}
/**
* Add footnote exception
*/
public function testAddFootnoteException()
{
$this->expectException(\BadMethodCallException::class);
$header = new Header(1);
$header->addFootnote();
}
2014-04-06 15:19:09 +07:00
/**
* Set/get type
*/
public function testSetGetType()
{
$object = new Header(1);
$this->assertEquals(Header::AUTO, $object->getType());
$object->setType('ODD');
$this->assertEquals(Header::AUTO, $object->getType());
}
}