Remove DrupalComponentTestListenerTrait and replace with a PHPStan rule

Created on 9 November 2023, 6 months ago
Updated 4 May 2024, 14 days ago

Problem/Motivation

Discovered in ๐Ÿ“Œ Replace the test class protected $modules deprecation error with a phpstan-drupal rule Active .

\Drupal\Tests\Listeners\DrupalComponentTestListenerTrait is ensuring "that no component tests are extending a core test base class".

A noble cause, but by now we should use a PHPStan rule for this.

Steps to reproduce

Proposed resolution

  1. Write a phpstan rule to replace the listener, and appropriate tests
  2. Remove \Drupal\Tests\Listeners\DrupalComponentTestListenerTrait

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

๐Ÿ“Œ Task
Status

Fixed

Version

11.0 ๐Ÿ”ฅ

Component
PHPUnitย  โ†’

Last updated about 22 hours ago

Created by

๐Ÿ‡ณ๐Ÿ‡ฑNetherlands Spokje

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Issue created by @Spokje
  • Status changed to Postponed 6 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States mglaman WI, USA

    Should phpstan-drupal own this, or should we stick it in Drupal core and register it as a custom rule? It's a rule for Drupal core development, not contrib or user-land code. I wouldn't mind writing the rule. But it seems "wrong" to have a Drupal core development paradigm in phpstan-drupal.

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    My 0.02$ - do we need product/framework managers input here?

    Itโ€™s a positioning topic. This, and fwiw #2972100: Remove usage of uniqid โ†’ , aim at introducing phpstan checks on Drupal core, but we do not have any prior examples of something like this being done in Drupal itself. So quite naturally devs are opening issues against mglaman/phpstan-drupal.

    On a different approach, phpstan/phpstan-deprecation-rules, even if being actively used in core, was added as a dependency of phpstan-drupal, not of core.

    IMHO we need rules to guide where issues should be going to from now on.

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands Spokje

    But it seems "wrong" to have a Drupal core development paradigm in phpstan-drupal.

    Fair question, didn't realize this is indeed a core-only rule.

    Putting the rule in core itself and registering in core with phpstan seems reasonable, I "only" see issues with testing the rule, which would mean (AFAICT) core would need Yet Another test-job, now for phpstan, which tests core-specific rules.

    EDIT: I agree with (cross-post of) @mondrake that we need some guidance here. Tagging with Needs framework manager review as it's the closest one to what we need I could find.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States mglaman WI, USA

    , I "only" see issues with testing the rule, which would mean (AFAICT) core would need Yet Another test-job, now for phpstan, which tests core-specific rules.

    Why? It could be tested with a PHPUnit test

  • Assigned to mondrake
  • Status changed to Active 3 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    This is in the critical path to PHPUnit 10, since listeners will be going away.

    We are not going to get the rule from phpstan-drupal, so letโ€™s have one on our own.

    Will look into it the next days.

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    Update IS and title

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States mglaman WI, USA

    I wouldn't mind helping write the PHPStan rule. Did we get a decision on if it can go into Drupal core, or is it being delegated to the extension? The one downside of it not going into the extension is that contrib won't have the rule automatically (unless phpstan-drupal also configures it? It might get weird.)

  • Pipeline finished with Success
    3 months ago
    Total: 604s
    #106366
  • Issue was unassigned.
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    @mglaman if you can write the rule, I would suggest to do it here now. Then if core committers decide not to include in core, it would be anyway straightforward to move it to phpstan-drupal, I guess.

    The compelling argument to do it here, IMHO, is that 'components' is code that should not interact with 'core' code, and this check ensure that tests of components do not extend from core test classes but from PHPUnit TestCase. Should not be relevant for contrib/custom that would probably use composer if they need access to non-Drupal code libraries.

    #2972100: Remove usage of uniqid โ†’ is different I think, because it'd be debatable if restricting use of PHP functions should be for Drupal core only or also extending to contrib.

    One more thing - I would suggest not to use core namespaces from /core/lib/Drupal/Core for this, it's not code used by Drupal at runtime. I would rather go for something in /core/tests/Drupal or even better a new subdirectory /core/tests/PhpStan to prevent mixing PHPUnit related code with PHPStan one.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States mglaman WI, USA

    Sounds good to me! I'll try and take a stab at it this week. When I do I'll assign it to myself

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States mglaman WI, USA

    I added ComponentTestDoesNotExtendCoreTest as a rule to replace the test listener, along with a test for the rule.

  • Status changed to Needs review 2 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States mglaman WI, USA
  • Pipeline finished with Failed
    2 months ago
    Total: 342s
    #115464
  • Status changed to Needs work 2 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    Nice! Left comments inline. NW for that and for the test failures.

    BTW reviwing this I just found this test class Drupal\Tests\Component\DrupalComponentTest which may be something to convert to a PHPStan rule, too, in a follow-up.

  • Pipeline finished with Failed
    about 1 month ago
    Total: 366s
    #147384
  • Pipeline finished with Failed
    about 1 month ago
    Total: 492s
    #147391
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    Fixed the linting jobs.

  • Pipeline finished with Failed
    about 1 month ago
    Total: 990s
    #147398
  • Pipeline finished with Failed
    about 1 month ago
    Total: 1233s
    #148967
  • Status changed to Needs review about 1 month ago
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    This is reviewable now, I think.

    The test cannot run in CI until ๐Ÿ“Œ Downgrade (temporarily) nikic/php-parser to ^4 RTBC , but it runs locally when I downgrade the php parser to v4.
    In the test, we need to disable Symfony's DebugClassLoader at setup and re-enable at teardown, since PHPStan's internals invoked during the test itself do not dance well with it.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    How about... not putting PHPStan rule tests in core/tests/Drupal but put them in core/tests/PHPStan - and then in there as well as the test have a separate composer.json and phpunit.xml.dist file for running the tests. Then we add a new job to the pipeline in .gitlab/pipeline.yml to run these tests. That way PHPStans dependencies and Drupal's stay decoupled.

  • Assigned to mondrake
  • Status changed to Needs work 16 days ago
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    #21 sounds good - I will start moving stuff around but then adding the job pipeline is above me.

  • Pipeline finished with Failed
    16 days ago
    #162317
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    ๐Ÿ™‚ unintended, but the last change shows how the rule actually works...

    we do not want a PHPStan failure here though, so we need to exclude the fixture from PHPStan checking, and enable rule testing via PHPUnit

  • Pipeline finished with Success
    16 days ago
    Total: 533s
    #162333
  • Issue was unassigned.
  • Status changed to Needs review 16 days ago
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    I think this now is possibly committable:

    1) the test run is green, AND the first Drupal's own PHPStan rule works as proven by #22
    2) the test for the rule also works, tested locally, but we need a separate test job in the pipeline to setup PHPUnit to execute it decoupled from Drupal core tests - which will certainly take longer but can be deferred to a follow up

  • First commit to issue fork.
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom longwave UK

    How do I get this to work locally?

    $ composer require --dev nikic/php-parser:^4
    $ vendor/bin/phpunit -c core core/tests/PHPStan
    PHPUnit 9.6.19 by Sebastian Bergmann and contributors.
    
    Testing /var/www/html/drupal/core/tests/PHPStan
    E                                                                   1 / 1 (100%)
    
    Time: 00:01.504, Memory: 44.00 MB
    
    There was 1 error:
    
    1) Drupal\PHPStan\Tests\ComponentTestDoesNotExtendCoreTestTest::testRule
    RuntimeException: The autoloader expected class "Drupal\Tests\UnitTestCase" to be defined in file "/var/www/html/drupal/core/tests/Drupal/Tests/UnitTestCase.php". The file was found but the class was not in it, the class name or namespace probably has a typo.
    
  • Pipeline finished with Canceled
    16 days ago
    Total: 514s
    #162388
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    @longwave hang on ... I'm just making it work :)

    Short answer... do the same as the new pipline job...

        - cd core/tests/PHPStan
        - composer install
        - vendor/bin/phpunit tests --coverage-text --colors=never --coverage-cobertura=coverage.cobertura.xml --log-junit junit-reports/recipe-tests.xml
    
  • Pipeline finished with Success
    16 days ago
    Total: 509s
    #162394
  • Pipeline finished with Success
    16 days ago
    Total: 504s
    #162405
  • Pipeline finished with Canceled
    16 days ago
    Total: 354s
    #162412
  • Pipeline finished with Success
    16 days ago
    Total: 558s
    #162420
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States mglaman WI, USA

    Why does it need it's own composer.json? This seems really "extra". And ideally Drupal core should start providing it's own PHPStan rules for certain things. I don't see how this rule plus testing the rule introduced the need for a new subroot Composer file

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    @mglaman because testing phpstan rules requires it's dependencies. And it's and core's will not move together. We want to move to PHPUnit 10. PHPStan doesn't support the dependencies that resolves to for us - see ๐Ÿ“Œ Downgrade (temporarily) nikic/php-parser to ^4 RTBC

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States mglaman WI, USA

    Interesting that the test fails but scans don't for this, then. I guess because the APIs (test base classes) are public and working with non-prefixed code it exposes the problem.

    Thanks, I didn't easily see _why_ it was happening

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    And this is because PHPStan packages up it's own dependencies inside the phar file - which imo is a decent decision. But when you you their testing infra you hit a wall because then you have their real dependencies.

    I real don't think PHPStan should be deciding what our root dependencies are.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    I've put a README.md file in the directory. Atm it is just @todo - but I think we should explain ourselves in there and tell people how to run tests. I'll address this tomorrow as I don't have time right now. Maybe someone will beat me to it.

  • Status changed to Needs work 16 days ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom longwave UK

    Let's fill in the readme, at least just mentioning this directory is where we keep Drupal core's custom PHPStan rules. Other than that this looks good, no further comments.

  • Assigned to mondrake
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    Iโ€™ll work on #34

  • Issue was unassigned.
  • Status changed to Needs review 16 days ago
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    Fleshed out the README, and copied it to a draft CR https://www.drupal.org/node/3444866 โ†’ .

    I think a 'testing rules' section in the readme would be great, but left a @todo here, if @longwave or @alexpott can help

  • Pipeline finished with Success
    16 days ago
    Total: 630s
    #162562
  • Pipeline finished with Failed
    16 days ago
    Total: 161s
    #162649
  • Pipeline finished with Failed
    16 days ago
    Total: 173s
    #162653
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    Added the 'testing rules' section too. No more todos.

  • Pipeline finished with Success
    16 days ago
    #162657
  • Status changed to RTBC 16 days ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom longwave UK

    Looks great to me!

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia mstrelan

    Given that the custom phpstan rules won't be changing often, and they exist in isolation to Drupal, it seems weird that we would test them on every single ci run. It would make sense if there was a possibility that a change in Drupal could break the phpstan rule, but I don't think that's the case, is it?

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia mstrelan

    You can disregard that last comment, i see the changes key in gitlab ci now.

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    Parenting

    • catch โ†’ committed 58dd736c on 11.x
      Issue #3400366 by mondrake, alexpott, mglaman, longwave, Spokje: Remove...
  • Status changed to Fixed 14 days ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom catch

    This looks great. Alex Pott pointing out somewhere that if we ran all the component unit tests like we're running the phpstan test, we wouldn't need this phpstan rule at all - bigger change but would be a good follow-up.

    Committed/pushed to 11.x, thanks!

Production build 0.67.2 2024