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

[FR] Handle removed assertArraySubset() assertion #1

Closed
jrfnl opened this issue Oct 26, 2020 · 1 comment
Closed

[FR] Handle removed assertArraySubset() assertion #1

jrfnl opened this issue Oct 26, 2020 · 1 comment

Comments

@jrfnl
Copy link
Collaborator

jrfnl commented Oct 26, 2020

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 );
    }
}
@jrfnl
Copy link
Collaborator Author

jrfnl commented Jun 3, 2021

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.

@jrfnl jrfnl closed this as completed Jun 3, 2021
kilgore5 pushed a commit to kingandpartners/elastic-press that referenced this issue Jan 25, 2024
- fix `PHP Fatal error:  Declaration of ElasticPressTest::setUp() must be compatible with PHPUnit\Framework\TestCase::setUp(): void`
- fix deprecation of `assertArraySubset()` removed in phpunit... fixes suggested in Yoast/PHPUnit-Polyfills#1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant