Skip to content

Commit 1a44e9b

Browse files
committed
[TASK] Add Tests for Document::getAllDeclarationBlocks()
Also remove a misleading and incorrect comment. Part of #757
1 parent d71ff28 commit 1a44e9b

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

src/CSSList/Document.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public static function parse(ParserState $parserState): Document
3232

3333
/**
3434
* Gets all `DeclarationBlock` objects recursively, no matter how deeply nested the selectors are.
35-
* Aliased as `getAllSelectors()`.
3635
*
3736
* @return array<int, DeclarationBlock>
3837
*/

tests/Unit/CSSList/DocumentTest.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@
66

77
use PHPUnit\Framework\TestCase;
88
use Sabberworm\CSS\Comment\Commentable;
9+
use Sabberworm\CSS\CSSList\AtRuleBlockList;
910
use Sabberworm\CSS\CSSList\Document;
11+
use Sabberworm\CSS\Property\Charset;
12+
use Sabberworm\CSS\Property\Import;
1013
use Sabberworm\CSS\Renderable;
1114
use Sabberworm\CSS\RuleSet\DeclarationBlock;
15+
use Sabberworm\CSS\Value\CSSString;
16+
use Sabberworm\CSS\Value\URL;
1217

1318
/**
19+
* @covers \Sabberworm\CSS\CSSList\CSSBlockList
20+
* @covers \Sabberworm\CSS\CSSList\CSSList
1421
* @covers \Sabberworm\CSS\CSSList\Document
1522
*/
1623
final class DocumentTest extends TestCase
@@ -142,4 +149,80 @@ public function insertContentBeforeAppendsIfSiblingNotFound(): void
142149
self::assertCount(4, $this->subject->getContents());
143150
self::assertSame([$bogusOne, $sibling, $bogusTwo, $item], $this->subject->getContents());
144151
}
152+
153+
/**
154+
* @test
155+
*/
156+
public function getAllDeclarationBlocksForNoContentsReturnsEmptyArray(): void
157+
{
158+
self::assertSame([], $this->subject->getAllDeclarationBlocks());
159+
}
160+
161+
/**
162+
* @test
163+
*/
164+
public function getAllDeclarationBlocksCanReturnOneDirectDeclarationBlockContent(): void
165+
{
166+
$declarationBlock = new DeclarationBlock();
167+
$this->subject->setContents([$declarationBlock]);
168+
169+
$result = $this->subject->getAllDeclarationBlocks();
170+
171+
self::assertSame([$declarationBlock], $result);
172+
}
173+
174+
/**
175+
* @test
176+
*/
177+
public function getAllDeclarationBlocksCanReturnMultipleDirectDeclarationBlockContents(): void
178+
{
179+
$declarationBlock1 = new DeclarationBlock();
180+
$declarationBlock2 = new DeclarationBlock();
181+
$this->subject->setContents([$declarationBlock1, $declarationBlock2]);
182+
183+
$result = $this->subject->getAllDeclarationBlocks();
184+
185+
self::assertSame([$declarationBlock1, $declarationBlock2], $result);
186+
}
187+
188+
/**
189+
* @test
190+
*/
191+
public function getAllDeclarationBlocksAlsoReturnsDeclarationBlocksWithinAtRuleBlockList(): void
192+
{
193+
$declarationBlock = new DeclarationBlock();
194+
$atRuleBlockList = new AtRuleBlockList('media');
195+
$atRuleBlockList->setContents([$declarationBlock]);
196+
$this->subject->setContents([$atRuleBlockList]);
197+
198+
$result = $this->subject->getAllDeclarationBlocks();
199+
200+
self::assertSame([$declarationBlock], $result);
201+
}
202+
203+
/**
204+
* @test
205+
*/
206+
public function getAllDeclarationBlocksIgnoresImport(): void
207+
{
208+
$import = new Import(new URL(new CSSString('')), '');
209+
$this->subject->setContents([$import]);
210+
211+
$result = $this->subject->getAllDeclarationBlocks();
212+
213+
self::assertSame([], $result);
214+
}
215+
216+
/**
217+
* @test
218+
*/
219+
public function getAllDeclarationBlocksIgnoresCharset(): void
220+
{
221+
$Charset = new Charset(new CSSString(''));
222+
$this->subject->setContents([$Charset]);
223+
224+
$result = $this->subject->getAllDeclarationBlocks();
225+
226+
self::assertSame([], $result);
227+
}
145228
}

0 commit comments

Comments
 (0)