Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IsEqualHtml should extend PhpUnit Constraint #194

Merged
Merged
38 changes: 32 additions & 6 deletions php/WP_Mock/Tools/Constraints/ExpectationsMet.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,32 @@
use Mockery;
use Exception;

class ExpectationsMet extends \PHPUnit\Framework\Constraint\Constraint
/**
* Expectations-met constraint.
*/
class ExpectationsMet extends Constraint
{
private $_mockery_message;
/** @var string */
private $failureDescription;

/**
* Evaluates the constraint for parameter $other.
*
* Returns true if the constraint is met, false otherwise.
*
* @param mixed $other
* @return bool
*/
public function matches($other): bool
{
try {
Mockery::getContainer()->mockery_verify();
} catch (Exception $e) {
$this->_mockery_message = $e->getMessage();
} catch (Exception $exception) {
$this->failureDescription = $exception->getMessage();

return false;
}

return true;
}

Expand All @@ -28,14 +42,26 @@ public function matches($other): bool
*/
public function toString(): string
{
return 'WP Mock expectations are met';
return 'WP_Mock expectations are met';
}

/**
* Gets the additional failure description.
*
* @param mixed $other
* @return string
*/
protected function additionalFailureDescription($other): string
{
return str_replace(array( "\r", "\n" ), '', (string) $this->_mockery_message);
return str_replace(["\r", "\n"], '', $this->failureDescription);
}

/**
* Gets the failure description.
*
* @param mixed $other
* @return string
*/
protected function failureDescription($other): string
{
return $this->toString();
Expand Down
98 changes: 66 additions & 32 deletions php/WP_Mock/Tools/Constraints/IsEqualHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,93 @@

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
*/
protected 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);
return $isEqual->evaluate($other, $description, $returnResult);

$isEqual = new IsEqual($this->value, $this->delta, $this->canonicalize, $this->ignoreCase);
$result = $isEqual->evaluate($other, $description, $returnResult);

return $returnResult ? $result : null;
}

/**
* 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();
}
}
37 changes: 31 additions & 6 deletions php/WP_Mock/Tools/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace WP_Mock\Tools;

use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestResult;
use Exception;
use InvalidArgumentException;
Expand Down Expand Up @@ -137,23 +138,47 @@ public function expectOutputString(string $expectedString): void
parent::expectOutputString($expectedString);
}

public function assertCurrentConditionsMet($message = '')
/**
* Asserts that the current test conditions have been met.
*
* @deprecated prefer {@see TestCase::assertConditionsMet())
*
* @param string $message
* @return void
*/
public function assertCurrentConditionsMet(string $message = ''): void
{
$this->assertThat(null, new ExpectationsMet(), $message);
$this->assertConditionsMet($message);
}

public function assertConditionsMet($message = '')
/**
* Asserts that the current test conditions have been met.
*
* @param string $message
* @return void
*/
public function assertConditionsMet(string $message = ''): void
{
$this->assertCurrentConditionsMet($message);
/** @phpstan-ignore-next-line it will never throw an exception */
$this->assertThat(null, new ExpectationsMet(), $message);
}

public function assertEqualsHTML($expected, $actual, $message = '')
/**
* Evaluates that an HTML string is equal to another.
*
* @param string $expected
* @param string $actual
* @param string $message
* @return void
* @throws ExpectationFailedException|Exception
*/
public function assertEqualsHtml(string $expected, string $actual, string $message = ''): void
{
$constraint = new IsEqualHtml($expected);

$this->assertThat($actual, $constraint, $message);
}


/**
* Mocks a static method of a class.
*
Expand Down
74 changes: 74 additions & 0 deletions tests/Unit/WP_Mock/Tools/Constraints/ExpectationsMetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace WP_Mock\Tests\Unit\WP_Mock\Tools\Constraints;

use Exception;
use ReflectionException;
use ReflectionMethod;
use ReflectionProperty;
use WP_Mock\Tests\WP_MockTestCase;
use WP_Mock\Tools\Constraints\ExpectationsMet;

/**
* @covers \WP_Mock\Tools\Constraints\ExpectationsMet
*/
final class ExpectationsMetTest extends WP_MockTestCase
{
/**
* @covers \WP_Mock\Tools\Constraints\ExpectationsMet::matches()
*
* @return void
* @throws Exception
*/
public function testMatches(): void
{
$constraint = new ExpectationsMet();

$this->assertTrue($constraint->matches(null));
}

/**
* @covers \WP_Mock\Tools\Constraints\ExpectationsMet::toString()
*
* @return void
* @throws Exception
*/
public function testCanConvertToString(): void
{
$this->assertSame('WP_Mock expectations are met', (new ExpectationsMet())->toString());
}

/**
* @covers \WP_Mock\Tools\Constraints\ExpectationsMet::failureDescription()
*
* @return void
* @throws ReflectionException|Exception
*/
public function testCanGetFailureDescription(): void
{
$constraint = new ExpectationsMet();
$method = new ReflectionMethod($constraint, 'failureDescription');
$method->setAccessible(true);

$this->assertSame('WP_Mock expectations are met', $method->invokeArgs($constraint, [null]));
}

/**
* @covers \WP_Mock\Tools\Constraints\ExpectationsMet::additionalFailureDescription()
*
* @return void
* @throws ReflectionException|Exception
*/
public function testCanGetAdditionalFailureDescription(): void
{
$constraint = new ExpectationsMet();
$method = new ReflectionMethod($constraint, 'additionalFailureDescription');
$method->setAccessible(true);
$property = new ReflectionProperty($constraint, 'failureDescription');
$property->setAccessible(true);

$property->setValue($constraint, "\n\nTest\r");

$this->assertSame('Test', $method->invokeArgs($constraint, [null]));
}
}
Loading