Detect risky PHPUnit test

Open
#264 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
php
Domain
testing

Research direction

Start from the issue's try/catch example and review the repository's existing PHPUnit-related rules and tests. Define whether PHPStan should report tests that can finish successfully after the expected Throwable is caught without an explicit failure, then add coverage for the agreed behavior.

Written by the indexing model from the issue text.

Description

Feature request

In the following scenario:

function test_exception_state(): void {
  $myService = new MyService();

  try {
    $myService->doSomethingWrong();

    // missing: $this->fail();
  } catch (MyException $exception) {
    $this->assertSame('Custom message', $exception->getMessage());
    $this->assertSame('Another property', $exception->smthNotTypicalOfException);
  }
}

Essentially, a case where we want to do an explicit try/catch because expectExceptionObject() doesn't cut it (want to catch a Throwable instead of test some state of the exception besides message/code), it is easy to forget a fail() statement and leading to a risky test.

Maybe it leading as a risky is fine... But maybe it's a kinda of issue that PHPStan could detect & report. Just an idea.

Did PHPStan help you today? Did it make you happy in any way?

Today it did not help, but I didn't run it either so cannot blame it :) I like PHPStan otherwise!

Dominant language
PHP
Stars
544
Forks
58
Avg merge
2d 1h
Merged PRs (30d)
5

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from phpstan/phpstan-phpunit

All issues in phpstan/phpstan-phpunit

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.