-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathDeclarationBlockParserTest.php
More file actions
312 lines (294 loc) · 11.8 KB
/
DeclarationBlockParserTest.php
File metadata and controls
312 lines (294 loc) · 11.8 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
declare(strict_types=1);
namespace Pelago\Emogrifier\Tests\Unit\Utilities;
use Pelago\Emogrifier\Utilities\DeclarationBlockParser;
use PHPUnit\Framework\TestCase;
/**
* @covers \Pelago\Emogrifier\Utilities\DeclarationBlockParser
*/
final class DeclarationBlockParserTest extends TestCase
{
/**
* @return array<non-empty-string, array{name: non-empty-string, expect: non-empty-string}>
*/
public function providePropertyNameAndExpectedNormalization(): array
{
return [
'standard property' => [
'name' => 'color',
'expect' => 'color',
],
'vendor property' => [
'name' => '-moz-box-sizing',
'expect' => '-moz-box-sizing',
],
'custom property' => [
'name' => '--text-color',
'expect' => '--text-color',
],
'custom property with numbers' => [
'name' => '--base-size-16',
'expect' => '--base-size-16',
],
'custom property with everything' => [
'name' => '--Base_size-4u',
'expect' => '--Base_size-4u',
],
'standard property with some uppercase' => [
'name' => 'cOlOr',
'expect' => 'color',
],
'vendor property with some uppercase' => [
'name' => '-MOZ-box-Sizing',
'expect' => '-moz-box-sizing',
],
'custom property with some uppercase' => [
'name' => '--TEXT-Color',
'expect' => '--TEXT-Color',
],
];
}
/**
* @test
*
* @param non-empty-string $name
* @param non-empty-string $expectedNormalization
*
* @dataProvider providePropertyNameAndExpectedNormalization
*/
public function normalizesPropertyName(string $name, string $expectedNormalization): void
{
$result = DeclarationBlockParser::normalizePropertyName($name);
self::assertSame($expectedNormalization, $result);
}
/**
* @return array<non-empty-string, array{string: string, array: array<non-empty-string, non-empty-string>}>
*/
public function provideDeclarationBlockAsStringAndArray(): array
{
return [
'empty' => [
'string' => '',
'array' => [],
],
'whitespace only' => [
'string' => " \r\n\t",
'array' => [],
],
'semicolon only' => [
'string' => ';',
'array' => [],
],
'whitespace and semicolon only' => [
'string' => " \r\n\t; \r\n\t",
'array' => [],
],
'1 declaration without trailing semicolon' => [
'string' => 'color: green',
'array' => ['color' => 'green'],
],
'1 declaration with trailing semicolon' => [
'string' => 'color: green;',
'array' => ['color' => 'green'],
],
'1 declaration with leading semicolon' => [
'string' => '; color: green',
'array' => ['color' => 'green'],
],
'declaration with space before colon' => [
'string' => 'color : green;',
'array' => ['color' => 'green'],
],
'declaration without space after colon' => [
'string' => 'color:green;',
'array' => ['color' => 'green'],
],
'declaration without value' => [
'string' => 'color: ;',
'array' => [],
],
'declaration with only value' => [
'string' => 'red;',
'array' => [],
],
'declaration without property name' => [
'string' => ' : red;',
'array' => [],
],
'2 declarations' => [
'string' => 'color: green; background-color: green;',
'array' => ['color' => 'green', 'background-color' => 'green'],
],
'2 declarations with extra semicolon between' => [
'string' => 'color: green;; background-color: green;',
'array' => ['color' => 'green', 'background-color' => 'green'],
],
'2 declarations with newline between' => [
'string' => 'color: green;' . "\n" . 'background-color: green;',
'array' => ['color' => 'green', 'background-color' => 'green'],
],
'declaration with !important' => [
'string' => 'color: green !important;',
'array' => ['color' => 'green !important'],
],
'vendor property declaration' => [
'string' => '-moz-box-sizing: border-box;',
'array' => ['-moz-box-sizing' => 'border-box'],
],
'custom property definition' => [
'string' => '--text-color: green;',
'array' => ['--text-color' => 'green'],
],
'custom property with numbers definition' => [
'string' => '--base-size-16: 1rem;',
'array' => ['--base-size-16' => '1rem'],
],
'custom property with everything definition' => [
'string' => '--Base_size-4u: normal;',
'array' => ['--Base_size-4u' => 'normal'],
],
'specification test single character allowed' => [
'string' => 'x: 0;',
'array' => ['x' => '0'],
],
'specification test hyphen single character allowed' => [
'string' => '-o: normal;',
'array' => ['-o' => 'normal'],
],
'specification test underscore allowed' => [
'string' => '_allowed: normal;',
'array' => ['_allowed' => 'normal'],
],
'specification test hyphen underscore allowed' => [
'string' => '-_allowed: normal;',
'array' => ['-_allowed' => 'normal'],
],
'specification test double hyphen underscore allowed' => [
'string' => '--_allowed: normal;',
'array' => ['--_allowed' => 'normal'],
],
'specification test double underscore allowed' => [
'string' => '__allowed: normal;',
'array' => ['__allowed' => 'normal'],
],
'specification test number not allowed' => [
'string' => '2not-allowed: unset;',
'array' => [],
],
'specification test hyphen number not allowed' => [
'string' => '-2not-allowed: unset;',
'array' => [],
],
'specification test double hyphen number allowed' => [
'string' => '--2allowed: normal;',
'array' => ['--2allowed' => 'normal'],
],
'specification test backslash not allowed' => [
'string' => 'not\\allowed: unset;',
'array' => [],
],
'declaration using custom property' => [
'string' => 'color: var(--text-color);',
'array' => ['color' => 'var(--text-color)'],
],
'declaration with uppercase in property name' => [
'string' => 'CoLoR: green;',
'array' => ['color' => 'green'],
],
'vendor property declaration with uppercase in property name' => [
'string' => '-Moz-Box-Sizing: border-box;',
'array' => ['-moz-box-sizing' => 'border-box'],
],
'custom property definition with uppercase in property name' => [
'string' => '--Text-Color: green;',
'array' => ['--Text-Color' => 'green'],
],
'declaration using custom property with uppercase in its name' => [
'string' => 'color: var(--Text-Color);',
'array' => ['color' => 'var(--Text-Color)'],
],
'declaration with uppercase in property value' => [
'string' => 'font-family: Courier;',
'array' => ['font-family' => 'Courier'],
],
'declaration using CSS function' => [
'string' => 'width: calc(50% + 10vw + 10rem);',
'array' => ['width' => 'calc(50% + 10vw + 10rem)'],
],
'shorthand property declaration' => [
'string' => 'border: 2px solid green',
'array' => ['border' => '2px solid green'],
],
'declaration with text value (single quotes)' => [
'string' => 'content: \'Hello universe\';',
'array' => ['content' => '\'Hello universe\''],
],
'declaration with text value (double quotes)' => [
'string' => 'content: "Hello universe";',
'array' => ['content' => '"Hello universe"'],
],
'font declaration with size, line-height and family' => [
'string' => 'font: 1.2em/2 "Fira Sans", sans-serif;',
'array' => ['font' => '1.2em/2 "Fira Sans", sans-serif'],
],
'declaration using data URL with charset' => [
'string' => 'text: url("data:text/plain;charset=UTF-8,Hello%20universe");',
'array' => ['text' => 'url("data:text/plain;charset=UTF-8,Hello%20universe")'],
],
'declaration using data URL with base64-encoding' => [
'string' => 'text: url("data:;base64,SGVsbG8gdW5pdmVyc2U=");',
'array' => ['text' => 'url("data:;base64,SGVsbG8gdW5pdmVyc2U=")'],
],
'declaration using data URL with charset and base64-encoding' => [
'string' => 'text: url("data:text/plain;charset=UTF-8;base64,SGVsbG8gdW5pdmVyc2U=");',
'array' => ['text' => 'url("data:text/plain;charset=UTF-8;base64,SGVsbG8gdW5pdmVyc2U=")'],
],
];
}
/**
* @test
*
* @param array<non-empty-string, non-empty-string> $declarationBlockAsArray
*
* @dataProvider provideDeclarationBlockAsStringAndArray
*/
public function parses(string $declarationBlockAsString, array $declarationBlockAsArray): void
{
$result = DeclarationBlockParser::parse($declarationBlockAsString);
self::assertSame($declarationBlockAsArray, $result);
}
/**
* @test
*/
public function overridesEarlierDeclarationWithLaterOne(): void
{
$result = DeclarationBlockParser::parse('color: red; color: green;');
self::assertSame(['color' => 'green'], $result);
}
/**
* From a black (or orange) box point of view, the class under test may or may not implement some caching.
* If it does, check that the same result is obtained from a second identical request.
*
* @test
*/
public function providesConsistentResults(): void
{
$declarationBlock = 'color: green;';
$firstResult = DeclarationBlockParser::parse($declarationBlock);
$secondResult = DeclarationBlockParser::parse($declarationBlock);
self::assertSame($firstResult, $secondResult);
}
/**
* @test
*/
public function clearCacheEmptiesStaticCache(): void
{
DeclarationBlockParser::parse('color: green;');
DeclarationBlockParser::clearCache();
$cacheProperty = new \ReflectionProperty(DeclarationBlockParser::class, 'cache');
if (\PHP_VERSION_ID < 80100) {
$cacheProperty->setAccessible(true);
}
self::assertSame([], $cacheProperty->getValue());
}
}