PHPWord/tests/PhpWordTests/Style/ParagraphTest.php

190 lines
5.5 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;
use PhpOffice\PhpWord\SimpleType\LineSpacingRule;
2022-09-16 14:09:17 +02:00
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\Style\Tab;
use PhpOffice\PhpWordTests\TestHelperDOCX;
/**
2022-09-16 11:45:45 +02:00
* Test class for PhpOffice\PhpWord\Style\Paragraph.
2014-03-27 23:55:06 +07:00
*
* @runTestsInSeparateProcesses
*/
class ParagraphTest 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
{
TestHelperDOCX::clear();
}
/**
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 Paragraph();
2022-09-16 11:45:45 +02:00
$attributes = [
'widowControl' => true,
'keepNext' => false,
'keepLines' => false,
'pageBreakBefore' => false,
2017-07-01 22:51:53 +02:00
'contextualSpacing' => false,
2022-09-16 11:45:45 +02:00
];
foreach ($attributes as $key => $default) {
2017-07-01 22:51:53 +02:00
$get = $this->findGetter($key, $default, $object);
$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 Paragraph();
2022-09-16 11:45:45 +02:00
$attributes = [
'spaceAfter' => 240,
'spaceBefore' => 240,
'indent' => 1,
'hanging' => 1,
'spacing' => 120,
'spacingLineRule' => LineSpacingRule::AT_LEAST,
'basedOn' => 'Normal',
'next' => 'Normal',
'numStyle' => 'numStyle',
'numLevel' => 1,
'widowControl' => false,
'keepNext' => true,
'keepLines' => true,
'pageBreakBefore' => true,
'contextualSpacing' => true,
'textAlignment' => 'auto',
'bidi' => true,
'suppressAutoHyphens' => true,
2022-09-16 11:45:45 +02:00
];
foreach ($attributes as $key => $value) {
2017-07-01 22:51:53 +02:00
$get = $this->findGetter($key, $value, $object);
$object->setStyleValue("$key", $value);
if ('indent' == $key || 'hanging' == $key) {
$value = $value * 720;
}
2022-09-16 11:45:45 +02:00
self::assertEquals($value, $object->$get());
}
}
2017-07-01 22:51:53 +02:00
private function findGetter($key, $value, $object)
{
if (is_bool($value)) {
if (method_exists($object, "is{$key}")) {
return "is{$key}";
} elseif (method_exists($object, "has{$key}")) {
2017-07-01 22:51:53 +02:00
return "has{$key}";
}
}
2017-07-01 22:51:53 +02:00
return "get{$key}";
}
/**
2022-09-16 11:45:45 +02:00
* Test get null style value.
*/
2022-09-16 11:45:45 +02:00
public function testGetNullStyleValue(): void
{
$object = new Paragraph();
2022-09-16 11:45:45 +02:00
$attributes = ['spacing', 'indent', 'hanging', 'spaceBefore', 'spaceAfter', 'textAlignment'];
foreach ($attributes as $key) {
2017-07-01 22:51:53 +02:00
$get = $this->findGetter($key, null, $object);
2022-09-16 11:45:45 +02:00
self::assertNull($object->$get());
}
}
/**
2022-09-16 11:45:45 +02:00
* Test tabs.
*/
2022-09-16 11:45:45 +02:00
public function testTabs(): void
{
$object = new Paragraph();
2022-09-16 11:45:45 +02:00
$object->setTabs([new Tab('left', 1550), new Tab('right', 5300)]);
self::assertCount(2, $object->getTabs());
}
2014-03-30 14:15:23 +07:00
/**
2022-09-16 11:45:45 +02:00
* Line height.
2014-03-30 14:15:23 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testLineHeight(): void
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
// Test style array
2022-09-16 11:45:45 +02:00
$text = $section->addText('This is a test', [], ['line-height' => 2.0]);
$doc = TestHelperDOCX::getDocument($phpWord);
$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);
// Test setter
$text->getParagraphStyle()->setLineHeight(3.0);
$doc = TestHelperDOCX::getDocument($phpWord);
$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);
}
/**
2022-09-16 11:45:45 +02:00
* Test setLineHeight validation.
*/
2022-09-16 11:45:45 +02:00
public function testLineHeightValidation(): void
{
$object = new Paragraph();
$object->setLineHeight('12.5pt');
2022-09-16 11:45:45 +02:00
self::assertEquals(12.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 Paragraph();
$object->setLineHeight('a');
}
}