From f72bc6989046d45a707f0e8de75ff9f181a85524 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Sat, 1 Mar 2025 13:14:41 +0100 Subject: [PATCH] [TASK] Move up `getAllRuleSets` 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 | 13 --- tests/Unit/CSSList/CSSBlockListTest.php | 137 +++++++++++++++++++++++ tests/Unit/CSSList/DocumentTest.php | 143 ------------------------ 4 files changed, 150 insertions(+), 156 deletions(-) diff --git a/src/CSSList/CSSBlockList.php b/src/CSSList/CSSBlockList.php index cae6d9f0..0f8bbe36 100644 --- a/src/CSSList/CSSBlockList.php +++ b/src/CSSList/CSSBlockList.php @@ -54,6 +54,19 @@ protected function allDeclarationBlocks(array &$result): void } } + /** + * Returns all `RuleSet` objects recursively found in the tree, no matter how deeply nested the rule sets are. + * + * @return array + */ + public function getAllRuleSets(): array + { + /** @var array $result */ + $result = []; + $this->allRuleSets($result); + return $result; + } + /** * @param array $result */ diff --git a/src/CSSList/Document.php b/src/CSSList/Document.php index 06a8064f..bdf168e5 100644 --- a/src/CSSList/Document.php +++ b/src/CSSList/Document.php @@ -29,19 +29,6 @@ public static function parse(ParserState $parserState): Document return $oDocument; } - /** - * Returns all `RuleSet` objects recursively found in the tree, no matter how deeply nested the rule sets are. - * - * @return array - */ - public function getAllRuleSets(): array - { - /** @var array $result */ - $result = []; - $this->allRuleSets($result); - return $result; - } - /** * Returns all `Value` objects found recursively in `Rule`s in the tree. * diff --git a/tests/Unit/CSSList/CSSBlockListTest.php b/tests/Unit/CSSList/CSSBlockListTest.php index 05a50dac..0028288f 100644 --- a/tests/Unit/CSSList/CSSBlockListTest.php +++ b/tests/Unit/CSSList/CSSBlockListTest.php @@ -11,6 +11,7 @@ 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\Tests\Unit\CSSList\Fixtures\ConcreteCSSBlockList; use Sabberworm\CSS\Value\CSSString; @@ -139,4 +140,140 @@ public function getAllDeclarationBlocksIgnoresCharset(): void self::assertSame([], $result); } + + /** + * @test + */ + public function getAllRuleSetsWhenNoContentSetReturnsEmptyArray(): void + { + $subject = new ConcreteCSSBlockList(); + + self::assertSame([], $subject->getAllRuleSets()); + } + + /** + * @test + */ + public function getAllRuleSetsReturnsOneDeclarationBlockDirectlySetAsContent(): void + { + $subject = new ConcreteCSSBlockList(); + + $declarationBlock = new DeclarationBlock(); + $subject->setContents([$declarationBlock]); + + $result = $subject->getAllRuleSets(); + + self::assertSame([$declarationBlock], $result); + } + + /** + * @test + */ + public function getAllRuleSetsReturnsOneAtRuleSetDirectlySetAsContent(): void + { + $subject = new ConcreteCSSBlockList(); + + $atRuleSet = new AtRuleSet('media'); + $subject->setContents([$atRuleSet]); + + $result = $subject->getAllRuleSets(); + + self::assertSame([$atRuleSet], $result); + } + + /** + * @test + */ + public function getAllRuleSetsReturnsMultipleDeclarationBlocksDirectlySetAsContents(): void + { + $subject = new ConcreteCSSBlockList(); + + $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 ConcreteCSSBlockList(); + + $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 ConcreteCSSBlockList(); + + $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 ConcreteCSSBlockList(); + + $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 ConcreteCSSBlockList(); + + $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 ConcreteCSSBlockList(); + + $charset = new Charset(new CSSString('UTF-8')); + $subject->setContents([$charset]); + + $result = $subject->getAllRuleSets(); + + self::assertSame([], $result); + } } diff --git a/tests/Unit/CSSList/DocumentTest.php b/tests/Unit/CSSList/DocumentTest.php index 509b4b9d..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,142 +54,6 @@ 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 */