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

Deprecate assertArraySubset() #3494

Closed
sebastianbergmann opened this Issue Jan 22, 2019 · 14 comments

Comments

Projects
None yet
10 participants
@sebastianbergmann
Copy link
Owner

sebastianbergmann commented Jan 22, 2019

The assertArraySubset() method is a constant source of confusion and frustration. For example, see #2069, #2108, #2568, #3101, #3234, #3240, or #3319.

I have come to the conclusion that this mess cannot be fixed without breaking backward compatibility. In addition to that, I myself have yet to see a use case for this or how this would be used then. I have also not seen this used in the wild.

This is why I decided to deprecate assertArraySubset() in PHPUnit 8 and to remove it in PHPUnit 9. Anybody who thinks this functionality is useful is more than welcome to take the code, put it into a separate project, and package it as an extension for PHPUnit.

@jflambert

This comment has been minimized.

Copy link

jflambert commented Jan 31, 2019

Ah, we use assertArraySubset quite a lot with the current phpunit 7.5.2. Thanks for the early heads up, but could you provide some recommended alternatives?

@sebastianbergmann

This comment has been minimized.

Copy link
Owner Author

sebastianbergmann commented Jan 31, 2019

For assertArraySubset() there is no suggested alternative.

@solofeed

This comment has been minimized.

Copy link

solofeed commented Feb 6, 2019

@sebastianbergmann maybe will be better to improve this method or refactor instead of remove?

I think this method really useful

if I can help somehow, tell me about it

@rdohms

This comment has been minimized.

Copy link
Contributor

rdohms commented Feb 15, 2019

I find it really useful to assert that an array has a set of keys/values instead of asserting one by one.

for example:

    public function fromExceptionWithDebugShouldAppendDebugInformation(): void
    {
        $dto       = ExceptionDto::fromExceptionWithDebug(new RuntimeException('Test', 123));
        $tracePart = [['function' => __FUNCTION__, 'class' => __CLASS__, 'type' => '->']];

        self::assertSame('Test', $dto->message);
        self::assertSame(RuntimeException::class, $dto->exception);
        self::assertSame(123, $dto->code);
        self::assertSame(__FILE__, $dto->file);
        self::assertSame(__LINE__ - 7, $dto->line);
        self::assertArraySubset($tracePart, $dto->trace);
        self::assertNull($dto->errorCode);
        self::assertNull($dto->violations);
    }
@borNfreee

This comment has been minimized.

Copy link

borNfreee commented Feb 15, 2019

We were using it a lot. Decided to copy this code to our codebase after upgrading to PHPUnit 8.0 since no alternative has been suggested.

@GrahamCampbell

This comment has been minimized.

Copy link
Contributor

GrahamCampbell commented Feb 16, 2019

What is this replaced by?

@GrahamCampbell

This comment has been minimized.

Copy link
Contributor

GrahamCampbell commented Feb 16, 2019

Laravel is using this too.

@rdohms

This comment has been minimized.

Copy link
Contributor

rdohms commented Feb 17, 2019

As suggested by @sebastianbergmann I have moved this to a separate package and added a thin layer for ease of use. If you are in need: https://packagist.org/packages/dms/phpunit-arraysubset-asserts

Will explore what we can do to make it clearer and more flexible as well, any input is welcome.

@clicars

This comment has been minimized.

Copy link

clicars commented Feb 21, 2019

We use that method in every single test!! we need it!!

We are going to use the @rdohms package, if not all our tests will be destroyed.

@derrabus

This comment has been minimized.

Copy link
Contributor

derrabus commented Feb 21, 2019

Well, PHPUnit 9 hasn't even been released yet and PHPUnit 8 will still be around for a while. Your tests are going to be fine.

@srosato

This comment has been minimized.

Copy link

srosato commented Feb 22, 2019

Removing it will break backwards compatibility, might as well take the hit and use that BC break to remove the confusion. I find this method really useful. It helps us clearly show intent on what the actual test scenario really cares about. Without that function I can see some developers reverting back to assertEquals() by copy pasting entire arrays of data that is not relevant to the test's scenario intention.

@rdohms

This comment has been minimized.

Copy link
Contributor

rdohms commented Feb 22, 2019

@derrabus depends on your config, mine for example break on deprecated warning.

@derrabus

This comment has been minimized.

Copy link
Contributor

derrabus commented Feb 22, 2019

@rdohms I understand. But as far as I can tell, deprecating functionality is a normal thing to do with a minor release according to SemVer. I understand that you don't want to ignore deprecations until it's too late, but your configuration sounds a bit too strict to me.

@kubawerlos

This comment has been minimized.

Copy link
Contributor

kubawerlos commented Feb 22, 2019

@srosato

Removing it will break backwards compatibility

Thus label backward-compatibility-break ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment