2014-03-09 17:47:04 +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
|
|
|
*
|
|
|
|
|
* @link https://github.com/PHPOffice/PHPWord
|
2014-05-05 12:38:31 +04:00
|
|
|
* @copyright 2010-2014 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
|
|
|
*/
|
|
|
|
|
|
2014-03-23 11:59:22 -04:00
|
|
|
namespace PhpOffice\PhpWord\Tests\Style;
|
2014-03-09 17:47:04 +07:00
|
|
|
|
2014-03-22 10:06:08 +04:00
|
|
|
use PhpOffice\PhpWord\PhpWord;
|
2014-03-19 11:04:48 +04:00
|
|
|
use PhpOffice\PhpWord\Style\Paragraph;
|
|
|
|
|
use PhpOffice\PhpWord\Style\Tab;
|
2014-03-23 12:37:31 -04:00
|
|
|
use PhpOffice\PhpWord\Tests\TestHelperDOCX;
|
2014-03-09 17:47:04 +07:00
|
|
|
|
|
|
|
|
/**
|
2014-03-27 23:55:06 +07:00
|
|
|
* Test class for PhpOffice\PhpWord\Style\Paragraph
|
|
|
|
|
*
|
2014-03-09 17:47:04 +07:00
|
|
|
* @runTestsInSeparateProcesses
|
|
|
|
|
*/
|
2014-03-09 14:18:49 -04:00
|
|
|
class ParagraphTest extends \PHPUnit_Framework_TestCase
|
2014-03-09 17:47:04 +07:00
|
|
|
{
|
2014-03-30 14:15:23 +07:00
|
|
|
/**
|
|
|
|
|
* Tear down after each test
|
|
|
|
|
*/
|
2014-03-09 14:26:32 -04:00
|
|
|
public function tearDown()
|
|
|
|
|
{
|
|
|
|
|
TestHelperDOCX::clear();
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-09 17:47:04 +07:00
|
|
|
/**
|
|
|
|
|
* Test setting style values with null or empty value
|
|
|
|
|
*/
|
|
|
|
|
public function testSetStyleValueWithNullOrEmpty()
|
|
|
|
|
{
|
2014-03-19 11:04:48 +04:00
|
|
|
$object = new Paragraph();
|
2014-03-09 17:47:04 +07:00
|
|
|
|
|
|
|
|
$attributes = array(
|
2015-02-06 22:28:31 +04:00
|
|
|
'widowControl' => true,
|
|
|
|
|
'keepNext' => false,
|
|
|
|
|
'keepLines' => false,
|
2014-03-09 17:47:04 +07:00
|
|
|
'pageBreakBefore' => false,
|
|
|
|
|
);
|
|
|
|
|
foreach ($attributes as $key => $default) {
|
2014-03-09 21:59:23 +07:00
|
|
|
$get = "get{$key}";
|
2015-02-06 22:28:31 +04:00
|
|
|
$object->setStyleValue($key, null);
|
2014-03-09 21:59:23 +07:00
|
|
|
$this->assertEquals($default, $object->$get());
|
2015-02-06 22:28:31 +04:00
|
|
|
$object->setStyleValue($key, '');
|
2014-03-09 21:59:23 +07:00
|
|
|
$this->assertEquals($default, $object->$get());
|
2014-03-09 17:47:04 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test setting style values with normal value
|
|
|
|
|
*/
|
|
|
|
|
public function testSetStyleValueNormal()
|
|
|
|
|
{
|
2014-03-19 11:04:48 +04:00
|
|
|
$object = new Paragraph();
|
2014-03-09 17:47:04 +07:00
|
|
|
|
|
|
|
|
$attributes = array(
|
2015-02-21 18:30:00 +04:00
|
|
|
// 'align' => 'justify',
|
2015-02-06 22:28:31 +04:00
|
|
|
'spaceAfter' => 240,
|
|
|
|
|
'spaceBefore' => 240,
|
|
|
|
|
'indent' => 1,
|
|
|
|
|
'hanging' => 1,
|
|
|
|
|
'spacing' => 120,
|
|
|
|
|
'basedOn' => 'Normal',
|
|
|
|
|
'next' => 'Normal',
|
|
|
|
|
'numStyle' => 'numStyle',
|
|
|
|
|
'numLevel' => 1,
|
|
|
|
|
'widowControl' => false,
|
|
|
|
|
'keepNext' => true,
|
|
|
|
|
'keepLines' => true,
|
2014-03-09 17:47:04 +07:00
|
|
|
'pageBreakBefore' => true,
|
|
|
|
|
);
|
|
|
|
|
foreach ($attributes as $key => $value) {
|
2014-03-09 21:59:23 +07:00
|
|
|
$get = "get{$key}";
|
2014-04-06 18:03:03 +07:00
|
|
|
$object->setStyleValue("$key", $value);
|
2015-02-21 18:30:00 +04:00
|
|
|
/*if ('align' == $key) {
|
2015-02-06 22:28:31 +04:00
|
|
|
if ('justify' == $value) {
|
2014-03-09 17:47:04 +07:00
|
|
|
$value = 'both';
|
|
|
|
|
}
|
2015-02-21 18:30:00 +04:00
|
|
|
} else*/if ('indent' == $key || 'hanging' == $key) {
|
2014-03-09 17:47:04 +07:00
|
|
|
$value = $value * 720;
|
2015-02-06 22:28:31 +04:00
|
|
|
} elseif ('spacing' == $key) {
|
2014-03-09 17:47:04 +07:00
|
|
|
$value += 240;
|
|
|
|
|
}
|
2014-03-09 21:59:23 +07:00
|
|
|
$this->assertEquals($value, $object->$get());
|
2014-03-09 17:47:04 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-25 22:51:14 +07:00
|
|
|
/**
|
|
|
|
|
* Test get null style value
|
|
|
|
|
*/
|
|
|
|
|
public function testGetNullStyleValue()
|
|
|
|
|
{
|
|
|
|
|
$object = new Paragraph();
|
|
|
|
|
|
|
|
|
|
$attributes = array('spacing', 'indent', 'hanging', 'spaceBefore', 'spaceAfter');
|
|
|
|
|
foreach ($attributes as $key) {
|
|
|
|
|
$get = "get{$key}";
|
|
|
|
|
$this->assertNull($object->$get());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-09 17:47:04 +07:00
|
|
|
/**
|
|
|
|
|
* Test tabs
|
|
|
|
|
*/
|
|
|
|
|
public function testTabs()
|
|
|
|
|
{
|
2014-03-19 11:04:48 +04:00
|
|
|
$object = new Paragraph();
|
|
|
|
|
$object->setTabs(array(new Tab('left', 1550), new Tab('right', 5300)));
|
2014-06-20 09:34:49 +04:00
|
|
|
$this->assertCount(2, $object->getTabs());
|
2014-03-09 17:47:04 +07:00
|
|
|
}
|
2014-03-09 14:26:32 -04:00
|
|
|
|
2014-03-30 14:15:23 +07:00
|
|
|
/**
|
|
|
|
|
* Line height
|
|
|
|
|
*/
|
2014-03-09 14:26:32 -04:00
|
|
|
public function testLineHeight()
|
|
|
|
|
{
|
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 14:26:32 -04:00
|
|
|
|
|
|
|
|
// Test style array
|
2015-02-06 22:28:31 +04:00
|
|
|
$text = $section->addText(htmlspecialchars('This is a test', ENT_COMPAT, 'UTF-8'), array(), array('line-height' => 2.0));
|
2014-03-09 14:26:32 -04:00
|
|
|
|
2014-03-20 16:54:12 +04:00
|
|
|
$doc = TestHelperDOCX::getDocument($phpWord);
|
2014-03-09 14:26:32 -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');
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(480, $lineHeight);
|
|
|
|
|
$this->assertEquals('auto', $lineRule);
|
|
|
|
|
|
|
|
|
|
// Test setter
|
|
|
|
|
$text->getParagraphStyle()->setLineHeight(3.0);
|
2014-03-20 16:54:12 +04:00
|
|
|
$doc = TestHelperDOCX::getDocument($phpWord);
|
2014-03-09 14:26:32 -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');
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(720, $lineHeight);
|
|
|
|
|
$this->assertEquals('auto', $lineRule);
|
|
|
|
|
}
|
2014-03-10 03:20:24 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test setLineHeight validation
|
|
|
|
|
*/
|
|
|
|
|
public function testLineHeightValidation()
|
|
|
|
|
{
|
2014-03-19 11:04:48 +04:00
|
|
|
$object = new Paragraph();
|
2014-03-10 03:20:24 +07:00
|
|
|
$object->setLineHeight('12.5pt');
|
|
|
|
|
$this->assertEquals(12.5, $object->getLineHeight());
|
|
|
|
|
}
|
2014-03-28 13:50:53 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test line height exception by using nonnumeric value
|
|
|
|
|
*
|
2014-03-31 01:13:02 +07:00
|
|
|
* @expectedException \PhpOffice\PhpWord\Exception\InvalidStyleException
|
2014-03-28 13:50:53 +07:00
|
|
|
*/
|
|
|
|
|
public function testLineHeightException()
|
|
|
|
|
{
|
|
|
|
|
$object = new Paragraph();
|
|
|
|
|
$object->setLineHeight('a');
|
|
|
|
|
}
|
2014-03-11 21:44:54 +07:00
|
|
|
}
|