psalm-plugin-phpunit icon indicating copy to clipboard operation
psalm-plugin-phpunit copied to clipboard

Improve PHPUnit assertions

Open ktomk opened this issue 5 years ago • 5 comments

Improve PHPUnit assertions

The two assertion methods:

  • assertArrayHasKey
  • assertArrayNotHasKey

benefit from a @psalm-assert as well.

ktomk avatar Aug 07 '20 10:08 ktomk

Kind of interesting with this change in a test-case:

public function testEnvServerSuperglobalFiltering()
{
    $server = $_SERVER;

    $server['foo=bar'] = 'baz';
    self::assertArrayHasKey('HOME', $server, 'pre-condition');
          ^ 
          `--- 133:15

psalm says:

ERROR: RedundantCondition - tests/unit/LibTest.php:133:15 - Found a redundant condition when evaluating $server and trying to reconcile type 'array{foo=bar: string(baz)}<array-key, mixed>' to array (see https://psalm.dev/122)
self::assertArrayHasKey('HOME', $server, 'pre-condition');

looks like that now being an assertion psalm over-emphasizes the fact that it's a known type now, feeling a bit like not seeing the whole picture, this came a bit unexpected to me.

ktomk avatar Aug 07 '20 11:08 ktomk

Just pushed another change that turns @psalm-assert array $array to @psalm-assert array{} $array which makes the evaluation redundant condition free.

ktomk avatar Aug 07 '20 12:08 ktomk

\Enhance\Assert::areIdentical($expected, $actual);

ghost avatar Aug 11 '20 08:08 ghost

\Enhance\Assert::throws($someClass, 'MethodName'); You can also pass arguments to be used in the target method call:

\Enhance\Assert::throws($someClass, 'MethodName', array(2, 'Arg2', 5));

ghost avatar Aug 11 '20 08:08 ghost

@Rahul-coder01 Care to elaborate? I must admit I have no clue what that code is referring to but might just be me not seeing the wood for the trees. Thanks.

ktomk avatar Aug 11 '20 17:08 ktomk