195 lines
5.9 KiB
PHP
Raw Normal View History

<?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\Style;
use PhpOffice\PhpWord\PhpWord;
2015-10-10 19:22:19 +04:00
use PhpOffice\PhpWord\SimpleType\Jc;
2022-09-16 14:09:17 +02:00
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Language;
use PhpOffice\PhpWordTests\TestHelperDOCX;
/**
2022-09-16 11:45:45 +02:00
* Test class for PhpOffice\PhpWord\Style\Font.
2014-03-27 23:55:06 +07:00
*
* @runTestsInSeparateProcesses
*/
class FontTest extends \PHPUnit\Framework\TestCase
{
2014-03-30 14:15:23 +07:00
/**
2022-09-16 11:45:45 +02:00
* Tear down after each test.
2014-03-30 14:15:23 +07:00
*/
2022-09-16 11:45:45 +02:00
protected function tearDown(): void
2014-03-09 15:03:53 -04:00
{
TestHelperDOCX::clear();
}
/**
2022-09-16 11:45:45 +02:00
* Test initiation for style type and paragraph style.
*/
2022-09-16 11:45:45 +02:00
public function testInitiation(): void
{
2022-09-16 11:45:45 +02:00
$object = new Font('text', ['alignment' => Jc::BOTH]);
2022-09-16 11:45:45 +02:00
self::assertEquals('text', $object->getStyleType());
self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $object->getParagraphStyle());
self::assertIsArray($object->getStyleValues());
}
/**
2022-09-16 11:45:45 +02:00
* Test setting style values with null or empty value.
*/
2022-09-16 11:45:45 +02:00
public function testSetStyleValueWithNullOrEmpty(): void
{
$object = new Font();
2022-09-16 11:45:45 +02:00
$attributes = [
'name' => null,
'size' => null,
'hint' => null,
'color' => null,
'bold' => false,
'italic' => false,
'underline' => Font::UNDERLINE_NONE,
'superScript' => false,
'subScript' => false,
'strikethrough' => false,
'doubleStrikethrough' => false,
2022-09-16 11:45:45 +02:00
'smallCaps' => false,
'allCaps' => false,
'rtl' => false,
'fgColor' => null,
'bgColor' => null,
'scale' => null,
'spacing' => null,
'kerning' => null,
'lang' => null,
'hidden' => false,
];
foreach ($attributes as $key => $default) {
$get = is_bool($default) ? "is{$key}" : "get{$key}";
2022-09-16 11:45:45 +02:00
self::assertEquals($default, $object->$get());
$object->setStyleValue($key, null);
2022-09-16 11:45:45 +02:00
self::assertEquals($default, $object->$get());
$object->setStyleValue($key, '');
2022-09-16 11:45:45 +02:00
self::assertEquals($default, $object->$get());
}
}
/**
2022-09-16 11:45:45 +02:00
* Test setting style values with normal value.
*/
2022-09-16 11:45:45 +02:00
public function testSetStyleValueNormal(): void
{
$object = new Font();
2022-09-16 11:45:45 +02:00
$attributes = [
'name' => 'Times New Roman',
'size' => 9,
'color' => '999999',
'hint' => 'eastAsia',
'bold' => true,
'italic' => true,
'underline' => Font::UNDERLINE_HEAVY,
'superScript' => true,
'subScript' => false,
'strikethrough' => true,
'doubleStrikethrough' => false,
2022-09-16 11:45:45 +02:00
'smallCaps' => true,
'allCaps' => false,
'fgColor' => Font::FGCOLOR_YELLOW,
'bgColor' => 'FFFF00',
'lineHeight' => 2,
'scale' => 150,
'spacing' => 240,
'kerning' => 10,
'rtl' => true,
'noProof' => true,
'lang' => new Language(Language::EN_US),
'hidden' => true,
];
2014-05-07 09:47:02 +07:00
$object->setStyleByArray($attributes);
foreach ($attributes as $key => $value) {
$get = is_bool($value) ? "is{$key}" : "get{$key}";
2022-09-16 11:45:45 +02:00
self::assertEquals($value, $object->$get());
}
}
2014-03-09 15:03:53 -04:00
2014-03-28 13:50:53 +07:00
/**
2022-09-16 11:45:45 +02:00
* Test set line height.
2014-03-28 13:50:53 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testLineHeight(): void
2014-03-09 15:03:53 -04:00
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
2014-03-09 15:03:53 -04:00
// Test style array
2022-09-16 11:45:45 +02:00
$text = $section->addText('This is a test', ['line-height' => 2.0]);
2014-03-09 15:03:53 -04:00
$doc = TestHelperDOCX::getDocument($phpWord);
2014-03-09 15:03:53 -04:00
$element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:spacing');
$lineHeight = $element->getAttribute('w:line');
$lineRule = $element->getAttribute('w:lineRule');
2022-09-16 11:45:45 +02:00
self::assertEquals(480, $lineHeight);
self::assertEquals('auto', $lineRule);
2014-03-09 15:03:53 -04:00
// Test setter
$text->getFontStyle()->setLineHeight(3.0);
$doc = TestHelperDOCX::getDocument($phpWord);
2014-03-09 15:03:53 -04:00
$element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:spacing');
$lineHeight = $element->getAttribute('w:line');
$lineRule = $element->getAttribute('w:lineRule');
2022-09-16 11:45:45 +02:00
self::assertEquals(720, $lineHeight);
self::assertEquals('auto', $lineRule);
2014-03-09 15:03:53 -04:00
}
2014-03-28 13:50:53 +07:00
/**
2022-09-16 11:45:45 +02:00
* Test line height floatval.
2014-03-28 13:50:53 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testLineHeightFloatval(): void
2014-03-28 13:50:53 +07:00
{
2022-09-16 11:45:45 +02:00
$object = new Font(null, ['alignment' => Jc::CENTER]);
2014-03-28 13:50:53 +07:00
$object->setLineHeight('1.5pt');
2022-09-16 11:45:45 +02:00
self::assertEquals(1.5, $object->getLineHeight());
2014-03-28 13:50:53 +07:00
}
/**
2022-09-16 11:45:45 +02:00
* Test line height exception by using nonnumeric value.
2014-03-28 13:50:53 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testLineHeightException(): void
2014-03-28 13:50:53 +07:00
{
$this->expectException(\PhpOffice\PhpWord\Exception\InvalidStyleException::class);
2014-03-28 13:50:53 +07:00
$object = new Font();
$object->setLineHeight('a');
}
2017-11-05 21:39:10 +01:00
/**
2022-09-16 11:45:45 +02:00
* Test setting the language as a string.
2017-11-05 21:39:10 +01:00
*/
2022-09-16 11:45:45 +02:00
public function testSetLangAsString(): void
2017-11-05 21:39:10 +01:00
{
$object = new Font();
$object->setLang(Language::FR_BE);
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\PhpWord\Style\Language', $object->getLang());
self::assertEquals(Language::FR_BE, $object->getLang()->getLatin());
2017-11-05 21:39:10 +01:00
}
}