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

request: suppress MissingThrowsDocblock when preceding code invokes TestCase::expectException #48

Open
SignpostMarv opened this issue Jan 12, 2020 · 4 comments
Labels
more info needed There's no enough information provided to address the issue

Comments

@SignpostMarv
Copy link
Contributor

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)

@weirdan
Copy link
Member

weirdan commented Jan 12, 2020

Do you have a test case for this?

@weirdan weirdan added the more info needed There's no enough information provided to address the issue label Jan 12, 2020
@SignpostMarv
Copy link
Contributor Author

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

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
more info needed There's no enough information provided to address the issue
Projects
None yet
Development

No branches or pull requests

2 participants