Closed (fixed)
Project:
Drupal core
Version:
11.x-dev
Component:
phpunit
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
9 Nov 2023 at 08:49 UTC
Updated:
18 May 2024 at 08:24 UTC
Jump to comment: Most recent
Comments
Comment #2
spokjePostpone on https://github.com/mglaman/phpstan-drupal/issues/626
Comment #3
mglamanShould 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.
Comment #4
mondrakeMy 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.
Comment #5
spokjeFair 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 reviewas it's the closest one to what we need I could find.Comment #6
mglamanWhy? It could be tested with a PHPUnit test
Comment #7
mondrakeThis 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.
Comment #8
mondrakeComment #9
mondrakeComment #10
mondrakeUpdate IS and title
Comment #11
mondrakeComment #12
mglamanI 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.)
Comment #14
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/Corefor this, it's not code used by Drupal at runtime. I would rather go for something in/core/tests/Drupalor even better a new subdirectory/core/tests/PhpStanto prevent mixing PHPUnit related code with PHPStan one.Comment #15
mglamanSounds good to me! I'll try and take a stab at it this week. When I do I'll assign it to myself
Comment #16
mglamanI added ComponentTestDoesNotExtendCoreTest as a rule to replace the test listener, along with a test for the rule.
Comment #17
mglamanComment #18
mondrakeNice! Left comments inline. NW for that and for the test failures.
BTW reviwing this I just found this test class
Drupal\Tests\Component\DrupalComponentTestwhich may be something to convert to a PHPStan rule, too, in a follow-up.Comment #19
mondrakeFixed the linting jobs.
Comment #20
mondrakeThis is reviewable now, I think.
The test cannot run in CI until #3441353: Downgrade (temporarily) nikic/php-parser to ^4, 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.
Comment #21
alexpottHow 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.
Comment #22
mondrake#21 sounds good - I will start moving stuff around but then adding the job pipeline is above me.
Comment #23
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
Comment #24
mondrakeI 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
Comment #26
longwaveHow do I get this to work locally?
The class is defined correctly so I'm confused as to why this fails here.
Comment #27
alexpott@longwave hang on ... I'm just making it work :)
Short answer... do the same as the new pipline job...
Comment #28
mglamanWhy 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
Comment #29
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 #3441353: Downgrade (temporarily) nikic/php-parser to ^4
Comment #30
mglamanInteresting 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
Comment #31
alexpottAnd 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.
Comment #32
alexpottI'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.
Comment #33
longwaveLet'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.
Comment #34
mondrakeI’ll work on #34
Comment #35
mondrakeFleshed 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
Comment #36
mondrakeAdded the 'testing rules' section too. No more todos.
Comment #37
longwaveLooks great to me!
Comment #38
mondrakeComment #39
mstrelan commentedGiven 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?
Comment #40
mstrelan commentedYou can disregard that last comment, i see the changes key in gitlab ci now.
Comment #41
mondrakeParenting
Comment #43
catchThis 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!