225 lines
6.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;
2018-04-11 09:56:02 +02:00
use PhpOffice\PhpWord\ComplexType\TblWidth as TblWidthComplexType;
2015-10-10 19:22:19 +04:00
use PhpOffice\PhpWord\SimpleType\JcTable;
use PhpOffice\PhpWord\SimpleType\TblWidth;
2022-09-16 14:09:17 +02:00
use PhpOffice\PhpWord\Style\Table;
use PhpOffice\PhpWord\Style\TablePosition;
/**
2022-09-16 11:45:45 +02:00
* Test class for PhpOffice\PhpWord\Style\Table.
2014-03-27 23:55:06 +07:00
*
* @runTestsInSeparateProcesses
*/
class TableTest extends \PHPUnit\Framework\TestCase
{
/**
2022-09-16 11:45:45 +02:00
* Test class construction.
2014-03-24 11:33:20 +07:00
*
* There are 3 variables for class constructor:
* - $styleTable: Define table styles
* - $styleFirstRow: Define style for the first row
*/
2022-09-16 11:45:45 +02:00
public function testConstruct(): void
2014-03-24 11:33:20 +07:00
{
2022-09-16 11:45:45 +02:00
$styleTable = ['bgColor' => 'FF0000'];
$styleFirstRow = ['borderBottomSize' => 3];
2014-03-24 11:33:20 +07:00
$object = new Table($styleTable, $styleFirstRow);
2022-09-16 11:45:45 +02:00
self::assertEquals('FF0000', $object->getBgColor());
2014-03-24 11:33:20 +07:00
$firstRow = $object->getFirstRow();
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Table', $firstRow);
self::assertEquals(3, $firstRow->getBorderBottomSize());
2014-03-24 11:33:20 +07:00
}
/**
2022-09-16 11:45:45 +02:00
* Test default values when passing no style.
*/
2022-09-16 11:45:45 +02:00
public function testDefaultValues(): void
{
$object = new Table();
2022-09-16 11:45:45 +02:00
self::assertNull($object->getBgColor());
self::assertEquals(Table::LAYOUT_AUTO, $object->getLayout());
self::assertEquals(TblWidth::AUTO, $object->getUnit());
self::assertNull($object->getIndent());
}
2014-03-24 11:33:20 +07:00
/**
2022-09-16 11:45:45 +02:00
* Test setting style with normal value.
2014-03-24 11:33:20 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testSetGetNormal(): void
{
$object = new Table();
2022-09-16 11:45:45 +02:00
$attributes = [
'bgColor' => 'FF0000',
'borderTopSize' => 4,
'borderTopColor' => 'FF0000',
'borderLeftSize' => 4,
'borderLeftColor' => 'FF0000',
'borderRightSize' => 4,
'borderRightColor' => 'FF0000',
'borderBottomSize' => 4,
'borderBottomColor' => 'FF0000',
'borderInsideHSize' => 4,
2014-03-24 11:33:20 +07:00
'borderInsideHColor' => 'FF0000',
2022-09-16 11:45:45 +02:00
'borderInsideVSize' => 4,
2014-03-24 11:33:20 +07:00
'borderInsideVColor' => 'FF0000',
2022-09-16 11:45:45 +02:00
'cellMarginTop' => 240,
'cellMarginLeft' => 240,
'cellMarginRight' => 240,
'cellMarginBottom' => 240,
'alignment' => JcTable::CENTER,
'width' => 100,
'unit' => 'pct',
'layout' => Table::LAYOUT_FIXED,
];
2014-03-24 11:33:20 +07:00
foreach ($attributes as $key => $value) {
$set = "set{$key}";
$get = "get{$key}";
$object->$set($value);
2022-09-16 11:45:45 +02:00
self::assertEquals($value, $object->$get());
2014-03-24 11:33:20 +07:00
}
}
/**
2022-09-16 11:45:45 +02:00
* Test border color.
2014-03-24 11:33:20 +07:00
*
* Set border color and test if each part has the same color
* While looping, push values array to be asserted with getBorderColor
*/
2022-09-16 11:45:45 +02:00
public function testBorderColor(): void
2014-03-24 11:33:20 +07:00
{
$object = new Table();
2022-09-16 11:45:45 +02:00
$parts = ['Top', 'Left', 'Right', 'Bottom', 'InsideH', 'InsideV'];
2014-03-24 11:33:20 +07:00
$value = 'FF0000';
$object->setBorderColor($value);
2022-09-16 11:45:45 +02:00
$values = [];
foreach ($parts as $part) {
2014-03-24 11:33:20 +07:00
$get = "getBorder{$part}Color";
$values[] = $value;
2022-09-16 11:45:45 +02:00
self::assertEquals($value, $object->$get());
2014-03-24 11:33:20 +07:00
}
2022-09-16 11:45:45 +02:00
self::assertEquals($values, $object->getBorderColor());
2014-03-24 11:33:20 +07:00
}
/**
2022-09-16 11:45:45 +02:00
* Test border size.
2014-03-24 11:33:20 +07:00
*
* Set border size and test if each part has the same size
* While looping, push values array to be asserted with getBorderSize
* Value is in eights of a point, i.e. 4 / 8 = .5pt
*/
2022-09-16 11:45:45 +02:00
public function testBorderSize(): void
2014-03-24 11:33:20 +07:00
{
$object = new Table();
2022-09-16 11:45:45 +02:00
$parts = ['Top', 'Left', 'Right', 'Bottom', 'InsideH', 'InsideV'];
2014-03-24 11:33:20 +07:00
$value = 4;
$object->setBorderSize($value);
2022-09-16 11:45:45 +02:00
$values = [];
2014-03-24 11:33:20 +07:00
foreach ($parts as $part) {
$get = "getBorder{$part}Size";
$values[] = $value;
2022-09-16 11:45:45 +02:00
self::assertEquals($value, $object->$get());
}
2022-09-16 11:45:45 +02:00
self::assertEquals($values, $object->getBorderSize());
}
/**
2022-09-16 11:45:45 +02:00
* Test cell margin.
2014-03-24 11:33:20 +07:00
*
* Set cell margin and test if each part has the same margin
* While looping, push values array to be asserted with getCellMargin
* Value is in twips
*/
2022-09-16 11:45:45 +02:00
public function testCellMargin(): void
{
$object = new Table();
2022-09-16 11:45:45 +02:00
$parts = ['Top', 'Left', 'Right', 'Bottom'];
2014-03-24 11:33:20 +07:00
$value = 240;
$object->setCellMargin($value);
2022-09-16 11:45:45 +02:00
$values = [];
foreach ($parts as $part) {
$get = "getCellMargin{$part}";
$values[] = $value;
2022-09-16 11:45:45 +02:00
self::assertEquals($value, $object->$get());
}
2022-09-16 11:45:45 +02:00
self::assertEquals($values, $object->getCellMargin());
self::assertTrue($object->hasMargin());
}
2014-03-28 13:50:53 +07:00
/**
2022-09-16 11:45:45 +02:00
* Set style value for various special value types.
2014-03-28 13:50:53 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testSetStyleValue(): void
2014-03-28 13:50:53 +07:00
{
$object = new Table();
$object->setStyleValue('borderSize', 120);
$object->setStyleValue('cellMargin', 240);
$object->setStyleValue('borderColor', '999999');
2014-03-28 13:50:53 +07:00
2022-09-16 11:45:45 +02:00
self::assertEquals([120, 120, 120, 120, 120, 120], $object->getBorderSize());
self::assertEquals([240, 240, 240, 240], $object->getCellMargin());
self::assertEquals(
['999999', '999999', '999999', '999999', '999999', '999999'],
2014-03-28 13:50:53 +07:00
$object->getBorderColor()
);
}
/**
2022-09-16 11:45:45 +02:00
* Tests table cell spacing.
*/
2022-09-16 11:45:45 +02:00
public function testTableCellSpacing(): void
{
$object = new Table();
2022-09-16 11:45:45 +02:00
self::assertNull($object->getCellSpacing());
2022-09-16 11:45:45 +02:00
$object = new Table(['cellSpacing' => 20]);
self::assertEquals(20, $object->getCellSpacing());
}
/**
2022-09-16 11:45:45 +02:00
* Tests table floating position.
*/
2022-09-16 11:45:45 +02:00
public function testTablePosition(): void
{
$object = new Table();
2022-09-16 11:45:45 +02:00
self::assertNull($object->getPosition());
2022-09-16 11:45:45 +02:00
$object->setPosition(['vertAnchor' => TablePosition::VANCHOR_PAGE]);
self::assertNotNull($object->getPosition());
self::assertEquals(TablePosition::VANCHOR_PAGE, $object->getPosition()->getVertAnchor());
}
2018-04-11 09:56:02 +02:00
2022-09-16 11:45:45 +02:00
public function testIndent(): void
2018-04-11 09:56:02 +02:00
{
$indent = new TblWidthComplexType(100, TblWidth::TWIP);
2022-09-16 11:45:45 +02:00
$table = new Table(['indent' => $indent]);
2018-04-11 09:56:02 +02:00
2022-09-16 11:45:45 +02:00
self::assertSame($indent, $table->getIndent());
2018-04-11 09:56:02 +02:00
}
}