Skip to content

Commit

Permalink
IsEqualHtml should extend PhpUnit Constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
unfulvio-godaddy committed Jan 3, 2023
1 parent 212424d commit 99ffe89
Showing 1 changed file with 64 additions and 31 deletions.
95 changes: 64 additions & 31 deletions php/WP_Mock/Tools/Constraints/IsEqualHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 99ffe89

Please sign in to comment.