3 * This file is part of PHPUnit.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 use SebastianBergmann\Exporter\Exporter;
14 * Abstract base class for constraints which can be applied to any value.
16 * @since Interface available since Release 3.0.0
18 abstract class PHPUnit_Framework_Constraint implements Countable, PHPUnit_Framework_SelfDescribing
22 public function __construct()
24 $this->exporter = new Exporter;
28 * Evaluates the constraint for parameter $other
30 * If $returnResult is set to false (the default), an exception is thrown
31 * in case of a failure. null is returned otherwise.
33 * If $returnResult is true, the result of the evaluation is returned as
34 * a boolean value instead: true in case of success, false in case of a
37 * @param mixed $other Value or object to evaluate.
38 * @param string $description Additional information about the test
39 * @param bool $returnResult Whether to return a result or throw an exception
43 * @throws PHPUnit_Framework_ExpectationFailedException
45 public function evaluate($other, $description = '', $returnResult = false)
49 if ($this->matches($other)) {
58 $this->fail($other, $description);
63 * Evaluates the constraint for parameter $other. Returns true if the
64 * constraint is met, false otherwise.
66 * This method can be overridden to implement the evaluation algorithm.
68 * @param mixed $other Value or object to evaluate.
72 protected function matches($other)
78 * Counts the number of constraint elements.
82 * @since Method available since Release 3.4.0
84 public function count()
90 * Throws an exception for the given compared value and test description
92 * @param mixed $other Evaluated value or object.
93 * @param string $description Additional information about the test
94 * @param SebastianBergmann\Comparator\ComparisonFailure $comparisonFailure
96 * @throws PHPUnit_Framework_ExpectationFailedException
98 protected function fail($other, $description, SebastianBergmann\Comparator\ComparisonFailure $comparisonFailure = null)
100 $failureDescription = sprintf(
101 'Failed asserting that %s.',
102 $this->failureDescription($other)
105 $additionalFailureDescription = $this->additionalFailureDescription($other);
107 if ($additionalFailureDescription) {
108 $failureDescription .= "\n" . $additionalFailureDescription;
111 if (!empty($description)) {
112 $failureDescription = $description . "\n" . $failureDescription;
115 throw new PHPUnit_Framework_ExpectationFailedException(
122 * Return additional failure description where needed
124 * The function can be overridden to provide additional failure
125 * information like a diff
127 * @param mixed $other Evaluated value or object.
131 protected function additionalFailureDescription($other)
137 * Returns the description of the failure
139 * The beginning of failure messages is "Failed asserting that" in most
140 * cases. This method should return the second part of that sentence.
142 * To provide additional failure information additionalFailureDescription
145 * @param mixed $other Evaluated value or object.
149 protected function failureDescription($other)
151 return $this->exporter->export($other) . ' ' . $this->toString();