sebastianbergmann/comparator · error · ComparisonFailure

Failed asserting that two objects are equal.

Error message

Failed asserting that two objects are equal.

What it means

Thrown by SplObjectStorageComparator::assertEquals when two SplObjectStorage instances do not contain the same set of objects (and/or the same associated data per object). The comparator accepts only pairs where both operands are SplObjectStorage; it iterates the actual storage and checks membership/info in the expected one, throwing ComparisonFailure on the first difference. The faulting input is whichever storage contains an object missing from the other, or an object with different associated data.

Solutions

  1. Compare counts first (count($storage)) to see which side is missing entries.
  2. Check that objects are the same instances: SplObjectStorage uses object identity (spl_object_id), not equality, for membership.
  3. Verify the per-object data attached via attach($obj, $data) matches on both sides.
  4. Ensure the same object instances are attached in both storages if identity was intended, or switch to a comparator based on value equality.
  5. Rebuild the actual storage deterministically (same attach order/data) if iteration order matters to downstream code.
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at src/SplObjectStorageComparator.php:41 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of sebastianbergmann/comparator@00837a9d22 (2026-09-15). Data as JSON: /api/errors/95a2730a882ddaa8. Report an issue: GitHub.

Appendix: source

Thrown at src/SplObjectStorageComparator.php:41

    public function accepts(mixed $expected, mixed $actual): bool
    {
        return $expected instanceof SplObjectStorage && $actual instanceof SplObjectStorage;
    }

    /**
     * @throws ComparisonFailure
     * @throws ObjectNotSupportedException
     */
    public function assertEquals(mixed $expected, mixed $actual, float $delta = 0.0, bool $canonicalize = false, bool $ignoreCase = false): void
    {
        assert($expected instanceof SplObjectStorage);
        assert($actual instanceof SplObjectStorage);

        $exporter = $this->exporter();

        foreach ($actual as $object) {
            if (!$expected->offsetExists($object)) {
                throw new ComparisonFailure(
                    $expected,
                    $actual,
                    $exporter->export($expected),
                    $exporter->export($actual),
                    'Failed asserting that two objects are equal.',
                    $this->contextLines(),
                );
            }
        }

        foreach ($expected as $object) {
            if (!$actual->offsetExists($object)) {
                throw new ComparisonFailure(
                    $expected,
                    $actual,
                    $exporter->export($expected),
                    $exporter->export($actual),
                    'Failed asserting that two objects are equal.',

View on GitHub (pinned to 00837a9d22)