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

request: suppress MissingThrowsDocblock when preceding code invokes `TestCase::expectException`

Open SignpostMarv opened this issue 5 years ago • 4 comments

MissingThrowsDocblock<T> currently bubbles up when preceding code invokes TestCase::expectException(T::class)

I'm reasonably certain phpunit doesn't let InvalidArgumentException bubble up if I'm calling static::expectException(InvalidArgumentException::class)

SignpostMarv avatar Jan 12 '20 12:01 SignpostMarv

Do you have a test case for this?

weirdan avatar Jan 12 '20 23:01 weirdan

not a proper one at the moment (presently at the dayjob, will see if I can dig one out), but it's something like this:

first, configure psalm to require @throws, then:

<?php
class FooTest extends TestCase {
    public function testThing() : void {
        static::expectException(InvalidArgumentException::class);

        // do thing that throws InvalidArgumentException
    }
}

SignpostMarv avatar Jan 13 '20 09:01 SignpostMarv

Feature: TestCase @throws
  In order to have TestCases have relaxed @throws requirements
  As a Psalm user
  I need Psalm to typecheck my testcases

  Background:
    Given I have the following config
      """
      <?xml version="1.0"?>
      <psalm checkForThrowsDocblock="true">
        <projectFiles>
          <directory name="."/>
          <ignoreFiles> <directory name="../../vendor"/> </ignoreFiles>
        </projectFiles>
        <plugins>
          <pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
        </plugins>
      </psalm>
      """
    And I have the following code preamble
      """
      <?php
      namespace NS;
      use PHPUnit\Framework\TestCase;

      """

  Scenario: uncaught exception triggers error
    Given I have the following code
      """
      class MyTestCase extends TestCase
      {
        /** @return void */
        public function testSomething() {
          throw new \InvalidArgumentException('foo');
        }
      }
      """
    When I run Psalm
    Then I see these errors
      | Type            | Message                                                                                                     |
      | MissingThrowsDocblock | InvalidArgumentException is thrown but not caught - please either catch or add a @throws annotation |
    And I see no other errors

  Scenario: expected exception triggers no errors
    Given I have the following code
      """
      class MyTestCase extends TestCase
      {
        /** @return void */
        public function testSomething() {
          $this->expectException(\InvalidArgumentException::class);
          throw new \InvalidArgumentException('foo');
        }
      }
      """
    When I run Psalm
    Then I see no errors

  Scenario: expected exception called statically triggers no errors
    Given I have the following code
      """
      class MyTestCase extends TestCase
      {
        /** @return void */
        public function testSomething() {
          static::expectException(\InvalidArgumentException::class);
          throw new \InvalidArgumentException('foo');
        }
      }
      """
    When I run Psalm
    Then I see no errors

SignpostMarv avatar Jan 13 '20 11:01 SignpostMarv

possibly related: https://youtrack.jetbrains.com/issue/WI-39461

SignpostMarv avatar Jan 27 '20 15:01 SignpostMarv