From 99ffe8945adfe2b52be22a9055531f33951e7fc6 Mon Sep 17 00:00:00 2001 From: Fulvio Notarstefano Date: Tue, 3 Jan 2023 15:24:40 +0800 Subject: [PATCH] IsEqualHtml should extend PhpUnit Constraint --- php/WP_Mock/Tools/Constraints/IsEqualHtml.php | 95 +++++++++++++------ 1 file changed, 64 insertions(+), 31 deletions(-) diff --git a/php/WP_Mock/Tools/Constraints/IsEqualHtml.php b/php/WP_Mock/Tools/Constraints/IsEqualHtml.php index 0d56210d..4803ffea 100644 --- a/php/WP_Mock/Tools/Constraints/IsEqualHtml.php +++ b/php/WP_Mock/Tools/Constraints/IsEqualHtml.php @@ -2,59 +2,92 @@ namespace WP_Mock\Tools\Constraints; +use Exception; +use PHPUnit\Framework\Constraint\Constraint; use PHPUnit\Framework\Constraint\IsEqual; +use PHPUnit\Framework\ExpectationFailedException; -class IsEqualHtml +/** + * HTML string constraint. + */ +class IsEqualHtml extends Constraint { - protected $IsEqual; + /** @var string */ protected $value; - /** - * @var float - */ + /** @var float */ private $delta; - /** - * @var int - */ - private $maxDepth; - - /** - * @var bool - */ + /** @var bool */ private $canonicalize; - /** - * @var bool - */ + /** @var bool */ private $ignoreCase; - public function __construct( - $value, - float $delta = 0.0, - int $maxDepth = 10, - bool $canonicalize = false, - bool $ignoreCase = false - ) { + /** + * Constructor. + * + * @param string $value + * @param float $delta + * @param bool $canonicalize + * @param bool $ignoreCase + */ + public function __construct(string $value, float $delta = 0.0, bool $canonicalize = false, bool $ignoreCase = false) + { $this->value = $value; $this->delta = $delta; - $this->maxDepth = $maxDepth; $this->canonicalize = $canonicalize; $this->ignoreCase = $ignoreCase; } - private function clean($thing) + /** + * Trims and removes tabs, newlines and return carriages from a string. + * + * @param string $value + * @return string + */ + private function clean(string $value): string { - $thing = preg_replace('/\n\s+/', '', $thing); - $thing = preg_replace('/\s\s+/', ' ', $thing); - return str_replace(array( "\r", "\n", "\t" ), '', $thing); + $value = preg_replace('/\n\s+/', '', $value) ?: ''; + $value = preg_replace('/\s\s+/', ' ', $value) ?: ''; + + return str_replace(array( "\r", "\n", "\t" ), '', $value); } - public function evaluate($other, $description = '', $returnResult = false) + /** + * Evaluates the constraint for parameter $other. + * + * If $returnResult is false (default), an exception is thrown in case of a failure. null is returned otherwise. + * If $returnResult is true, the result of the evaluation is returned as a boolean instead, based on success or failure. + * + * @param string $other value to evaluate + * @param string $description message used in failures + * @param bool $returnResult whether to throw an exception in case of failure or return boolean + * @return bool|null + * @throws ExpectationFailedException + */ + public function evaluate($other, string $description = '', bool $returnResult = false): ?bool { - $other = $this->clean($other); + $other = $this->clean($other); $this->value = $this->clean($this->value); - $isEqual = new IsEqual($this->value, $this->delta, $this->maxDepth, $this->canonicalize, $this->ignoreCase); + + $isEqual = new IsEqual($this->value, $this->delta, $this->canonicalize, $this->ignoreCase); + return $isEqual->evaluate($other, $description, $returnResult); } + + /** + * Returns a string representation of the constraint. + * + * @see Constraint::toString() + * + * @return string + * @throws Exception + */ + public function toString(): string + { + $isEqual = new IsEqual($this->value, $this->delta, $this->canonicalize, $this->ignoreCase); + + return 'html '.$isEqual->toString(); + } }