Skip to content

[TASK] Make RuleSet concrete #1341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/RuleSet/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* Note that `CSSListItem` extends both `Commentable` and `Renderable`,
* so those interfaces must also be implemented by concrete subclasses.
*/
abstract class RuleSet implements CSSElement, CSSListItem, Positionable, RuleContainer
class RuleSet implements CSSElement, CSSListItem, Positionable, RuleContainer
{
use CommentContainer;
use Position;
Expand Down Expand Up @@ -293,6 +293,14 @@ public function removeAllRules(): void
$this->rules = [];
}

/**
* @internal
*/
public function render(OutputFormat $outputFormat): string
{
return $this->renderRules($outputFormat);
}

protected function renderRules(OutputFormat $outputFormat): string
{
$result = '';
Expand Down
119 changes: 119 additions & 0 deletions tests/Functional/RuleSet/RuleSetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

declare(strict_types=1);

namespace Sabberworm\CSS\Tests\Functional\RuleSet;

use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\OutputFormat;
use Sabberworm\CSS\Rule\Rule;
use Sabberworm\CSS\RuleSet\RuleSet;

/**
* @covers \Sabberworm\CSS\RuleSet\RuleSet
*/
final class RuleSetTest extends TestCase
{
/**
* @var RuleSet
*/
private $subject;

protected function setUp(): void
{
$this->subject = new RuleSet();
}

/**
* @return array<string, array{0: list<array{name: string, value: string}>, 1: string}>
*/
public static function providePropertyNamesAndValuesAndExpectedCss(): array
{
return [
'no properties' => [[], ''],
'one property' => [
[['name' => 'color', 'value' => 'green']],
'color: green;',
],
'two different properties' => [
[
['name' => 'color', 'value' => 'green'],
['name' => 'display', 'value' => 'block'],
],
'color: green;display: block;',
],
'two of the same property' => [
[
['name' => 'color', 'value' => '#40A040'],
['name' => 'color', 'value' => 'rgba(0, 128, 0, 0.25)'],
],
'color: #40A040;color: rgba(0, 128, 0, 0.25);',
],
];
}

/**
* @test
*
* @param list<array{name: string, value: string}> $propertyNamesAndValuesToSet
*
* @dataProvider providePropertyNamesAndValuesAndExpectedCss
*/
public function renderReturnsCssForRulesSet(array $propertyNamesAndValuesToSet, string $expectedCss): void
{
$this->setRulesFromPropertyNamesAndValues($propertyNamesAndValuesToSet);

$result = $this->subject->render(OutputFormat::create());

self::assertSame($expectedCss, $result);
}

/**
* @test
*/
public function renderWithCompactOutputFormatReturnsCssWithoutWhitespace(): void
{
$this->setRulesFromPropertyNamesAndValues([
['name' => 'color', 'value' => 'green'],
['name' => 'display', 'value' => 'block'],
]);

$result = $this->subject->render(OutputFormat::createCompact());

self::assertSame('color:green;display:block;', $result);
}

/**
* @test
*/
public function renderWithPrettyOutputFormatReturnsCssWithNewlinesAroundIndentedDeclarations(): void
{
$this->setRulesFromPropertyNamesAndValues([
['name' => 'color', 'value' => 'green'],
['name' => 'display', 'value' => 'block'],
]);

$result = $this->subject->render(OutputFormat::createPretty());

self::assertSame("\n\tcolor: green;\n\tdisplay: block;\n", $result);
}

/**
* @param list<array{name: string, value: string}> $propertyNamesAndValues
*/
private function setRulesFromPropertyNamesAndValues(array $propertyNamesAndValues): void
{
$rulesToSet = \array_map(
/**
* @param array{name: string, value: string} $nameAndValue
*/
static function (array $nameAndValue): Rule {
$rule = new Rule($nameAndValue['name']);
$rule->setValue($nameAndValue['value']);
return $rule;
},
$propertyNamesAndValues
);
$this->subject->setRules($rulesToSet);
}
}
19 changes: 0 additions & 19 deletions tests/Unit/RuleSet/Fixtures/ConcreteRuleSet.php

This file was deleted.

6 changes: 3 additions & 3 deletions tests/Unit/RuleSet/RuleSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\CSSElement;
use Sabberworm\CSS\CSSList\CSSListItem;
use Sabberworm\CSS\Tests\Unit\RuleSet\Fixtures\ConcreteRuleSet;
use Sabberworm\CSS\RuleSet\RuleSet;

/**
* @covers \Sabberworm\CSS\RuleSet\RuleSet
Expand All @@ -17,13 +17,13 @@ final class RuleSetTest extends TestCase
use RuleContainerTest;

/**
* @var ConcreteRuleSet
* @var RuleSet
*/
private $subject;

protected function setUp(): void
{
$this->subject = new ConcreteRuleSet();
$this->subject = new RuleSet();
}

/**
Expand Down