sebastianbergmann / comparator

Provides the functionality to compare PHP values for equality.
BSD 3-Clause "New" or "Revised" License
6.97k stars 67 forks source link

Arrays with different keys and the same values are considered equal in canonicalize mode #112

Closed mgleska closed 1 month ago

mgleska commented 1 month ago
Q A
comparator version 6.0.1
PHP version 8.3.9
Installation method Composer

Summary

Consider following arrays: ['a' => 1 , 'b' => 2] and ['c' => 1, 'd' => 2] Comparing these arrays in canonicalize mode do not throw ComparisonFailure exception.

How to reproduce

<?php declare(strict_types=1);

namespace MGleska\Issue;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use SebastianBergmann\Comparator\ArrayComparator;
use SebastianBergmann\Comparator\ComparisonFailure;
use SebastianBergmann\Comparator\Factory;

#[CoversClass(ArrayComparator::class)]
class IssueArrayComparator extends TestCase
{
    private ArrayComparator $comparator;

    protected function setUp(): void
    {
        $this->comparator = new ArrayComparator;
        $this->comparator->setFactory(new Factory);
    }

    public function testOne(): void
    {
        $this->expectException(ComparisonFailure::class);
        $this->expectExceptionMessage('Failed asserting that two arrays are equal');

        $this->comparator->assertEquals(['a' => 1 , 'b' => 2], ['c' => 1, 'd' => 2], 0, true);
    }

    public function testTwo(): void
    {
        $this->expectException(ComparisonFailure::class);
        $this->expectExceptionMessage('Failed asserting that two arrays are equal');

        $this->comparator->assertEquals(['a' => 1 , 'b' => 2], ['c' => 1, 'd' => 2]);  // without canonicalization
    }
}

Current response

$ vendor/bin/phpunit tests/IssueArrayComparator.php
PHPUnit 11.3.0 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.3.9
Configuration: C:\prog\phpunit-comparator\phpunit.xml

F.                                                                  2 / 2 (100%)

Time: 00:00.031, Memory: 8.00 MB

There was 1 failure:

1) MGleska\Issue\IssueArrayComparator::testOne
Failed asserting that exception of type "SebastianBergmann\Comparator\ComparisonFailure" is thrown.

FAILURES!
Tests: 2, Assertions: 3, Failures: 1.

Expected response Both tests passing with success.

mgleska commented 1 month ago

Proposed solution in PR #113

Bilge commented 2 weeks ago

This doesn't seem backwards-compatible at all. Even though it may be considered "wrong" to only compare values, ignoring keys, some of my tests were relying on this behaviour, so I'm not sure why this was released with a minor version bump.