Skip to content

Commit

Permalink
Changes suggested in 2nd code review:
Browse files Browse the repository at this point in the history
- “class names” -> “names of classes” in PHPDoc;
- Added specialized assertion methods: `assertSubstringCount` and
  `assertMinified`.
  • Loading branch information
JakeQZ committed Sep 27, 2019
1 parent 60a232f commit b4b18fd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Emogrifier/HtmlProcessor/HtmlPruner.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function removeElementsWithDisplayNone()
* This method also has the (presumably beneficial) side-effect of minifying (removing superfluous whitespace from)
* `class` attributes.
*
* @param string[] $classesToKeep class names that should not be removed
* @param string[] $classesToKeep names of classes that should not be removed
*
* @return self fluent interface
*/
Expand Down
39 changes: 32 additions & 7 deletions tests/Unit/Emogrifier/HtmlProcessor/HtmlPrunerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,7 @@ public function removeRedundantClassesNotRemovesClassesToKeep($html, array $clas
$result = $subject->render();
foreach ($classesToKeep as $class) {
$expectedInstanceCount = \substr_count($html, $class);
self::assertSame(
$expectedInstanceCount,
\substr_count($result, $class),
'asserting \'' . $result . '\' contains ' . $expectedInstanceCount . ' instance(s) of "' . $class
. '"'
);
self::assertSubstringCount($expectedInstanceCount, $result, $class);
}
}

Expand All @@ -360,7 +355,37 @@ public function removeRedundantClassesMinifiesClassAttributes($html, array $clas

\preg_match_all('/class="([^"]*+)"/', $subject->render(), $classAttributeMatches);
foreach ($classAttributeMatches[1] as $classAttributeValue) {
self::assertNotRegExp('/^\\s|\\s{2}|\\s$/', $classAttributeValue);
self::assertMinified($classAttributeValue);
}
}

/**
* Asserts that the number of occurrences of `$needle` within the string `$haystack` is as expected.
*
* @param int $expectedCount
* @param string $haystack
* @param string $needle
*
* @throws \PHPUnit_Framework_AssertionFailedError
*/
public static function assertSubstringCount($expectedCount, $haystack, $needle)
{
self::assertSame(
$expectedCount,
\substr_count($haystack, $needle),
'asserting \'' . $haystack . '\' contains ' . $expectedCount . ' instance(s) of "' . $needle . '"'
);
}

/**
* Asserts that a string does not contain consecutive whitespace characters, or begin or end with whitespace.
*
* @param string $string
*
* @throws \PHPUnit_Framework_AssertionFailedError
*/
public static function assertMinified($string)
{
self::assertNotRegExp('/^\\s|\\s{2}|\\s$/', $string);
}
}

0 comments on commit b4b18fd

Please sign in to comment.