From 2255abc4b82d510a67daab106aba819b843982bd Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Tue, 25 Feb 2025 11:56:08 +0100 Subject: [PATCH 1/2] [TASK] Move up `getAllDeclarationBlocks` to `CSSBlockList` Move this method up without any changes to the method or its callers. We'll clean this up in later changes. Part of #994. --- src/CSSList/CSSBlockList.php | 13 ++ src/CSSList/Document.php | 14 -- tests/Unit/CSSList/CSSBlockListTest.php | 94 ++++++++++ tests/Unit/CSSList/DocumentTest.php | 231 ------------------------ 4 files changed, 107 insertions(+), 245 deletions(-) diff --git a/src/CSSList/CSSBlockList.php b/src/CSSList/CSSBlockList.php index 76216279..0f2817c3 100644 --- a/src/CSSList/CSSBlockList.php +++ b/src/CSSList/CSSBlockList.php @@ -20,6 +20,19 @@ */ abstract class CSSBlockList extends CSSList { + /** + * Gets all `DeclarationBlock` objects recursively, no matter how deeply nested the selectors are. + * + * @return array + */ + public function getAllDeclarationBlocks(): array + { + /** @var array $result */ + $result = []; + $this->allDeclarationBlocks($result); + return $result; + } + /** * @param array $result */ diff --git a/src/CSSList/Document.php b/src/CSSList/Document.php index 00d4cabd..06a8064f 100644 --- a/src/CSSList/Document.php +++ b/src/CSSList/Document.php @@ -8,7 +8,6 @@ use Sabberworm\CSS\Parsing\ParserState; use Sabberworm\CSS\Parsing\SourceException; use Sabberworm\CSS\Property\Selector; -use Sabberworm\CSS\RuleSet\DeclarationBlock; use Sabberworm\CSS\RuleSet\RuleSet; use Sabberworm\CSS\Value\Value; @@ -30,19 +29,6 @@ public static function parse(ParserState $parserState): Document return $oDocument; } - /** - * Gets all `DeclarationBlock` objects recursively, no matter how deeply nested the selectors are. - * - * @return array - */ - public function getAllDeclarationBlocks(): array - { - /** @var array $result */ - $result = []; - $this->allDeclarationBlocks($result); - return $result; - } - /** * Returns all `RuleSet` objects recursively found in the tree, no matter how deeply nested the rule sets are. * diff --git a/tests/Unit/CSSList/CSSBlockListTest.php b/tests/Unit/CSSList/CSSBlockListTest.php index 9d7ef076..b44f7114 100644 --- a/tests/Unit/CSSList/CSSBlockListTest.php +++ b/tests/Unit/CSSList/CSSBlockListTest.php @@ -6,9 +6,15 @@ use PHPUnit\Framework\TestCase; use Sabberworm\CSS\Comment\Commentable; +use Sabberworm\CSS\CSSList\AtRuleBlockList; use Sabberworm\CSS\CSSList\CSSList; +use Sabberworm\CSS\Property\Charset; +use Sabberworm\CSS\Property\Import; use Sabberworm\CSS\Renderable; +use Sabberworm\CSS\RuleSet\DeclarationBlock; use Sabberworm\CSS\Tests\Unit\CSSList\Fixtures\ConcreteCSSBlockList; +use Sabberworm\CSS\Value\CSSString; +use Sabberworm\CSS\Value\URL; /** * @covers \Sabberworm\CSS\CSSList\CSSBlockList @@ -45,4 +51,92 @@ public function isCSSList(): void self::assertInstanceOf(CSSList::class, $subject); } + + /** + * @test + */ + public function getAllDeclarationBlocksForNoContentsReturnsEmptyArray(): void + { + $subject = new ConcreteCSSBlockList(); + + self::assertSame([], $subject->getAllDeclarationBlocks()); + } + + /** + * @test + */ + public function getAllDeclarationBlocksCanReturnOneDirectDeclarationBlockContent(): void + { + $subject = new ConcreteCSSBlockList(); + + $declarationBlock = new DeclarationBlock(); + $subject->setContents([$declarationBlock]); + + $result = $subject->getAllDeclarationBlocks(); + + self::assertSame([$declarationBlock], $result); + } + + /** + * @test + */ + public function getAllDeclarationBlocksCanReturnMultipleDirectDeclarationBlockContents(): void + { + $subject = new ConcreteCSSBlockList(); + + $declarationBlock1 = new DeclarationBlock(); + $declarationBlock2 = new DeclarationBlock(); + $subject->setContents([$declarationBlock1, $declarationBlock2]); + + $result = $subject->getAllDeclarationBlocks(); + + self::assertSame([$declarationBlock1, $declarationBlock2], $result); + } + + /** + * @test + */ + public function getAllDeclarationBlocksReturnsDeclarationBlocksWithinAtRuleBlockList(): void + { + $subject = new ConcreteCSSBlockList(); + + $declarationBlock = new DeclarationBlock(); + $atRuleBlockList = new AtRuleBlockList('media'); + $atRuleBlockList->setContents([$declarationBlock]); + $subject->setContents([$atRuleBlockList]); + + $result = $subject->getAllDeclarationBlocks(); + + self::assertSame([$declarationBlock], $result); + } + + /** + * @test + */ + public function getAllDeclarationBlocksIgnoresImport(): void + { + $subject = new ConcreteCSSBlockList(); + + $import = new Import(new URL(new CSSString('https://www.example.com/')), ''); + $subject->setContents([$import]); + + $result = $subject->getAllDeclarationBlocks(); + + self::assertSame([], $result); + } + + /** + * @test + */ + public function getAllDeclarationBlocksIgnoresCharset(): void + { + $subject = new ConcreteCSSBlockList(); + + $charset = new Charset(new CSSString('UTF-8')); + $subject->setContents([$charset]); + + $result = $subject->getAllDeclarationBlocks(); + + self::assertSame([], $result); + } } diff --git a/tests/Unit/CSSList/DocumentTest.php b/tests/Unit/CSSList/DocumentTest.php index 07afcf1b..77e8aa81 100644 --- a/tests/Unit/CSSList/DocumentTest.php +++ b/tests/Unit/CSSList/DocumentTest.php @@ -6,17 +6,10 @@ use PHPUnit\Framework\TestCase; use Sabberworm\CSS\Comment\Commentable; -use Sabberworm\CSS\CSSList\AtRuleBlockList; use Sabberworm\CSS\CSSList\CSSBlockList; use Sabberworm\CSS\CSSList\CSSList; use Sabberworm\CSS\CSSList\Document; -use Sabberworm\CSS\Property\Charset; -use Sabberworm\CSS\Property\Import; use Sabberworm\CSS\Renderable; -use Sabberworm\CSS\RuleSet\AtRuleSet; -use Sabberworm\CSS\RuleSet\DeclarationBlock; -use Sabberworm\CSS\Value\CSSString; -use Sabberworm\CSS\Value\URL; /** * @covers \Sabberworm\CSS\CSSList\CSSBlockList @@ -61,230 +54,6 @@ public function isCSSList(): void self::assertInstanceOf(CSSList::class, $subject); } - /** - * @test - */ - public function getAllDeclarationBlocksForNoContentsReturnsEmptyArray(): void - { - $subject = new Document(); - - self::assertSame([], $subject->getAllDeclarationBlocks()); - } - - /** - * @test - */ - public function getAllDeclarationBlocksCanReturnOneDirectDeclarationBlockContent(): void - { - $subject = new Document(); - - $declarationBlock = new DeclarationBlock(); - $subject->setContents([$declarationBlock]); - - $result = $subject->getAllDeclarationBlocks(); - - self::assertSame([$declarationBlock], $result); - } - - /** - * @test - */ - public function getAllDeclarationBlocksCanReturnMultipleDirectDeclarationBlockContents(): void - { - $subject = new Document(); - - $declarationBlock1 = new DeclarationBlock(); - $declarationBlock2 = new DeclarationBlock(); - $subject->setContents([$declarationBlock1, $declarationBlock2]); - - $result = $subject->getAllDeclarationBlocks(); - - self::assertSame([$declarationBlock1, $declarationBlock2], $result); - } - - /** - * @test - */ - public function getAllDeclarationBlocksReturnsDeclarationBlocksWithinAtRuleBlockList(): void - { - $subject = new Document(); - - $declarationBlock = new DeclarationBlock(); - $atRuleBlockList = new AtRuleBlockList('media'); - $atRuleBlockList->setContents([$declarationBlock]); - $subject->setContents([$atRuleBlockList]); - - $result = $subject->getAllDeclarationBlocks(); - - self::assertSame([$declarationBlock], $result); - } - - /** - * @test - */ - public function getAllDeclarationBlocksIgnoresImport(): void - { - $subject = new Document(); - - $import = new Import(new URL(new CSSString('https://www.example.com/')), ''); - $subject->setContents([$import]); - - $result = $subject->getAllDeclarationBlocks(); - - self::assertSame([], $result); - } - - /** - * @test - */ - public function getAllDeclarationBlocksIgnoresCharset(): void - { - $subject = new Document(); - - $charset = new Charset(new CSSString('UTF-8')); - $subject->setContents([$charset]); - - $result = $subject->getAllDeclarationBlocks(); - - self::assertSame([], $result); - } - - /** - * @test - */ - public function getAllRuleSetsWhenNoContentSetReturnsEmptyArray(): void - { - $subject = new Document(); - - self::assertSame([], $subject->getAllRuleSets()); - } - - /** - * @test - */ - public function getAllRuleSetsReturnsOneDeclarationBlockDirectlySetAsContent(): void - { - $subject = new Document(); - - $declarationBlock = new DeclarationBlock(); - $subject->setContents([$declarationBlock]); - - $result = $subject->getAllRuleSets(); - - self::assertSame([$declarationBlock], $result); - } - - /** - * @test - */ - public function getAllRuleSetsReturnsOneAtRuleSetDirectlySetAsContent(): void - { - $subject = new Document(); - - $atRuleSet = new AtRuleSet('media'); - $subject->setContents([$atRuleSet]); - - $result = $subject->getAllRuleSets(); - - self::assertSame([$atRuleSet], $result); - } - - /** - * @test - */ - public function getAllRuleSetsReturnsMultipleDeclarationBlocksDirectlySetAsContents(): void - { - $subject = new Document(); - - $declarationBlock1 = new DeclarationBlock(); - $declarationBlock2 = new DeclarationBlock(); - $subject->setContents([$declarationBlock1, $declarationBlock2]); - - $result = $subject->getAllRuleSets(); - - self::assertSame([$declarationBlock1, $declarationBlock2], $result); - } - - /** - * @test - */ - public function getAllRuleSetsReturnsMultipleAtRuleSetsDirectlySetAsContents(): void - { - $subject = new Document(); - - $atRuleSet1 = new AtRuleSet('media'); - $atRuleSet2 = new AtRuleSet('media'); - $subject->setContents([$atRuleSet1, $atRuleSet2]); - - $result = $subject->getAllRuleSets(); - - self::assertSame([$atRuleSet1, $atRuleSet2], $result); - } - - /** - * @test - */ - public function getAllRuleSetsReturnsDeclarationBlocksWithinAtRuleBlockList(): void - { - $subject = new Document(); - - $declarationBlock = new DeclarationBlock(); - $atRuleBlockList = new AtRuleBlockList('media'); - $atRuleBlockList->setContents([$declarationBlock]); - $subject->setContents([$atRuleBlockList]); - - $result = $subject->getAllRuleSets(); - - self::assertSame([$declarationBlock], $result); - } - - /** - * @test - */ - public function getAllRuleSetsReturnsAtRuleSetsWithinAtRuleBlockList(): void - { - $subject = new Document(); - - $atRule = new AtRuleSet('media'); - $atRuleBlockList = new AtRuleBlockList('media'); - $atRuleBlockList->setContents([$atRule]); - $subject->setContents([$atRuleBlockList]); - - $result = $subject->getAllRuleSets(); - - self::assertSame([$atRule], $result); - } - - /** - * @test - */ - public function getAllRuleSetsIgnoresImport(): void - { - $subject = new Document(); - - $import = new Import(new URL(new CSSString('https://www.example.com/')), ''); - $subject->setContents([$import]); - - $result = $subject->getAllRuleSets(); - - self::assertSame([], $result); - } - - /** - * @test - */ - public function getAllRuleSetsIgnoresCharset(): void - { - $subject = new Document(); - - $charset = new Charset(new CSSString('UTF-8')); - $subject->setContents([$charset]); - - $result = $subject->getAllRuleSets(); - - self::assertSame([], $result); - } - /** * @test */ From 5fcbbedf02fa4b261e53b0b6438b4b94768e3c6b Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Wed, 26 Feb 2025 11:28:38 +0100 Subject: [PATCH 2/2] Re-add tests that I accidentally deleted --- tests/Unit/CSSList/DocumentTest.php | 143 ++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) diff --git a/tests/Unit/CSSList/DocumentTest.php b/tests/Unit/CSSList/DocumentTest.php index 77e8aa81..509b4b9d 100644 --- a/tests/Unit/CSSList/DocumentTest.php +++ b/tests/Unit/CSSList/DocumentTest.php @@ -6,10 +6,17 @@ use PHPUnit\Framework\TestCase; use Sabberworm\CSS\Comment\Commentable; +use Sabberworm\CSS\CSSList\AtRuleBlockList; use Sabberworm\CSS\CSSList\CSSBlockList; use Sabberworm\CSS\CSSList\CSSList; use Sabberworm\CSS\CSSList\Document; +use Sabberworm\CSS\Property\Charset; +use Sabberworm\CSS\Property\Import; use Sabberworm\CSS\Renderable; +use Sabberworm\CSS\RuleSet\AtRuleSet; +use Sabberworm\CSS\RuleSet\DeclarationBlock; +use Sabberworm\CSS\Value\CSSString; +use Sabberworm\CSS\Value\URL; /** * @covers \Sabberworm\CSS\CSSList\CSSBlockList @@ -54,6 +61,142 @@ public function isCSSList(): void self::assertInstanceOf(CSSList::class, $subject); } + /** + * @test + */ + public function getAllRuleSetsWhenNoContentSetReturnsEmptyArray(): void + { + $subject = new Document(); + + self::assertSame([], $subject->getAllRuleSets()); + } + + /** + * @test + */ + public function getAllRuleSetsReturnsOneDeclarationBlockDirectlySetAsContent(): void + { + $subject = new Document(); + + $declarationBlock = new DeclarationBlock(); + $subject->setContents([$declarationBlock]); + + $result = $subject->getAllRuleSets(); + + self::assertSame([$declarationBlock], $result); + } + + /** + * @test + */ + public function getAllRuleSetsReturnsOneAtRuleSetDirectlySetAsContent(): void + { + $subject = new Document(); + + $atRuleSet = new AtRuleSet('media'); + $subject->setContents([$atRuleSet]); + + $result = $subject->getAllRuleSets(); + + self::assertSame([$atRuleSet], $result); + } + + /** + * @test + */ + public function getAllRuleSetsReturnsMultipleDeclarationBlocksDirectlySetAsContents(): void + { + $subject = new Document(); + + $declarationBlock1 = new DeclarationBlock(); + $declarationBlock2 = new DeclarationBlock(); + $subject->setContents([$declarationBlock1, $declarationBlock2]); + + $result = $subject->getAllRuleSets(); + + self::assertSame([$declarationBlock1, $declarationBlock2], $result); + } + + /** + * @test + */ + public function getAllRuleSetsReturnsMultipleAtRuleSetsDirectlySetAsContents(): void + { + $subject = new Document(); + + $atRuleSet1 = new AtRuleSet('media'); + $atRuleSet2 = new AtRuleSet('media'); + $subject->setContents([$atRuleSet1, $atRuleSet2]); + + $result = $subject->getAllRuleSets(); + + self::assertSame([$atRuleSet1, $atRuleSet2], $result); + } + + /** + * @test + */ + public function getAllRuleSetsReturnsDeclarationBlocksWithinAtRuleBlockList(): void + { + $subject = new Document(); + + $declarationBlock = new DeclarationBlock(); + $atRuleBlockList = new AtRuleBlockList('media'); + $atRuleBlockList->setContents([$declarationBlock]); + $subject->setContents([$atRuleBlockList]); + + $result = $subject->getAllRuleSets(); + + self::assertSame([$declarationBlock], $result); + } + + /** + * @test + */ + public function getAllRuleSetsReturnsAtRuleSetsWithinAtRuleBlockList(): void + { + $subject = new Document(); + + $atRule = new AtRuleSet('media'); + $atRuleBlockList = new AtRuleBlockList('media'); + $atRuleBlockList->setContents([$atRule]); + $subject->setContents([$atRuleBlockList]); + + $result = $subject->getAllRuleSets(); + + self::assertSame([$atRule], $result); + } + + /** + * @test + */ + public function getAllRuleSetsIgnoresImport(): void + { + $subject = new Document(); + + $import = new Import(new URL(new CSSString('https://www.example.com/')), ''); + $subject->setContents([$import]); + + $result = $subject->getAllRuleSets(); + + self::assertSame([], $result); + } + + /** + * @test + */ + public function getAllRuleSetsIgnoresCharset(): void + { + $subject = new Document(); + + $charset = new Charset(new CSSString('UTF-8')); + $subject->setContents([$charset]); + + $result = $subject->getAllRuleSets(); + + self::assertSame([], $result); + } + /** * @test */