Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mgleska opened this issue Aug 11, 2024 · 3 comments
Labels

Comments

@mgleska
Copy link
Contributor

mgleska commented Aug 11, 2024

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
Copy link
Contributor Author

mgleska commented Aug 11, 2024

Proposed solution in PR #113

@sebastianbergmann sebastianbergmann changed the title Arrays with different keys and the same values are recognized as equals in canonicalize mode Arrays with different keys and the same values are considered equal in canonicalize mode Aug 12, 2024
@Bilge
Copy link

Bilge commented Aug 30, 2024

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.

@olivier-zenchef
Copy link

It is a breaking change because it's even throwing with int keys: sebastianbergmann/phpunit#5967 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants