-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseTest.php
More file actions
67 lines (54 loc) · 1.89 KB
/
Copy pathparseTest.php
File metadata and controls
67 lines (54 loc) · 1.89 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
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class ParseTest extends TestCase
{
private array $files = [
"bg.json",
"bgImage.json",
"bgPosition.json",
"directives.json",
"objPosition.json",
"perspectiveOrigin.json",
"properties.json",
"regression.json",
"transformOrigin.json",
"transforms.json",
"values.json",
"valuesSyntaxN.json"
// TODO: add variables support
//"variables.json"
];
private function addSpacesAroundCSSValues(string $css): string {
// Regular expression to match CSS rules and ensure spaces around curly braces
$pattern = '/(\w+\s*\{\s*)([^}]+)(\s*\})/';
$replacement = '$1 $2 $3';
// Apply the regex replacement
$css = preg_replace($pattern, $replacement, $css);
return str_replace(' ', ' ', $css);
}
private function parseAction(string $css, array $options = []): string
{
// trying to parse created css sources
$css_parser = new \Sabberworm\CSS\Parser($css);
try {
$css_tree = $css_parser->parse();
} catch (\Sabberworm\CSS\Parsing\SourceException $e) {
return '';
}
$rtlcss = new \Irmmr\RTLCss\Parser($css_tree, $options);
$rtlcss->flip();
return $this->addSpacesAroundCSSValues( $css_tree->render() );
}
public function testParseEveryEntry(): void
{
foreach ($this->files as $file) {
$content = file_get_contents(__DIR__ . '/data/' . $file);
$json = json_decode($content, true);
foreach ($json as $e) {
$input = $this->parseAction($e['input']);
$expected = $this->addSpacesAroundCSSValues($e['expected']);
$this->assertSame($expected, $input, $e['should']);
}
}
}
}