@@ -29,7 +29,7 @@ public function expandBorderShorthand($sCss, $sExpected)
29
29
foreach ($ oDoc ->getAllDeclarationBlocks () as $ oDeclaration ) {
30
30
$ oDeclaration ->expandBorderShorthand ();
31
31
}
32
- self ::assertSame (trim ((string )$ oDoc ), $ sExpected );
32
+ self ::assertDeclarationBlockEquals (trim ((string )$ oDoc ), $ sExpected );
33
33
}
34
34
35
35
/**
@@ -62,7 +62,7 @@ public function expandFontShorthand($sCss, $sExpected)
62
62
foreach ($ oDoc ->getAllDeclarationBlocks () as $ oDeclaration ) {
63
63
$ oDeclaration ->expandFontShorthand ();
64
64
}
65
- self ::assertSame (trim ((string )$ oDoc ), $ sExpected );
65
+ self ::assertDeclarationBlockEquals (trim ((string )$ oDoc ), $ sExpected );
66
66
}
67
67
68
68
/**
@@ -118,7 +118,7 @@ public function expandBackgroundShorthand($sCss, $sExpected)
118
118
foreach ($ oDoc ->getAllDeclarationBlocks () as $ oDeclaration ) {
119
119
$ oDeclaration ->expandBackgroundShorthand ();
120
120
}
121
- self ::assertSame (trim ((string )$ oDoc ), $ sExpected );
121
+ self ::assertDeclarationBlockEquals (trim ((string )$ oDoc ), $ sExpected );
122
122
}
123
123
124
124
/**
@@ -171,7 +171,7 @@ public function expandDimensionsShorthand($sCss, $sExpected)
171
171
foreach ($ oDoc ->getAllDeclarationBlocks () as $ oDeclaration ) {
172
172
$ oDeclaration ->expandDimensionsShorthand ();
173
173
}
174
- self ::assertSame (trim ((string )$ oDoc ), $ sExpected );
174
+ self ::assertDeclarationBlockEquals (trim ((string )$ oDoc ), $ sExpected );
175
175
}
176
176
177
177
/**
@@ -505,4 +505,50 @@ public function canRemoveCommentsFromRulesUsingStrictParsing(
505
505
506
506
self ::assertSame ($ cssWithoutComments , $ renderedDocument );
507
507
}
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
+ }
508
554
}
0 commit comments