Skip to content

Commit

Permalink
Closes #3060
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jan 19, 2019
1 parent 9e7ba24 commit 4afeedd
Show file tree
Hide file tree
Showing 32 changed files with 40 additions and 128 deletions.
1 change: 1 addition & 0 deletions ChangeLog-8.0.md
Expand Up @@ -6,6 +6,7 @@ All notable changes of the PHPUnit 8.0 release series are documented in this fil

### Changed

* Implemented [#3060](https://github.com/sebastianbergmann/phpunit/issues/3060): Cleanup `PHPUnit\Framework\Constraint\Constraint`
* Implemented [#3133](https://github.com/sebastianbergmann/phpunit/issues/3133): Enable dependency resolution by default
* Implemented [#3236](https://github.com/sebastianbergmann/phpunit/issues/3236): Define which parts of PHPUnit are covered by the backward compatibility promise
* Implemented [#3244](https://github.com/sebastianbergmann/phpunit/issues/3244): Enable result cache by default
Expand Down
4 changes: 1 addition & 3 deletions src/Framework/Constraint/ArrayHasKey.php
Expand Up @@ -31,8 +31,6 @@ final class ArrayHasKey extends Constraint
*/
public function __construct($key)
{
parent::__construct();

$this->key = $key;
}

Expand All @@ -43,7 +41,7 @@ public function __construct($key)
*/
public function toString(): string
{
return 'has the key ' . $this->exporter->export($this->key);
return 'has the key ' . $this->exporter()->export($this->key);
}

/**
Expand Down
10 changes: 2 additions & 8 deletions src/Framework/Constraint/ArraySubset.php
Expand Up @@ -32,8 +32,6 @@ final class ArraySubset extends Constraint

public function __construct(iterable $subset, bool $strict = false)
{
parent::__construct();

$this->strict = $strict;
$this->subset = $subset;
}
Expand All @@ -48,14 +46,10 @@ public function __construct(iterable $subset, bool $strict = false)
* a boolean value instead: true in case of success, false in case of a
* failure.
*
* @param mixed $other value or object to evaluate
* @param string $description Additional information about the test
* @param bool $returnResult Whether to return a result or throw an exception
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function evaluate($other, $description = '', $returnResult = false)
public function evaluate($other, string $description = '', bool $returnResult = false)
{
//type cast $other & $this->subset as an array to allow
//support in standard array functions.
Expand Down Expand Up @@ -93,7 +87,7 @@ public function evaluate($other, $description = '', $returnResult = false)
*/
public function toString(): string
{
return 'has the subset ' . $this->exporter->export($this->subset);
return 'has the subset ' . $this->exporter()->export($this->subset);
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/Framework/Constraint/Attribute.php
Expand Up @@ -36,16 +36,12 @@ public function __construct(Constraint $constraint, string $attributeName)
* a boolean value instead: true in case of success, false in case of a
* failure.
*
* @param mixed $other value or object to evaluate
* @param string $description Additional information about the test
* @param bool $returnResult Whether to return a result or throw an exception
*
* @throws ExpectationFailedException
* @throws \PHPUnit\Framework\Exception
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
* @throws \ReflectionException
*/
public function evaluate($other, $description = '', $returnResult = false)
public function evaluate($other, string $description = '', bool $returnResult = false)
{
return parent::evaluate(
Assert::readAttribute(
Expand Down
2 changes: 0 additions & 2 deletions src/Framework/Constraint/Callback.php
Expand Up @@ -21,8 +21,6 @@ final class Callback extends Constraint

public function __construct(callable $callback)
{
parent::__construct();

$this->callback = $callback;
}

Expand Down
2 changes: 0 additions & 2 deletions src/Framework/Constraint/ClassHasAttribute.php
Expand Up @@ -26,8 +26,6 @@ class ClassHasAttribute extends Constraint

public function __construct(string $attributeName)
{
parent::__construct();

$this->attributeName = $attributeName;
}

Expand Down
8 changes: 1 addition & 7 deletions src/Framework/Constraint/Composite.php
Expand Up @@ -20,8 +20,6 @@ abstract class Composite extends Constraint

public function __construct(Constraint $innerConstraint)
{
parent::__construct();

$this->innerConstraint = $innerConstraint;
}

Expand All @@ -35,14 +33,10 @@ public function __construct(Constraint $innerConstraint)
* a boolean value instead: true in case of success, false in case of a
* failure.
*
* @param mixed $other value or object to evaluate
* @param string $description Additional information about the test
* @param bool $returnResult Whether to return a result or throw an exception
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function evaluate($other, $description = '', $returnResult = false)
public function evaluate($other, string $description = '', bool $returnResult = false)
{
try {
return $this->innerConstraint->evaluate(
Expand Down
27 changes: 15 additions & 12 deletions src/Framework/Constraint/Constraint.php
Expand Up @@ -20,12 +20,10 @@
*/
abstract class Constraint implements Countable, SelfDescribing
{
protected $exporter;

public function __construct()
{
$this->exporter = new Exporter;
}
/**
* @var Exporter
*/
private $exporter;

/**
* Evaluates the constraint for parameter $other
Expand All @@ -37,14 +35,10 @@ public function __construct()
* a boolean value instead: true in case of success, false in case of a
* failure.
*
* @param mixed $other value or object to evaluate
* @param string $description Additional information about the test
* @param bool $returnResult Whether to return a result or throw an exception
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function evaluate($other, $description = '', $returnResult = false)
public function evaluate($other, string $description = '', bool $returnResult = false)
{
$success = false;

Expand All @@ -69,6 +63,15 @@ public function count(): int
return 1;
}

protected function exporter(): Exporter
{
if ($this->exporter === null) {
$this->exporter = new Exporter;
}

return $this->exporter;
}

/**
* Evaluates the constraint for parameter $other. Returns true if the
* constraint is met, false otherwise.
Expand Down Expand Up @@ -143,6 +146,6 @@ protected function additionalFailureDescription($other): string
*/
protected function failureDescription($other): string
{
return $this->exporter->export($other) . ' ' . $this->toString();
return $this->exporter()->export($other) . ' ' . $this->toString();
}
}
2 changes: 0 additions & 2 deletions src/Framework/Constraint/Count.php
Expand Up @@ -24,8 +24,6 @@ class Count extends Constraint

public function __construct(int $expected)
{
parent::__construct();

$this->expectedCount = $expected;
}

Expand Down
2 changes: 0 additions & 2 deletions src/Framework/Constraint/Exception.php
Expand Up @@ -21,8 +21,6 @@ final class Exception extends Constraint

public function __construct(string $className)
{
parent::__construct();

$this->className = $className;
}

Expand Down
6 changes: 2 additions & 4 deletions src/Framework/Constraint/ExceptionCode.php
Expand Up @@ -21,8 +21,6 @@ final class ExceptionCode extends Constraint
*/
public function __construct($expected)
{
parent::__construct();

$this->expectedCode = $expected;
}

Expand Down Expand Up @@ -56,8 +54,8 @@ protected function failureDescription($other): string
{
return \sprintf(
'%s is equal to expected exception code %s',
$this->exporter->export($other->getCode()),
$this->exporter->export($this->expectedCode)
$this->exporter()->export($other->getCode()),
$this->exporter()->export($this->expectedCode)
);
}
}
2 changes: 0 additions & 2 deletions src/Framework/Constraint/ExceptionMessage.php
Expand Up @@ -18,8 +18,6 @@ final class ExceptionMessage extends Constraint

public function __construct(string $expected)
{
parent::__construct();

$this->expectedMessage = $expected;
}

Expand Down
Expand Up @@ -20,8 +20,6 @@ final class ExceptionMessageRegularExpression extends Constraint

public function __construct(string $expected)
{
parent::__construct();

$this->expectedMessageRegExp = $expected;
}

Expand Down
4 changes: 1 addition & 3 deletions src/Framework/Constraint/GreaterThan.php
Expand Up @@ -25,8 +25,6 @@ final class GreaterThan extends Constraint
*/
public function __construct($value)
{
parent::__construct();

$this->value = $value;
}

Expand All @@ -37,7 +35,7 @@ public function __construct($value)
*/
public function toString(): string
{
return 'is greater than ' . $this->exporter->export($this->value);
return 'is greater than ' . $this->exporter()->export($this->value);
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/Framework/Constraint/IsAnything.php
Expand Up @@ -26,13 +26,9 @@ final class IsAnything extends Constraint
* a boolean value instead: true in case of success, false in case of a
* failure.
*
* @param mixed $other value or object to evaluate
* @param string $description Additional information about the test
* @param bool $returnResult Whether to return a result or throw an exception
*
* @throws ExpectationFailedException
*/
public function evaluate($other, $description = '', $returnResult = false)
public function evaluate($other, string $description = '', bool $returnResult = false)
{
return $returnResult ? true : null;
}
Expand Down
10 changes: 2 additions & 8 deletions src/Framework/Constraint/IsEqual.php
Expand Up @@ -51,8 +51,6 @@ final class IsEqual extends Constraint

public function __construct($value, float $delta = 0.0, int $maxDepth = 10, bool $canonicalize = false, bool $ignoreCase = false)
{
parent::__construct();

$this->value = $value;
$this->delta = $delta;
$this->maxDepth = $maxDepth;
Expand All @@ -70,13 +68,9 @@ public function __construct($value, float $delta = 0.0, int $maxDepth = 10, bool
* a boolean value instead: true in case of success, false in case of a
* failure.
*
* @param mixed $other value or object to evaluate
* @param string $description Additional information about the test
* @param bool $returnResult Whether to return a result or throw an exception
*
* @throws ExpectationFailedException
*/
public function evaluate($other, $description = '', $returnResult = false)
public function evaluate($other, string $description = '', bool $returnResult = false)
{
// If $this->value and $other are identical, they are also equal.
// This is the most common path and will allow us to skip
Expand Down Expand Up @@ -143,7 +137,7 @@ public function toString(): string

return \sprintf(
'is equal to %s%s',
$this->exporter->export($this->value),
$this->exporter()->export($this->value),
$delta
);
}
Expand Down
14 changes: 4 additions & 10 deletions src/Framework/Constraint/IsIdentical.php
Expand Up @@ -37,8 +37,6 @@ final class IsIdentical extends Constraint

public function __construct($value)
{
parent::__construct();

$this->value = $value;
}

Expand All @@ -52,14 +50,10 @@ public function __construct($value)
* a boolean value instead: true in case of success, false in case of a
* failure.
*
* @param mixed $other value or object to evaluate
* @param string $description Additional information about the test
* @param bool $returnResult Whether to return a result or throw an exception
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function evaluate($other, $description = '', $returnResult = false)
public function evaluate($other, string $description = '', bool $returnResult = false)
{
if (\is_float($this->value) && \is_float($other) &&
!\is_infinite($this->value) && !\is_infinite($other) &&
Expand Down Expand Up @@ -91,8 +85,8 @@ public function evaluate($other, $description = '', $returnResult = false)
$f = new ComparisonFailure(
$this->value,
$other,
$this->exporter->export($this->value),
$this->exporter->export($other)
$this->exporter()->export($this->value),
$this->exporter()->export($other)
);
}

Expand All @@ -112,7 +106,7 @@ public function toString(): string
\get_class($this->value) . '"';
}

return 'is identical to ' . $this->exporter->export($this->value);
return 'is identical to ' . $this->exporter()->export($this->value);
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/Framework/Constraint/IsInstanceOf.php
Expand Up @@ -27,8 +27,6 @@ final class IsInstanceOf extends Constraint

public function __construct(string $className)
{
parent::__construct();

$this->className = $className;
}

Expand Down Expand Up @@ -69,7 +67,7 @@ protected function failureDescription($other): string
{
return \sprintf(
'%s is an instance of %s "%s"',
$this->exporter->shortenedExport($other),
$this->exporter()->shortenedExport($other),
$this->getType(),
$this->className
);
Expand Down
2 changes: 1 addition & 1 deletion src/Framework/Constraint/IsJson.php
Expand Up @@ -66,7 +66,7 @@ protected function failureDescription($other): string

return \sprintf(
'%s is valid JSON (%s)',
$this->exporter->shortenedExport($other),
$this->exporter()->shortenedExport($other),
$error
);
}
Expand Down
2 changes: 0 additions & 2 deletions src/Framework/Constraint/IsType.php
Expand Up @@ -73,8 +73,6 @@ final class IsType extends Constraint
*/
public function __construct(string $type)
{
parent::__construct();

if (!isset(self::KNOWN_TYPES[$type])) {
throw new \PHPUnit\Framework\Exception(
\sprintf(
Expand Down
2 changes: 0 additions & 2 deletions src/Framework/Constraint/JsonMatches.php
Expand Up @@ -25,8 +25,6 @@ final class JsonMatches extends Constraint

public function __construct(string $value)
{
parent::__construct();

$this->value = $value;
}

Expand Down

0 comments on commit 4afeedd

Please sign in to comment.