Skip to content

Commit af6d8c0

Browse files
authored
[TASK] Make RuleSet concrete (#1341)
... adding internal `render` method. Precursor to #1194.
1 parent 8c4a77e commit af6d8c0

File tree

4 files changed

+131
-23
lines changed

4 files changed

+131
-23
lines changed

src/RuleSet/RuleSet.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* Note that `CSSListItem` extends both `Commentable` and `Renderable`,
2828
* so those interfaces must also be implemented by concrete subclasses.
2929
*/
30-
abstract class RuleSet implements CSSElement, CSSListItem, Positionable, RuleContainer
30+
class RuleSet implements CSSElement, CSSListItem, Positionable, RuleContainer
3131
{
3232
use CommentContainer;
3333
use Position;
@@ -293,6 +293,14 @@ public function removeAllRules(): void
293293
$this->rules = [];
294294
}
295295

296+
/**
297+
* @internal
298+
*/
299+
public function render(OutputFormat $outputFormat): string
300+
{
301+
return $this->renderRules($outputFormat);
302+
}
303+
296304
protected function renderRules(OutputFormat $outputFormat): string
297305
{
298306
$result = '';
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sabberworm\CSS\Tests\Functional\RuleSet;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Sabberworm\CSS\OutputFormat;
9+
use Sabberworm\CSS\Rule\Rule;
10+
use Sabberworm\CSS\RuleSet\RuleSet;
11+
12+
/**
13+
* @covers \Sabberworm\CSS\RuleSet\RuleSet
14+
*/
15+
final class RuleSetTest extends TestCase
16+
{
17+
/**
18+
* @var RuleSet
19+
*/
20+
private $subject;
21+
22+
protected function setUp(): void
23+
{
24+
$this->subject = new RuleSet();
25+
}
26+
27+
/**
28+
* @return array<string, array{0: list<array{name: string, value: string}>, 1: string}>
29+
*/
30+
public static function providePropertyNamesAndValuesAndExpectedCss(): array
31+
{
32+
return [
33+
'no properties' => [[], ''],
34+
'one property' => [
35+
[['name' => 'color', 'value' => 'green']],
36+
'color: green;',
37+
],
38+
'two different properties' => [
39+
[
40+
['name' => 'color', 'value' => 'green'],
41+
['name' => 'display', 'value' => 'block'],
42+
],
43+
'color: green;display: block;',
44+
],
45+
'two of the same property' => [
46+
[
47+
['name' => 'color', 'value' => '#40A040'],
48+
['name' => 'color', 'value' => 'rgba(0, 128, 0, 0.25)'],
49+
],
50+
'color: #40A040;color: rgba(0, 128, 0, 0.25);',
51+
],
52+
];
53+
}
54+
55+
/**
56+
* @test
57+
*
58+
* @param list<array{name: string, value: string}> $propertyNamesAndValuesToSet
59+
*
60+
* @dataProvider providePropertyNamesAndValuesAndExpectedCss
61+
*/
62+
public function renderReturnsCssForRulesSet(array $propertyNamesAndValuesToSet, string $expectedCss): void
63+
{
64+
$this->setRulesFromPropertyNamesAndValues($propertyNamesAndValuesToSet);
65+
66+
$result = $this->subject->render(OutputFormat::create());
67+
68+
self::assertSame($expectedCss, $result);
69+
}
70+
71+
/**
72+
* @test
73+
*/
74+
public function renderWithCompactOutputFormatReturnsCssWithoutWhitespace(): void
75+
{
76+
$this->setRulesFromPropertyNamesAndValues([
77+
['name' => 'color', 'value' => 'green'],
78+
['name' => 'display', 'value' => 'block'],
79+
]);
80+
81+
$result = $this->subject->render(OutputFormat::createCompact());
82+
83+
self::assertSame('color:green;display:block;', $result);
84+
}
85+
86+
/**
87+
* @test
88+
*/
89+
public function renderWithPrettyOutputFormatReturnsCssWithNewlinesAroundIndentedDeclarations(): void
90+
{
91+
$this->setRulesFromPropertyNamesAndValues([
92+
['name' => 'color', 'value' => 'green'],
93+
['name' => 'display', 'value' => 'block'],
94+
]);
95+
96+
$result = $this->subject->render(OutputFormat::createPretty());
97+
98+
self::assertSame("\n\tcolor: green;\n\tdisplay: block;\n", $result);
99+
}
100+
101+
/**
102+
* @param list<array{name: string, value: string}> $propertyNamesAndValues
103+
*/
104+
private function setRulesFromPropertyNamesAndValues(array $propertyNamesAndValues): void
105+
{
106+
$rulesToSet = \array_map(
107+
/**
108+
* @param array{name: string, value: string} $nameAndValue
109+
*/
110+
static function (array $nameAndValue): Rule {
111+
$rule = new Rule($nameAndValue['name']);
112+
$rule->setValue($nameAndValue['value']);
113+
return $rule;
114+
},
115+
$propertyNamesAndValues
116+
);
117+
$this->subject->setRules($rulesToSet);
118+
}
119+
}

tests/Unit/RuleSet/Fixtures/ConcreteRuleSet.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

tests/Unit/RuleSet/RuleSetTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PHPUnit\Framework\TestCase;
88
use Sabberworm\CSS\CSSElement;
99
use Sabberworm\CSS\CSSList\CSSListItem;
10-
use Sabberworm\CSS\Tests\Unit\RuleSet\Fixtures\ConcreteRuleSet;
10+
use Sabberworm\CSS\RuleSet\RuleSet;
1111

1212
/**
1313
* @covers \Sabberworm\CSS\RuleSet\RuleSet
@@ -17,13 +17,13 @@ final class RuleSetTest extends TestCase
1717
use RuleContainerTest;
1818

1919
/**
20-
* @var ConcreteRuleSet
20+
* @var RuleSet
2121
*/
2222
private $subject;
2323

2424
protected function setUp(): void
2525
{
26-
$this->subject = new ConcreteRuleSet();
26+
$this->subject = new RuleSet();
2727
}
2828

2929
/**

0 commit comments

Comments
 (0)