162 lines
5.2 KiB
PHP
Raw Normal View History

2014-05-28 17:59:44 +02:00
<?php
/**
* 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.
*
* @see https://github.com/PHPOffice/PHPWord
2022-09-16 11:45:45 +02:00
*
2014-05-28 17:59:44 +02:00
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
2022-09-16 14:09:17 +02:00
namespace PhpOffice\PhpWordTests\Element;
2014-05-28 17:59:44 +02:00
2022-09-16 11:45:45 +02:00
use InvalidArgumentException;
2022-09-16 14:09:17 +02:00
use PhpOffice\PhpWord\Element\Field;
use PhpOffice\PhpWord\Element\TextRun;
2022-09-16 11:45:45 +02:00
2014-05-28 17:59:44 +02:00
/**
2022-09-16 11:45:45 +02:00
* Test class for PhpOffice\PhpWord\Element\Field.
2014-05-28 17:59:44 +02:00
*
* @runTestsInSeparateProcesses
*/
class FieldTest extends \PHPUnit\Framework\TestCase
2014-05-28 17:59:44 +02:00
{
/**
2022-09-16 11:45:45 +02:00
* New instance.
2014-05-28 17:59:44 +02:00
*/
2022-09-16 11:45:45 +02:00
public function testConstructNull(): void
2014-05-28 17:59:44 +02:00
{
$oField = new Field();
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField);
2014-05-28 17:59:44 +02:00
}
/**
2022-09-16 11:45:45 +02:00
* New instance with type.
2014-05-28 17:59:44 +02:00
*/
2022-09-16 11:45:45 +02:00
public function testConstructWithType(): void
2014-05-28 17:59:44 +02:00
{
$oField = new Field('DATE');
2014-05-31 17:39:54 +07:00
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField);
self::assertEquals('DATE', $oField->getType());
2014-05-28 17:59:44 +02:00
}
/**
2022-09-16 11:45:45 +02:00
* New instance with type and properties.
2014-05-28 17:59:44 +02:00
*/
2022-09-16 11:45:45 +02:00
public function testConstructWithTypeProperties(): void
2014-05-28 17:59:44 +02:00
{
2022-09-16 11:45:45 +02:00
$oField = new Field('DATE', ['dateformat' => 'd-M-yyyy']);
2014-05-31 17:39:54 +07:00
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField);
self::assertEquals('DATE', $oField->getType());
self::assertEquals(['dateformat' => 'd-M-yyyy'], $oField->getProperties());
2014-05-28 17:59:44 +02:00
}
/**
2022-09-16 11:45:45 +02:00
* New instance with type and properties and options.
2014-05-28 17:59:44 +02:00
*/
2022-09-16 11:45:45 +02:00
public function testConstructWithTypePropertiesOptions(): void
2014-05-28 17:59:44 +02:00
{
2022-09-16 11:45:45 +02:00
$oField = new Field('DATE', ['dateformat' => 'd-M-yyyy'], ['SakaEraCalendar', 'PreserveFormat']);
2014-05-31 17:39:54 +07:00
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField);
self::assertEquals('DATE', $oField->getType());
self::assertEquals(['dateformat' => 'd-M-yyyy'], $oField->getProperties());
self::assertEquals(['SakaEraCalendar', 'PreserveFormat'], $oField->getOptions());
2014-05-28 17:59:44 +02:00
}
2014-05-31 17:39:54 +07:00
/**
2022-09-16 11:45:45 +02:00
* New instance with type and properties and options and text.
*/
2022-09-16 11:45:45 +02:00
public function testConstructWithTypePropertiesOptionsText(): void
{
2022-09-16 11:45:45 +02:00
$oField = new Field('XE', [], ['Bold', 'Italic'], 'FieldValue');
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField);
self::assertEquals('XE', $oField->getType());
self::assertEquals([], $oField->getProperties());
self::assertEquals(['Bold', 'Italic'], $oField->getOptions());
self::assertEquals('FieldValue', $oField->getText());
}
/**
2022-09-16 11:45:45 +02:00
* New instance with type and properties and options and text as TextRun.
*/
2022-09-16 11:45:45 +02:00
public function testConstructWithTypePropertiesOptionsTextAsTextRun(): void
{
$textRun = new TextRun();
$textRun->addText('test string');
2022-09-16 11:45:45 +02:00
$oField = new Field('XE', [], ['Bold', 'Italic'], $textRun);
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField);
self::assertEquals('XE', $oField->getType());
self::assertEquals([], $oField->getProperties());
self::assertEquals(['Bold', 'Italic'], $oField->getOptions());
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oField->getText());
}
2022-09-16 11:45:45 +02:00
public function testConstructWithOptionValue(): void
{
2022-09-16 11:45:45 +02:00
$oField = new Field('INDEX', [], ['\\c "3" \\h "A"']);
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField);
self::assertEquals('INDEX', $oField->getType());
self::assertEquals([], $oField->getProperties());
self::assertEquals(['\\c "3" \\h "A"'], $oField->getOptions());
}
2014-05-31 17:39:54 +07:00
/**
2022-09-16 11:45:45 +02:00
* Test setType exception.
2014-05-31 17:39:54 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testSetTypeException(): void
2014-05-31 17:39:54 +07:00
{
2022-09-16 11:45:45 +02:00
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid type');
2014-05-31 17:39:54 +07:00
$object = new Field();
$object->setType('foo');
}
/**
2022-09-16 11:45:45 +02:00
* Test setProperties exception.
2014-05-31 17:39:54 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testSetPropertiesException(): void
2014-05-31 17:39:54 +07:00
{
2022-09-16 11:45:45 +02:00
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid property');
2014-05-31 17:39:54 +07:00
$object = new Field('PAGE');
2022-09-16 11:45:45 +02:00
$object->setProperties(['foo' => 'bar']);
2014-05-31 17:39:54 +07:00
}
/**
2022-09-16 11:45:45 +02:00
* Test setOptions exception.
2014-05-31 17:39:54 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testSetOptionsException(): void
2014-05-31 17:39:54 +07:00
{
2022-09-16 11:45:45 +02:00
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid option');
2014-05-31 17:39:54 +07:00
$object = new Field('PAGE');
2022-09-16 11:45:45 +02:00
$object->setOptions(['foo' => 'bar']);
2014-05-31 17:39:54 +07:00
}
/**
2022-09-16 11:45:45 +02:00
* Test setText exception.
*/
2022-09-16 11:45:45 +02:00
public function testSetTextException(): void
{
2022-09-16 11:45:45 +02:00
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid text');
$object = new Field('XE');
2022-09-16 11:45:45 +02:00
$object->setText([]);
}
2014-05-28 17:59:44 +02:00
}