[FR] Handle removed assertArraySubset() assertion
jrfnl opened this issue · 1 comments
The Assert::assertArraySubset()
assertion was hard deprecated in PHPUnit 8.0.0 and removed in PHPUnit 9.0.0.
Ref: sebastianbergmann/phpunit#3494
It could be useful to have this assertion still available as there is no one-on-one alternative for it within PHPUnit itself.
@rdohms has created a polyfill for this functionality. Unfortunately the version constraints of that repo do not allow for it to be included with this library at this time.
Widening the version constraints for that repo and using a custom autoloader, similar to the one used in this repo is being discussed in rdohms/phpunit-arraysubset-asserts#11
It would be great if that package could be included. If not, polyfilling the functionality within this repo should be considered.
If this functionality is not added to this repo, tests currently using assertArraySubset()
could be refactored using one of the below patterns:
class FooTest extends TestCase {
public function testArrayWithKeyOriginalLoose() {
$subset = [
'key1' => 'value',
'key2' => 'value'
];
$strict = false;
$array = ClassUnderTest::getArray();
$this->assertArraySubset( $subset, $array, $strict );
}
public function testArrayWithKeyRefactoredLoose() {
$subset = [
'key1' => 'value',
'key2' => 'value'
];
$array = ClassUnderTest::getArray();
foreach ( $subset as $key => $value ) {
$this->assertArrayHasKey( $key, $array );
$this->assertEquals( $value, $array[ $key ] );
}
}
public function testArrayWithKeyOriginalStrict() {
$subset = [
'key1' => 'value',
'key2' => 'value'
];
$strict = true;
$array = ClassUnderTest::getArray();
$this->assertArraySubset( $subset, $array, $strict );
}
public function testArrayWithKeyRefactoredStrict() {
$subset = [
'key1' => 'value',
'key2' => 'value'
];
$array = ClassUnderTest::getArray();
foreach ( $subset as $key => $value ) {
$this->assertArrayHasKey( $key, $array );
$this->assertSame( $value, $array[ $key ] );
}
}
public function testArrayWithoutKeyOriginalStrict() {
$subset = [ 'value1', 'value3', 'value10' ];
$strict = true;
$array = ClassUnderTest::getArray();
$this->assertArraySubset( $subset, $array, $strict );
}
public function testArrayWithoutKeyRefactoredStrict() {
$subset = [ 'value1', 'value3', 'value10' ];
$array = ClassUnderTest::getArray();
$this->assertTrue( array_intersect( $subset, $array ) === $subset );
}
}
As of version 0.3.0, the dms/phpunit-arraysubset-asserts
package allows for installation in combination with PHP 5.4 - current and PHPUnit 4.8.36/5.7.21 - current, which effectively solves this issue.
The dms/phpunit-arraysubset-asserts
package is now listed in the README of this repo as an alternative for the removed functionality. See #16.