Skip to content

Commit 5de6138

Browse files
committed
Move tests to Functional
... as they are co-dependent on `OutputFormat`. Also, add tests for `createCompact` and `createPretty` flavours of `OutputFormat`.
1 parent 6fdb2e3 commit 5de6138

File tree

2 files changed

+119
-57
lines changed

2 files changed

+119
-57
lines changed
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/RuleSetTest.php

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
use PHPUnit\Framework\TestCase;
88
use Sabberworm\CSS\CSSElement;
99
use Sabberworm\CSS\CSSList\CSSListItem;
10-
use Sabberworm\CSS\OutputFormat;
11-
use Sabberworm\CSS\Rule\Rule;
1210
use Sabberworm\CSS\RuleSet\RuleSet;
1311

1412
/**
@@ -43,59 +41,4 @@ public function implementsCSSListItem(): void
4341
{
4442
self::assertInstanceOf(CSSListItem::class, $this->subject);
4543
}
46-
47-
/**
48-
* @return array<string, array{0: list<array{name: string, value: string}>, 1: string}>
49-
*/
50-
public static function providePropertyNamesAndValuesAndExpectedCss(): array
51-
{
52-
return [
53-
'no properties' => [[], ''],
54-
'one property' => [
55-
[['name' => 'color', 'value' => 'green']],
56-
'color: green;',
57-
],
58-
'two different properties' => [
59-
[
60-
['name' => 'color', 'value' => 'green'],
61-
['name' => 'display', 'value' => 'block'],
62-
],
63-
'color: green;display: block;',
64-
],
65-
'two of the same property' => [
66-
[
67-
['name' => 'color', 'value' => '#40A040'],
68-
['name' => 'color', 'value' => 'rgba(0, 128, 0, 0.25)'],
69-
],
70-
'color: #40A040;color: rgba(0, 128, 0, 0.25);',
71-
],
72-
];
73-
}
74-
75-
/**
76-
* @test
77-
*
78-
* @param list<array{name: string, value: string}> $propertyNamesAndValuesToSet
79-
*
80-
* @dataProvider providePropertyNamesAndValuesAndExpectedCss
81-
*/
82-
public function renderReturnsCssForRulesSet(array $propertyNamesAndValuesToSet, string $expectedCss): void
83-
{
84-
$rulesToSet = \array_map(
85-
/**
86-
* @param array{name: string, value: string} $nameAndValue
87-
*/
88-
static function (array $nameAndValue): Rule {
89-
$rule = new Rule($nameAndValue['name']);
90-
$rule->setValue($nameAndValue['value']);
91-
return $rule;
92-
},
93-
$propertyNamesAndValuesToSet
94-
);
95-
$this->subject->setRules($rulesToSet);
96-
97-
$result = $this->subject->render(OutputFormat::create());
98-
99-
self::assertSame($expectedCss, $result);
100-
}
10144
}

0 commit comments

Comments
 (0)