-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathFontTest.php
228 lines (202 loc) · 6.88 KB
/
FontTest.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
<?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\PhpWord;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Language;
use PhpOffice\PhpWordTests\TestHelperDOCX;
/**
* Test class for PhpOffice\PhpWord\Style\Font.
*
* @runTestsInSeparateProcesses
*/
class FontTest extends \PHPUnit\Framework\TestCase
{
/**
* Tear down after each test.
*/
protected function tearDown(): void
{
TestHelperDOCX::clear();
}
/**
* Test initiation for style type and paragraph style.
*/
public function testInitiation(): void
{
$object = new Font('text', ['alignment' => Jc::BOTH]);
self::assertEquals('text', $object->getStyleType());
self::assertInstanceOf(\PhpOffice\PhpWord\Style\Paragraph::class, $object->getParagraph());
self::assertIsArray($object->getStyleValues());
}
/**
* Test setting style values with null or empty value.
*/
public function testSetStyleValueWithNullOrEmpty(): void
{
$object = new Font();
$attributes = [
'name' => null,
'size' => null,
'hint' => null,
'color' => null,
'bold' => false,
'italic' => false,
'underline' => Font::UNDERLINE_NONE,
'superScript' => false,
'subScript' => false,
'strikethrough' => false,
'doubleStrikethrough' => false,
'smallCaps' => false,
'allCaps' => false,
'rtl' => false,
'fgColor' => null,
'bgColor' => null,
'scale' => null,
'spacing' => null,
'kerning' => null,
'lang' => null,
'hidden' => false,
'whiteSpace' => '',
'fallbackFont' => '',
];
foreach ($attributes as $key => $default) {
$get = is_bool($default) ? "is{$key}" : "get{$key}";
self::assertEquals($default, $object->$get());
$object->setStyleValue($key, null);
self::assertEquals($default, $object->$get());
$object->setStyleValue($key, '');
self::assertEquals($default, $object->$get());
}
}
/**
* Test setting style values with normal value.
*/
public function testSetStyleValueNormal(): void
{
$object = new Font();
$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,
'doubleStrikethrough' => false,
'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,
'whiteSpace' => 'pre-wrap',
'fallbackFont' => 'serif',
];
$object->setStyleByArray($attributes);
foreach ($attributes as $key => $value) {
$get = is_bool($value) ? "is{$key}" : "get{$key}";
self::assertEquals($value, $object->$get());
}
}
/**
* Test set line height.
*/
public function testLineHeight(): void
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
// Test style array
$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');
self::assertEquals(480, $lineHeight);
self::assertEquals('auto', $lineRule);
// Test setter
TestHelperDOCX::clear();
$text->getFontStyle()->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');
self::assertEquals(720, $lineHeight);
self::assertEquals('auto', $lineRule);
}
/**
* Test line height floatval.
*/
public function testLineHeightFloatval(): void
{
$object = new Font(null, ['alignment' => Jc::CENTER]);
$object->setLineHeight('1.5pt');
self::assertEquals(1.5, $object->getLineHeight());
}
/**
* Test line height exception by using nonnumeric value.
*/
public function testLineHeightException(): void
{
$this->expectException(\PhpOffice\PhpWord\Exception\InvalidStyleException::class);
$object = new Font();
$object->setLineHeight('a');
}
/**
* Test setting the language as a string.
*/
public function testSetLangAsString(): void
{
$object = new Font();
$object->setLang(Language::FR_BE);
self::assertInstanceOf('PhpOffice\PhpWord\Style\Language', $object->getLang());
self::assertEquals(Language::FR_BE, $object->getLang()->getLatin());
}
public function testRTL(): void
{
$object = new Font();
self::assertNull($object->isRTL());
self::assertInstanceOf(Font::class, $object->setRTL(true));
self::assertTrue($object->isRTL());
self::assertInstanceOf(Font::class, $object->setRTL(false));
self::assertFalse($object->isRTL());
}
public function testRTLSettings(): void
{
Settings::setDefaultRtl(null);
$object = new Font();
self::assertNull($object->isRTL());
Settings::setDefaultRtl(true);
$object = new Font();
self::assertTrue($object->isRTL());
Settings::setDefaultRtl(false);
$object = new Font();
self::assertFalse($object->isRTL());
Settings::setDefaultRtl(null);
}
}