2014-03-08 23:43:27 +07:00
|
|
|
<?php
|
2014-03-27 23:55:06 +07:00
|
|
|
/**
|
2014-05-05 13:06:53 +04: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
|
|
|
*
|
2017-11-04 22:44:12 +01: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;
|
2014-03-08 23:43:27 +07:00
|
|
|
|
2014-03-22 10:06:08 +04:00
|
|
|
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;
|
2014-03-08 23:43:27 +07:00
|
|
|
|
|
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Test class for PhpOffice\PhpWord\Style\Font.
|
2014-03-27 23:55:06 +07:00
|
|
|
*
|
2014-03-08 23:43:27 +07:00
|
|
|
* @runTestsInSeparateProcesses
|
|
|
|
|
*/
|
2017-11-09 06:36:47 -02:00
|
|
|
class FontTest extends \PHPUnit\Framework\TestCase
|
2014-03-08 23:43:27 +07:00
|
|
|
{
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-08 23:43:27 +07:00
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Test initiation for style type and paragraph style.
|
2014-03-08 23:43:27 +07:00
|
|
|
*/
|
2022-09-16 11:45:45 +02:00
|
|
|
public function testInitiation(): void
|
2014-03-08 23:43:27 +07:00
|
|
|
{
|
2022-09-16 11:45:45 +02:00
|
|
|
$object = new Font('text', ['alignment' => Jc::BOTH]);
|
2014-03-08 23:43:27 +07:00
|
|
|
|
2022-09-16 11:45:45 +02:00
|
|
|
self::assertEquals('text', $object->getStyleType());
|
2022-09-16 14:45:25 +02:00
|
|
|
self::assertInstanceOf(\PhpOffice\PhpWord\Style\Paragraph::class, $object->getParagraph());
|
2022-09-16 11:45:45 +02:00
|
|
|
self::assertIsArray($object->getStyleValues());
|
2014-03-08 23:43:27 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Test setting style values with null or empty value.
|
2014-03-08 23:43:27 +07:00
|
|
|
*/
|
2022-09-16 11:45:45 +02:00
|
|
|
public function testSetStyleValueWithNullOrEmpty(): void
|
2014-03-08 23:43:27 +07:00
|
|
|
{
|
2014-03-19 11:04:48 +04:00
|
|
|
$object = new Font();
|
2014-03-08 23:43:27 +07:00
|
|
|
|
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,
|
2014-05-13 11:05:12 +07:00
|
|
|
'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,
|
|
|
|
|
];
|
2014-03-08 23:43:27 +07:00
|
|
|
foreach ($attributes as $key => $default) {
|
2014-05-13 11:05:12 +07:00
|
|
|
$get = is_bool($default) ? "is{$key}" : "get{$key}";
|
2022-09-16 11:45:45 +02:00
|
|
|
self::assertEquals($default, $object->$get());
|
2015-02-06 22:28:31 +04:00
|
|
|
$object->setStyleValue($key, null);
|
2022-09-16 11:45:45 +02:00
|
|
|
self::assertEquals($default, $object->$get());
|
2015-02-06 22:28:31 +04:00
|
|
|
$object->setStyleValue($key, '');
|
2022-09-16 11:45:45 +02:00
|
|
|
self::assertEquals($default, $object->$get());
|
2014-03-08 23:43:27 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Test setting style values with normal value.
|
2014-03-08 23:43:27 +07:00
|
|
|
*/
|
2022-09-16 11:45:45 +02:00
|
|
|
public function testSetStyleValueNormal(): void
|
2014-03-08 23:43:27 +07:00
|
|
|
{
|
2014-03-19 11:04:48 +04:00
|
|
|
$object = new Font();
|
2014-03-08 23:43:27 +07:00
|
|
|
|
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,
|
2014-05-13 11:05:12 +07:00
|
|
|
'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);
|
2014-03-08 23:43:27 +07:00
|
|
|
foreach ($attributes as $key => $value) {
|
2014-05-13 11:05:12 +07:00
|
|
|
$get = is_bool($value) ? "is{$key}" : "get{$key}";
|
2022-09-16 11:45:45 +02:00
|
|
|
self::assertEquals($value, $object->$get());
|
2014-03-08 23:43:27 +07:00
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
{
|
2014-03-20 16:54:12 +04:00
|
|
|
$phpWord = new PhpWord();
|
2014-04-02 11:02:56 +07:00
|
|
|
$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
|
|
|
|
2014-03-20 16:54:12 +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);
|
2014-03-20 16:54:12 +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(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
|
|
|
{
|
2022-09-16 11:27:29 +02: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
|
|
|
}
|
2014-03-11 21:44:54 +07:00
|
|
|
}
|