phpstan-phpunit icon indicating copy to clipboard operation
phpstan-phpunit copied to clipboard

AssertArrayHasKey seems to be not supported

Open VincentLanglet opened this issue 3 years ago • 6 comments

Hi @ondrejmirtes, I just got an issue with my tests saying that an offset does not exist in an array. I tried to add a assertArrayHasKey check, without success.

I think this assert is not handle by this plugin, am I wrong ?

I'll try to do a PR in order to add the support, I think I'll be able to copy the way isArray works. https://github.com/phpstan/phpstan-phpunit/blob/master/src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php#L158

VincentLanglet avatar Jun 30 '21 16:06 VincentLanglet

Yes, this should be easy enough.

ondrejmirtes avatar Jul 01 '21 07:07 ondrejmirtes

@ondrejmirtes I'm not sure it the implementation in commit 6aaff1196c4f808769774b49a94a60e5fdf18de7 is complete. assertArrayHasKey accepts an ArrayObject as well and PHPUnit will use offsetExists if an object is provided, but in the commit PHPStan will only allow arrays (as it uses array_key_exists).

micheh avatar Jul 15 '21 11:07 micheh

@micheh Do you have a false-positive that stems from the current implementation?

ondrejmirtes avatar Jul 15 '21 11:07 ondrejmirtes

I think at least that it stems from the current implementation.

Unit Test:

public function testArrayHasKey(): void
{
    $object = new ArrayObject(['test' => 'hello']);
    self::assertArrayHasKey('test', $object);

    $object2 = new ArrayObject(['test' => 'hello']);
    if ($object2->offsetExists('test') === false) {
        echo 'hello';
    }
}

In version 0.12.21 PHPStan now reports the error for the PHPUnit assertion ($object), but no error for the conditional ($object2): Call to static method PHPUnit\Framework\Assert::assertArrayHasKey() with 'test' and ArrayObject<string, string> will always evaluate to false.

micheh avatar Jul 15 '21 11:07 micheh

According to the phpdoc https://github.com/sebastianbergmann/phpunit/blob/adeede29ca716930efb49ebe0fe1a5cab38af9c4/src/Framework/Assert.php#L95 ArrayAccess has to be supported too indeed.

Can you provide a fix @micheh ?

VincentLanglet avatar Jul 15 '21 11:07 VincentLanglet

To support ArrayAccess properly I think this issue has to be fixed first: https://github.com/phpstan/phpstan/issues/3323

villfa avatar May 02 '23 13:05 villfa