Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. IsIdentical.php

class IsIdentical

@no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit

Hierarchy

  • class \PHPUnit\Framework\Constraint\Constraint implements \Countable, \PHPUnit\Framework\SelfDescribing
    • class \PHPUnit\Framework\Constraint\IsIdentical extends \PHPUnit\Framework\Constraint\Constraint

Expanded class hierarchy of IsIdentical

3 files declare their use of IsIdentical
Assert.php in vendor/phpunit/phpunit/src/Framework/Assert.php
ConstraintValidatorTestCase.php in vendor/symfony/validator/Test/ConstraintValidatorTestCase.php
Functions.php in vendor/phpunit/phpunit/src/Framework/Assert/Functions.php

File

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

Namespace

PHPUnit\Framework\Constraint
View source
final class IsIdentical extends Constraint {
    private readonly mixed $value;
    public function __construct(mixed $value) {
        $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.
     *
     * @throws ExpectationFailedException
     */
    public function evaluate(mixed $other, string $description = '', bool $returnResult = false) : ?bool {
        $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 ComparisonFailure($this->value, $other, sprintf("'%s'", $this->value), sprintf("'%s'", $other));
            }
            // if both values are array or enums, make sure a diff is generated
            if (is_array($this->value) && is_array($other) || $this->value instanceof UnitEnum && $other instanceof UnitEnum) {
                $f = new ComparisonFailure($this->value, $other, Exporter::export($this->value, true), Exporter::export($other, true));
            }
            $this->fail($other, $description, $f);
        }
        return null;
    }
    
    /**
     * Returns a string representation of the constraint.
     */
    public function toString(bool $exportObjects = false) : string {
        if (is_object($this->value)) {
            return 'is identical to an object of class "' . $this->value::class . '"';
        }
        return 'is identical to ' . Exporter::export($this->value, $exportObjects);
    }
    
    /**
     * 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.
     */
    protected function failureDescription(mixed $other) : string {
        if (is_object($this->value) && is_object($other)) {
            return 'two variables reference the same object';
        }
        if (explode(' ', gettype($this->value), 2)[0] === 'resource' && explode(' ', gettype($other), 2)[0] === 'resource') {
            return 'two variables reference the same resource';
        }
        if (is_string($this->value) && is_string($other)) {
            return 'two strings are identical';
        }
        if (is_array($this->value) && is_array($other)) {
            return 'two arrays are identical';
        }
        return parent::failureDescription($other);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
Constraint::additionalFailureDescription protected function Return additional failure description where needed. 7
Constraint::count public function Counts the number of constraint elements. 3
Constraint::exporter protected function
Constraint::fail protected function Throws an exception for the given compared value and test description. 1
Constraint::failureDescriptionInContext protected function Returns the description of the failure when this constraint appears in
context of an $operator expression.
Constraint::matches protected function Evaluates the constraint for parameter $other. Returns true if the
constraint is met, false otherwise.
70
Constraint::reduce protected function Reduces the sub-expression starting at $this by skipping degenerate
sub-expression and returns first descendant constraint that starts
a non-reducible sub-expression.
2
Constraint::toStringInContext protected function Returns a custom string representation of the constraint object when it
appears in context of an $operator expression.
Constraint::valueToTypeStringFragment protected function @psalm-return non-empty-string
IsIdentical::$value private property
IsIdentical::evaluate public function Evaluates the constraint for parameter $other. Overrides Constraint::evaluate
IsIdentical::failureDescription protected function Returns the description of the failure. Overrides Constraint::failureDescription
IsIdentical::toString public function Returns a string representation of the constraint. Overrides SelfDescribing::toString
IsIdentical::__construct public function

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal