forked from MyIntervals/PHP-CSS-Parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtRuleBlockListTest.php
More file actions
221 lines (204 loc) · 6.84 KB
/
Copy pathAtRuleBlockListTest.php
File metadata and controls
221 lines (204 loc) · 6.84 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
<?php
declare(strict_types=1);
namespace Sabberworm\CSS\Tests\CSSList;
use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\CSSList\AtRuleBlockList;
use Sabberworm\CSS\Parser;
use Sabberworm\CSS\Settings;
/**
* @covers \Sabberworm\CSS\CSSList\AtRuleBlockList
* @covers \Sabberworm\CSS\CSSList\CSSBlockList
* @covers \Sabberworm\CSS\CSSList\CSSList
*/
final class AtRuleBlockListTest extends TestCase
{
/**
* @return array<string, array{0: string}>
*/
public static function provideMinWidthMediaRule(): array
{
return [
'without spaces around arguments' => ['@media(min-width: 768px){.class{color:red}}'],
'with spaces around arguments' => ['@media (min-width: 768px) {.class{color:red}}'],
];
}
/**
* @return array<string, array{0: string}>
*/
public static function provideSyntacticallyCorrectAtRule(): array
{
return [
'media print' => ['@media print { html { background: white; color: black; } }'],
'keyframes' => ['@keyframes mymove { from { top: 0px; } }'],
'supports' => [
'
@supports (display: flex) {
.flex-container > * {
text-shadow: 0 0 2px blue;
float: none;
}
.flex-container {
display: flex;
}
}
',
],
'container' => [
'@container (min-width: 60rem) { .items { background: blue; } }',
],
'layer named' => [
'@layer theme { .button { color: blue; } }',
],
'layer anonymous' => [
'@layer { .card { padding: 1rem; } }',
],
'scope with selector' => [
'@scope (.card) { .title { font-size: 2rem; } }',
],
'scope root only' => [
'@scope { .content { margin: 0; } }',
],
'scope with limit' => [
'@scope (.article-body) to (figure) { h2 { color: red; } }',
],
'starting-style' => [
'@starting-style { .dialog { opacity: 0; transform: translateY(-10px); } }',
],
];
}
/**
* @test
*
* @dataProvider provideMinWidthMediaRule
*/
public function parsesRuleNameOfMediaQueries(string $css): void
{
$contents = (new Parser($css))->parse()->getContents();
$atRuleBlockList = $contents[0];
self::assertInstanceOf(AtRuleBlockList::class, $atRuleBlockList);
self::assertSame('media', $atRuleBlockList->atRuleName());
}
/**
* @test
*
* @dataProvider provideMinWidthMediaRule
*/
public function parsesArgumentsOfMediaQueries(string $css): void
{
$contents = (new Parser($css))->parse()->getContents();
$atRuleBlockList = $contents[0];
self::assertInstanceOf(AtRuleBlockList::class, $atRuleBlockList);
self::assertSame('(min-width: 768px)', $atRuleBlockList->atRuleArgs());
}
/**
* @test
*
* @dataProvider provideMinWidthMediaRule
* @dataProvider provideSyntacticallyCorrectAtRule
*/
public function parsesSyntacticallyCorrectAtRuleInStrictMode(string $css): void
{
$contents = (new Parser($css, Settings::create()->beStrict()))->parse()->getContents();
self::assertNotEmpty($contents, 'Failing CSS: `' . $css . '`');
}
/**
* @return array<string, array{0: string, 1: string, 2: string, 3: int}>
*/
public static function provideAtRuleParsingData(): array
{
return [
'layer with named argument' => [
'@layer theme { .button { color: blue; } }',
'layer',
'theme',
1,
],
'layer without arguments' => [
'@layer { .card { padding: 1rem; } }',
'layer',
'',
1,
],
'scope with selector' => [
'@scope (.card) { .title { font-size: 2rem; } }',
'scope',
'(.card)',
1,
],
'scope without selector' => [
'@scope { .content { margin: 0; } }',
'scope',
'',
1,
],
'scope with limit' => [
'@scope (.article-body) to (figure) { h2 { color: red; } }',
'scope',
'(.article-body) to (figure)',
1,
],
'starting-style' => [
'@starting-style { .dialog { opacity: 0; transform: translateY(-10px); } }',
'starting-style',
'',
1,
],
];
}
/**
* @return array<string, array{0: string, 1: list<string>}>
*/
public static function provideAtRuleRenderingData(): array
{
return [
'layer with named argument' => [
'@layer theme { .button { color: blue; } }',
['@layer theme', '.button', 'color: blue'],
],
'scope with selector' => [
'@scope (.card) { .title { font-size: 2rem; } }',
['@scope (.card)', '.title', 'font-size: 2rem'],
],
'scope with limit' => [
'@scope (.article-body) to (figure) { h2 { color: red; } }',
['@scope (.article-body) to (figure)', 'h2', 'color: red'],
],
'starting-style' => [
'@starting-style { .dialog { opacity: 0; } }',
['@starting-style', '.dialog', 'opacity: 0'],
],
];
}
/**
* @test
*
* @dataProvider provideAtRuleParsingData
*/
public function parsesAtRuleBlockList(
string $css,
string $expectedName,
string $expectedArgs,
int $expectedContentCount
): void {
$contents = (new Parser($css))->parse()->getContents();
$atRuleBlockList = $contents[0];
self::assertInstanceOf(AtRuleBlockList::class, $atRuleBlockList);
self::assertSame($expectedName, $atRuleBlockList->atRuleName());
self::assertSame($expectedArgs, $atRuleBlockList->atRuleArgs());
self::assertCount($expectedContentCount, $atRuleBlockList->getContents());
}
/**
* @test
*
* @dataProvider provideAtRuleRenderingData
*
* @param list<string> $expectedSubstrings
*/
public function rendersAtRuleBlockListCorrectly(string $css, array $expectedSubstrings): void
{
$rendered = (new Parser($css))->parse()->render();
foreach ($expectedSubstrings as $expected) {
self::assertStringContainsString($expected, $rendered);
}
}
}