-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathTableTest.php
255 lines (225 loc) · 7.89 KB
/
TableTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
/**
* 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.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWordTests\Style;
use PhpOffice\PhpWord\ComplexType\TblWidth as TblWidthComplexType;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\SimpleType\JcTable;
use PhpOffice\PhpWord\SimpleType\TblWidth;
use PhpOffice\PhpWord\Style\Table;
use PhpOffice\PhpWord\Style\TablePosition;
/**
* Test class for PhpOffice\PhpWord\Style\Table.
*
* @runTestsInSeparateProcesses
*/
class TableTest extends \PHPUnit\Framework\TestCase
{
/**
* Test class construction.
*
* There are 3 variables for class constructor:
* - $styleTable: Define table styles
* - $styleFirstRow: Define style for the first row
*/
public function testConstruct(): void
{
$styleTable = ['bgColor' => 'FF0000'];
$styleFirstRow = ['borderBottomSize' => 3];
$object = new Table($styleTable, $styleFirstRow);
self::assertEquals('FF0000', $object->getBgColor());
$firstRow = $object->getFirstRow();
self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Table', $firstRow);
self::assertEquals(3, $firstRow->getBorderBottomSize());
}
/**
* Test default values when passing no style.
*/
public function testDefaultValues(): void
{
$object = new Table();
self::assertNull($object->getBgColor());
self::assertEquals(Table::LAYOUT_AUTO, $object->getLayout());
self::assertEquals(TblWidth::AUTO, $object->getUnit());
self::assertNull($object->getIndent());
}
/**
* Test setting style with normal value.
*/
public function testSetGetNormal(): void
{
$object = new Table();
$attributes = [
'bgColor' => 'FF0000',
'borderTopSize' => 4,
'borderTopColor' => 'FF0000',
'borderLeftSize' => 4,
'borderLeftColor' => 'FF0000',
'borderRightSize' => 4,
'borderRightColor' => 'FF0000',
'borderBottomSize' => 4,
'borderBottomColor' => 'FF0000',
'borderInsideHSize' => 4,
'borderInsideHColor' => 'FF0000',
'borderInsideVSize' => 4,
'borderInsideVColor' => 'FF0000',
'cellMarginTop' => 240,
'cellMarginLeft' => 240,
'cellMarginRight' => 240,
'cellMarginBottom' => 240,
'alignment' => JcTable::CENTER,
'width' => 100,
'unit' => 'pct',
'layout' => Table::LAYOUT_FIXED,
];
foreach ($attributes as $key => $value) {
$set = "set{$key}";
$get = "get{$key}";
$object->$set($value);
self::assertEquals($value, $object->$get());
}
}
public function testBidiVisual(): void
{
$object = new Table();
self::assertNull($object->isBidiVisual());
self::assertInstanceOf(Table::class, $object->setBidiVisual(true));
self::assertTrue($object->isBidiVisual());
self::assertInstanceOf(Table::class, $object->setBidiVisual(false));
self::assertFalse($object->isBidiVisual());
self::assertInstanceOf(Table::class, $object->setBidiVisual(null));
self::assertNull($object->isBidiVisual());
}
public function testBidiVisualSettings(): void
{
Settings::setDefaultRtl(null);
$object = new Table();
self::assertNull($object->isBidiVisual());
Settings::setDefaultRtl(true);
$object = new Table();
self::assertTrue($object->isBidiVisual());
Settings::setDefaultRtl(false);
$object = new Table();
self::assertFalse($object->isBidiVisual());
Settings::setDefaultRtl(null);
}
/**
* Test border color.
*
* Set border color and test if each part has the same color
* While looping, push values array to be asserted with getBorderColor
*/
public function testBorderColor(): void
{
$object = new Table();
$parts = ['Top', 'Left', 'Right', 'Bottom', 'InsideH', 'InsideV'];
$value = 'FF0000';
$object->setBorderColor($value);
$values = [];
foreach ($parts as $part) {
$get = "getBorder{$part}Color";
$values[] = $value;
self::assertEquals($value, $object->$get());
}
self::assertEquals($values, $object->getBorderColor());
}
/**
* Test border size.
*
* 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
*/
public function testBorderSize(): void
{
$object = new Table();
$parts = ['Top', 'Left', 'Right', 'Bottom', 'InsideH', 'InsideV'];
$value = 4;
$object->setBorderSize($value);
$values = [];
foreach ($parts as $part) {
$get = "getBorder{$part}Size";
$values[] = $value;
self::assertEquals($value, $object->$get());
}
self::assertEquals($values, $object->getBorderSize());
}
/**
* Test cell margin.
*
* 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
*/
public function testCellMargin(): void
{
$object = new Table();
$parts = ['Top', 'Left', 'Right', 'Bottom'];
$value = 240;
$object->setCellMargin($value);
$values = [];
foreach ($parts as $part) {
$get = "getCellMargin{$part}";
$values[] = $value;
self::assertEquals($value, $object->$get());
}
self::assertEquals($values, $object->getCellMargin());
self::assertTrue($object->hasMargin());
}
/**
* Set style value for various special value types.
*/
public function testSetStyleValue(): void
{
$object = new Table();
$object->setStyleValue('borderSize', 120);
$object->setStyleValue('cellMargin', 240);
$object->setStyleValue('borderColor', '999999');
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'],
$object->getBorderColor()
);
}
/**
* Tests table cell spacing.
*/
public function testTableCellSpacing(): void
{
$object = new Table();
self::assertNull($object->getCellSpacing());
$object = new Table(['cellSpacing' => 20]);
self::assertEquals(20, $object->getCellSpacing());
}
/**
* Tests table floating position.
*/
public function testTablePosition(): void
{
$object = new Table();
self::assertNull($object->getPosition());
$object->setPosition(['vertAnchor' => TablePosition::VANCHOR_PAGE]);
self::assertNotNull($object->getPosition());
self::assertEquals(TablePosition::VANCHOR_PAGE, $object->getPosition()->getVertAnchor());
}
public function testIndent(): void
{
$indent = new TblWidthComplexType(100, TblWidth::TWIP);
$table = new Table(['indent' => $indent]);
self::assertSame($indent, $table->getIndent());
}
}