Skip to content

Commit c71c8a3

Browse files
authored
[TASK] Add dedicated tests for the abstract CSSList class (#979)
1 parent 321dc5e commit c71c8a3

File tree

5 files changed

+221
-128
lines changed

5 files changed

+221
-128
lines changed

tests/Unit/CSSList/AtRuleBlockListTest.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPUnit\Framework\TestCase;
88
use Sabberworm\CSS\Comment\Commentable;
99
use Sabberworm\CSS\CSSList\AtRuleBlockList;
10+
use Sabberworm\CSS\CSSList\CSSList;
1011
use Sabberworm\CSS\Renderable;
1112

1213
/**
@@ -17,8 +18,8 @@
1718
final class AtRuleBlockListTest extends TestCase
1819
{
1920
/*
20-
* Tests for the implemented interfaces
21-
*/
21+
* Tests for the implemented interfaces and superclasses
22+
*/
2223

2324
/**
2425
* @test
@@ -50,6 +51,16 @@ public function implementsCommentable(): void
5051
self::assertInstanceOf(Commentable::class, $subject);
5152
}
5253

54+
/**
55+
* @test
56+
*/
57+
public function isCSSList(): void
58+
{
59+
$subject = new AtRuleBlockList('supports');
60+
61+
self::assertInstanceOf(CSSList::class, $subject);
62+
}
63+
5364
/*
5465
* not grouped yet
5566
*/
@@ -66,16 +77,6 @@ public function atRuleNameReturnsTypeProvidedToConstructor(): void
6677
self::assertSame($type, $subject->atRuleName());
6778
}
6879

69-
/**
70-
* @test
71-
*/
72-
public function getLineNoByDefaultReturnsZero(): void
73-
{
74-
$subject = new AtRuleBlockList('supports');
75-
76-
self::assertSame(0, $subject->getLineNo());
77-
}
78-
7980
/**
8081
* @test
8182
*/

tests/Unit/CSSList/CSSListTest.php

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sabberworm\CSS\Tests\Unit\CSSList;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Sabberworm\CSS\Comment\Commentable;
9+
use Sabberworm\CSS\Renderable;
10+
use Sabberworm\CSS\RuleSet\DeclarationBlock;
11+
use Sabberworm\CSS\Tests\Unit\CSSList\Fixtures\ConcreteCSSList;
12+
13+
/**
14+
* @covers \Sabberworm\CSS\CSSList\CSSList
15+
*/
16+
final class CSSListTest extends TestCase
17+
{
18+
/**
19+
* @test
20+
*/
21+
public function implementsRenderable(): void
22+
{
23+
$subject = new ConcreteCSSList();
24+
25+
self::assertInstanceOf(Renderable::class, $subject);
26+
}
27+
28+
/**
29+
* @test
30+
*/
31+
public function implementsCommentable(): void
32+
{
33+
$subject = new ConcreteCSSList();
34+
35+
self::assertInstanceOf(Commentable::class, $subject);
36+
}
37+
38+
/**
39+
* @test
40+
*/
41+
public function getLineNoByDefaultReturnsZero(): void
42+
{
43+
$subject = new ConcreteCSSList();
44+
45+
self::assertSame(0, $subject->getLineNo());
46+
}
47+
48+
/**
49+
* @test
50+
*/
51+
public function getLineNoReturnsLineNumberProvidedToConstructor(): void
52+
{
53+
$lineNumber = 42;
54+
55+
$subject = new ConcreteCSSList($lineNumber);
56+
57+
self::assertSame($lineNumber, $subject->getLineNo());
58+
}
59+
60+
/**
61+
* @test
62+
*/
63+
public function getContentsInitiallyReturnsEmptyArray(): void
64+
{
65+
$subject = new ConcreteCSSList();
66+
67+
self::assertSame([], $subject->getContents());
68+
}
69+
70+
/**
71+
* @return array<string, array{0: list<DeclarationBlock>}>
72+
*/
73+
public static function contentsDataProvider(): array
74+
{
75+
return [
76+
'empty array' => [[]],
77+
'1 item' => [[new DeclarationBlock()]],
78+
'2 items' => [[new DeclarationBlock(), new DeclarationBlock()]],
79+
];
80+
}
81+
82+
/**
83+
* @test
84+
*
85+
* @param list<DeclarationBlock> $contents
86+
*
87+
* @dataProvider contentsDataProvider
88+
*/
89+
public function setContentsSetsContents(array $contents): void
90+
{
91+
$subject = new ConcreteCSSList();
92+
93+
$subject->setContents($contents);
94+
95+
self::assertSame($contents, $subject->getContents());
96+
}
97+
98+
/**
99+
* @test
100+
*/
101+
public function setContentsReplacesContentsSetInPreviousCall(): void
102+
{
103+
$subject = new ConcreteCSSList();
104+
105+
$contents2 = [new DeclarationBlock()];
106+
107+
$subject->setContents([new DeclarationBlock()]);
108+
$subject->setContents($contents2);
109+
110+
self::assertSame($contents2, $subject->getContents());
111+
}
112+
113+
/**
114+
* @test
115+
*/
116+
public function insertBeforeInsertsContentBeforeSibling(): void
117+
{
118+
$subject = new ConcreteCSSList();
119+
120+
$bogusOne = new DeclarationBlock();
121+
$bogusOne->setSelectors('.bogus-one');
122+
$bogusTwo = new DeclarationBlock();
123+
$bogusTwo->setSelectors('.bogus-two');
124+
125+
$item = new DeclarationBlock();
126+
$item->setSelectors('.item');
127+
128+
$sibling = new DeclarationBlock();
129+
$sibling->setSelectors('.sibling');
130+
131+
$subject->setContents([$bogusOne, $sibling, $bogusTwo]);
132+
133+
self::assertCount(3, $subject->getContents());
134+
135+
$subject->insertBefore($item, $sibling);
136+
137+
self::assertCount(4, $subject->getContents());
138+
self::assertSame([$bogusOne, $item, $sibling, $bogusTwo], $subject->getContents());
139+
}
140+
141+
/**
142+
* @test
143+
*/
144+
public function insertBeforeAppendsIfSiblingNotFound(): void
145+
{
146+
$subject = new ConcreteCSSList();
147+
148+
$bogusOne = new DeclarationBlock();
149+
$bogusOne->setSelectors('.bogus-one');
150+
$bogusTwo = new DeclarationBlock();
151+
$bogusTwo->setSelectors('.bogus-two');
152+
153+
$item = new DeclarationBlock();
154+
$item->setSelectors('.item');
155+
156+
$sibling = new DeclarationBlock();
157+
$sibling->setSelectors('.sibling');
158+
159+
$orphan = new DeclarationBlock();
160+
$orphan->setSelectors('.forever-alone');
161+
162+
$subject->setContents([$bogusOne, $sibling, $bogusTwo]);
163+
164+
self::assertCount(3, $subject->getContents());
165+
166+
$subject->insertBefore($item, $orphan);
167+
168+
self::assertCount(4, $subject->getContents());
169+
self::assertSame([$bogusOne, $sibling, $bogusTwo, $item], $subject->getContents());
170+
}
171+
}

tests/Unit/CSSList/DocumentTest.php

Lines changed: 7 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPUnit\Framework\TestCase;
88
use Sabberworm\CSS\Comment\Commentable;
99
use Sabberworm\CSS\CSSList\AtRuleBlockList;
10+
use Sabberworm\CSS\CSSList\CSSList;
1011
use Sabberworm\CSS\CSSList\Document;
1112
use Sabberworm\CSS\Property\Charset;
1213
use Sabberworm\CSS\Property\Import;
@@ -23,8 +24,8 @@
2324
final class DocumentTest extends TestCase
2425
{
2526
/*
26-
* Tests for the implemented interfaces
27-
*/
27+
* Tests for the implemented interfaces and superclasses
28+
*/
2829

2930
/**
3031
* @test
@@ -42,121 +43,19 @@ public function implementsCommentable(): void
4243
self::assertInstanceOf(Commentable::class, new Document());
4344
}
4445

45-
/*
46-
* not grouped yet
47-
*/
48-
49-
/**
50-
* @test
51-
*/
52-
public function getContentsInitiallyReturnsEmptyArray(): void
53-
{
54-
$subject = new Document();
55-
56-
self::assertSame([], $subject->getContents());
57-
}
58-
59-
/**
60-
* @return array<string, array{0: list<DeclarationBlock>}>
61-
*/
62-
public static function contentsDataProvider(): array
63-
{
64-
return [
65-
'empty array' => [[]],
66-
'1 item' => [[new DeclarationBlock()]],
67-
'2 items' => [[new DeclarationBlock(), new DeclarationBlock()]],
68-
];
69-
}
70-
71-
/**
72-
* @test
73-
*
74-
* @param list<DeclarationBlock> $contents
75-
*
76-
* @dataProvider contentsDataProvider
77-
*/
78-
public function setContentsSetsContents(array $contents): void
79-
{
80-
$subject = new Document();
81-
82-
$subject->setContents($contents);
83-
84-
self::assertSame($contents, $subject->getContents());
85-
}
86-
87-
/**
88-
* @test
89-
*/
90-
public function setContentsReplacesContentsSetInPreviousCall(): void
91-
{
92-
$subject = new Document();
93-
94-
$contents2 = [new DeclarationBlock()];
95-
96-
$subject->setContents([new DeclarationBlock()]);
97-
$subject->setContents($contents2);
98-
99-
self::assertSame($contents2, $subject->getContents());
100-
}
101-
10246
/**
10347
* @test
10448
*/
105-
public function insertContentBeforeInsertsContentBeforeSibling(): void
49+
public function isCSSList(): void
10650
{
10751
$subject = new Document();
10852

109-
$bogusOne = new DeclarationBlock();
110-
$bogusOne->setSelectors('.bogus-one');
111-
$bogusTwo = new DeclarationBlock();
112-
$bogusTwo->setSelectors('.bogus-two');
113-
114-
$item = new DeclarationBlock();
115-
$item->setSelectors('.item');
116-
117-
$sibling = new DeclarationBlock();
118-
$sibling->setSelectors('.sibling');
119-
120-
$subject->setContents([$bogusOne, $sibling, $bogusTwo]);
121-
122-
self::assertCount(3, $subject->getContents());
123-
124-
$subject->insertBefore($item, $sibling);
125-
126-
self::assertCount(4, $subject->getContents());
127-
self::assertSame([$bogusOne, $item, $sibling, $bogusTwo], $subject->getContents());
53+
self::assertInstanceOf(CSSList::class, $subject);
12854
}
12955

130-
/**
131-
* @test
56+
/*
57+
* not grouped yet
13258
*/
133-
public function insertContentBeforeAppendsIfSiblingNotFound(): void
134-
{
135-
$subject = new Document();
136-
137-
$bogusOne = new DeclarationBlock();
138-
$bogusOne->setSelectors('.bogus-one');
139-
$bogusTwo = new DeclarationBlock();
140-
$bogusTwo->setSelectors('.bogus-two');
141-
142-
$item = new DeclarationBlock();
143-
$item->setSelectors('.item');
144-
145-
$sibling = new DeclarationBlock();
146-
$sibling->setSelectors('.sibling');
147-
148-
$orphan = new DeclarationBlock();
149-
$orphan->setSelectors('.forever-alone');
150-
151-
$subject->setContents([$bogusOne, $sibling, $bogusTwo]);
152-
153-
self::assertCount(3, $subject->getContents());
154-
155-
$subject->insertBefore($item, $orphan);
156-
157-
self::assertCount(4, $subject->getContents());
158-
self::assertSame([$bogusOne, $sibling, $bogusTwo, $item], $subject->getContents());
159-
}
16059

16160
/**
16261
* @test
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sabberworm\CSS\Tests\Unit\CSSList\Fixtures;
6+
7+
use Sabberworm\CSS\CSSList\CSSList;
8+
use Sabberworm\CSS\OutputFormat;
9+
10+
final class ConcreteCSSList extends CSSList
11+
{
12+
public function isRootList(): bool
13+
{
14+
throw new \BadMethodCallException('Not implemented', 1740395831);
15+
}
16+
17+
public function render(OutputFormat $outputFormat): string
18+
{
19+
throw new \BadMethodCallException('Not implemented', 1740395836);
20+
}
21+
}

0 commit comments

Comments
 (0)