Phpunit: Remove assertArraySubset()

Created on 22 Jan 2019  路  8Comments  路  Source: sebastianbergmann/phpunit

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.

typbackward-compatibility

Most helpful comment

I just started really focusing on unit testing and I was actually looking for this functionality. For me, it makes sense and is useful.

In this test, I'm testing an array of arrays (friends). I always know one element will exist, however it's failing for the following reason.

Test failure

The response will look like this (with surrounding elements of course):

[
  ....
  [
    'account_id' => '36159956',
    'name' => 'athatcher7',
    'player_id' => '13023655',
    'ret_msg' => null,
  ]
  ...
]

I think the function could be useful...but after reading some of those issues...maybe a rewrite of the function would be better?

All 8 comments

I just started really focusing on unit testing and I was actually looking for this functionality. For me, it makes sense and is useful.

In this test, I'm testing an array of arrays (friends). I always know one element will exist, however it's failing for the following reason.

Test failure

The response will look like this (with surrounding elements of course):

[
  ....
  [
    'account_id' => '36159956',
    'name' => 'athatcher7',
    'player_id' => '13023655',
    'ret_msg' => null,
  ]
  ...
]

I think the function could be useful...but after reading some of those issues...maybe a rewrite of the function would be better?

Same as @MatthewSH. I use the assertion, I find it quite straightforward to understand actually. It will be missed!

Indeed this assertion has caused me the same headache as by looking at the name I could argue that something like [b, c] is a subset of [a, b, c]. However, the assertion takes into account the keys as well.
There can be a lot of confusion around it indeed. Still, maybe we could have some brainstorming with having it replaced instead with a couple of other methods which are clearer, and also allow a parameter to handle some of these cases just like we use the $strict parameter.

I use assertArraySubset extensively in my tests, mostly with associative arrays, but also with sorted indexed arrays. Its removal would cause me more than a few headaches.

Seeing issues like https://github.com/sebastianbergmann/phpunit/issues/2069 I agree.

Should keys match or just value? What if user needs that? How can we know?

@JKingweb Could you help me to cover your case in upgrade path? So you won't have to deal with that manually: https://github.com/rectorphp/rector/pull/1193

I can certainly try. Please feel free to contact me with details on what you'd need.

@JKingweb I've merged first dumb implementation.
I've added "Test it" section to the Pull-request, so you know how to test it: https://github.com/rectorphp/rector/pull/1193

Report any issues found to Rector issues. There will be many in the start, but we'll try to handle one by one :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stephen-leavitt-sonyatv-com picture stephen-leavitt-sonyatv-com  路  4Comments

sebastianbergmann picture sebastianbergmann  路  4Comments

sebastianbergmann picture sebastianbergmann  路  3Comments

keradus picture keradus  路  4Comments

AnmSaiful picture AnmSaiful  路  4Comments