You are here

class PHPUnit_Framework_Constraint_IsIdentical in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/phpunit/phpunit/src/Framework/Constraint/IsIdentical.php \PHPUnit_Framework_Constraint_IsIdentical

Constraint that asserts that one value is identical to another.

Identical check is performed with PHP's === operator, the operator is explained in detail at {@url http://www.php.net/manual/en/types.comparisons.php}. Two values are identical if they have the same value and are of the same type.

The expected value is passed in the constructor.

@since Class available since Release 3.0.0

Hierarchy

Expanded class hierarchy of PHPUnit_Framework_Constraint_IsIdentical

File

vendor/phpunit/phpunit/src/Framework/Constraint/IsIdentical.php, line 24

View source
class PHPUnit_Framework_Constraint_IsIdentical extends PHPUnit_Framework_Constraint {

  /**
   * @var float
   */
  const EPSILON = 1.0E-10;

  /**
   * @var mixed
   */
  protected $value;

  /**
   * @param mixed $value
   */
  public function __construct($value) {
    parent::__construct();
    $this->value = $value;
  }

  /**
   * Evaluates the constraint for parameter $other
   *
   * If $returnResult is set to false (the 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 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
   * @return mixed
   * @throws PHPUnit_Framework_ExpectationFailedException
   */
  public function evaluate($other, $description = '', $returnResult = false) {
    if (is_double($this->value) && is_double($other) && !is_infinite($this->value) && !is_infinite($other) && !is_nan($this->value) && !is_nan($other)) {
      $success = abs($this->value - $other) < self::EPSILON;
    }
    else {
      $success = $this->value === $other;
    }
    if ($returnResult) {
      return $success;
    }
    if (!$success) {
      $f = null;

      // if both values are strings, make sure a diff is generated
      if (is_string($this->value) && is_string($other)) {
        $f = new SebastianBergmann\Comparator\ComparisonFailure($this->value, $other, $this->value, $other);
      }
      $this
        ->fail($other, $description, $f);
    }
  }

  /**
   * Returns the description of the failure
   *
   * The beginning of failure messages is "Failed asserting that" in most
   * cases. This method should return the second part of that sentence.
   *
   * @param  mixed  $other Evaluated value or object.
   * @return string
   */
  protected function failureDescription($other) {
    if (is_object($this->value) && is_object($other)) {
      return 'two variables reference the same object';
    }
    if (is_string($this->value) && is_string($other)) {
      return 'two strings are identical';
    }
    return parent::failureDescription($other);
  }

  /**
   * Returns a string representation of the constraint.
   *
   * @return string
   */
  public function toString() {
    if (is_object($this->value)) {
      return 'is identical to an object of class "' . get_class($this->value) . '"';
    }
    else {
      return 'is identical to ' . $this->exporter
        ->export($this->value);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PHPUnit_Framework_Constraint::$exporter protected property
PHPUnit_Framework_Constraint::additionalFailureDescription protected function Return additional failure description where needed 1
PHPUnit_Framework_Constraint::count public function Counts the number of constraint elements. 6
PHPUnit_Framework_Constraint::fail protected function Throws an exception for the given compared value and test description
PHPUnit_Framework_Constraint::matches protected function Evaluates the constraint for parameter $other. Returns true if the constraint is met, false otherwise. 25
PHPUnit_Framework_Constraint_IsIdentical::$value protected property
PHPUnit_Framework_Constraint_IsIdentical::EPSILON constant
PHPUnit_Framework_Constraint_IsIdentical::evaluate public function Evaluates the constraint for parameter $other Overrides PHPUnit_Framework_Constraint::evaluate
PHPUnit_Framework_Constraint_IsIdentical::failureDescription protected function Returns the description of the failure Overrides PHPUnit_Framework_Constraint::failureDescription
PHPUnit_Framework_Constraint_IsIdentical::toString public function Returns a string representation of the constraint. Overrides PHPUnit_Framework_SelfDescribing::toString
PHPUnit_Framework_Constraint_IsIdentical::__construct public function Overrides PHPUnit_Framework_Constraint::__construct