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

Improve PHPUnit assertions #82

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

ktomk
Copy link
Contributor

@ktomk ktomk commented Aug 7, 2020

Improve PHPUnit assertions

The two assertion methods:

  • assertArrayHasKey
  • assertArrayNotHasKey

benefit from a @psalm-assert as well.

@ktomk
Copy link
Contributor Author

ktomk commented Aug 7, 2020

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
Copy link
Contributor Author

ktomk commented Aug 7, 2020

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

The two assertion methods:

- assertArrayHasKey
- assertArrayNotHasKey

benefit from a @psalm-assert as well.
Make PHPUnit assertion of an array-key of an actual array redundant
condition free on evaluation and reconciliation of an actual array to
the array type in the psalm-assertion.
@ghost
Copy link

ghost commented Aug 11, 2020

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

@ghost
Copy link

ghost commented Aug 11, 2020

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

@ktomk
Copy link
Contributor Author

ktomk commented Aug 11, 2020

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant