Skip to content

[FR] Handle removed assertArraySubset() assertion #1

@jrfnl

Description

@jrfnl

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 );
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions