Skip to content

Commit 3cd1d06

Browse files
authored
[TASK] Allow rules to be in any order in shorthand tests (#1065)
This is a pre-patch for #1062.
1 parent 8804463 commit 3cd1d06

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

tests/ParserTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Sabberworm\CSS\RuleSet\DeclarationBlock;
2020
use Sabberworm\CSS\RuleSet\RuleSet;
2121
use Sabberworm\CSS\Settings;
22+
use Sabberworm\CSS\Tests\RuleSet\DeclarationBlockTest;
2223
use Sabberworm\CSS\Value\Color;
2324
use Sabberworm\CSS\Value\Size;
2425
use Sabberworm\CSS\Value\URL;
@@ -511,7 +512,7 @@ public function expandShorthands()
511512
. 'font-family: "Trebuchet MS",Georgia,serif;background-color: #ccc;'
512513
. 'background-image: url("/images/foo.png");background-repeat: no-repeat;background-attachment: scroll;'
513514
. 'background-position: left top;}';
514-
self::assertSame($sExpected, $oDoc->render());
515+
DeclarationBlockTest::assertDeclarationBlockEquals($sExpected, $oDoc->render());
515516
}
516517

517518
/**
@@ -528,7 +529,7 @@ public function createShorthands()
528529
$oDoc->createShorthands();
529530
$sExpected = 'body {background: #fff url("foobar.png") repeat-y;margin: 2px 5px 4px 3px;'
530531
. 'border: 2px dotted #999;font: bold 2em Helvetica,Arial,sans-serif;}';
531-
self::assertSame($sExpected, $oDoc->render());
532+
DeclarationBlockTest::assertDeclarationBlockEquals($sExpected, $oDoc->render());
532533
}
533534

534535
/**

tests/RuleSet/DeclarationBlockTest.php

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function expandBorderShorthand($sCss, $sExpected)
2929
foreach ($oDoc->getAllDeclarationBlocks() as $oDeclaration) {
3030
$oDeclaration->expandBorderShorthand();
3131
}
32-
self::assertSame(trim((string)$oDoc), $sExpected);
32+
self::assertDeclarationBlockEquals(trim((string)$oDoc), $sExpected);
3333
}
3434

3535
/**
@@ -62,7 +62,7 @@ public function expandFontShorthand($sCss, $sExpected)
6262
foreach ($oDoc->getAllDeclarationBlocks() as $oDeclaration) {
6363
$oDeclaration->expandFontShorthand();
6464
}
65-
self::assertSame(trim((string)$oDoc), $sExpected);
65+
self::assertDeclarationBlockEquals(trim((string)$oDoc), $sExpected);
6666
}
6767

6868
/**
@@ -118,7 +118,7 @@ public function expandBackgroundShorthand($sCss, $sExpected)
118118
foreach ($oDoc->getAllDeclarationBlocks() as $oDeclaration) {
119119
$oDeclaration->expandBackgroundShorthand();
120120
}
121-
self::assertSame(trim((string)$oDoc), $sExpected);
121+
self::assertDeclarationBlockEquals(trim((string)$oDoc), $sExpected);
122122
}
123123

124124
/**
@@ -171,7 +171,7 @@ public function expandDimensionsShorthand($sCss, $sExpected)
171171
foreach ($oDoc->getAllDeclarationBlocks() as $oDeclaration) {
172172
$oDeclaration->expandDimensionsShorthand();
173173
}
174-
self::assertSame(trim((string)$oDoc), $sExpected);
174+
self::assertDeclarationBlockEquals(trim((string)$oDoc), $sExpected);
175175
}
176176

177177
/**
@@ -505,4 +505,50 @@ public function canRemoveCommentsFromRulesUsingStrictParsing(
505505

506506
self::assertSame($cssWithoutComments, $renderedDocument);
507507
}
508+
509+
/**
510+
* Asserts two declaration blocks are equivalent, allowing the rules to be in any order.
511+
*
512+
* @param string $expected
513+
* @param string $actual
514+
*/
515+
public static function assertDeclarationBlockEquals($expected, $actual)
516+
{
517+
$normalizedExpected = self::sortRulesInDeclarationBlock($expected);
518+
$normalizedActual = self::sortRulesInDeclarationBlock($actual);
519+
520+
self::assertSame($normalizedExpected, $normalizedActual);
521+
}
522+
523+
/**
524+
* Sorts the rules within a declaration block by property name.
525+
*
526+
* @param string $declarationBlock
527+
*
528+
* @return string
529+
*/
530+
private static function sortRulesInDeclarationBlock($declarationBlock)
531+
{
532+
// Match everything between `{` and `}`.
533+
return \preg_replace_callback(
534+
'/(?<=\\{)[^\\}]*+(?=\\})/',
535+
[self::class, 'sortDeclarationBlockRules'],
536+
$declarationBlock
537+
);
538+
}
539+
540+
/**
541+
* Sorts rules from within a declaration block by property name.
542+
*
543+
* @param array{0: string} $rulesMatches
544+
* This method is intended as a callback for `preg_replace_callback`.
545+
*
546+
* @return string
547+
*/
548+
private static function sortDeclarationBlockRules($rulesMatches)
549+
{
550+
$rules = \explode(';', $rulesMatches[0]);
551+
\sort($rules);
552+
return \implode(';', $rules);
553+
}
508554
}

0 commit comments

Comments
 (0)