Add declare(strict_types=1) to all Unit tests

Created on 5 November 2023, about 1 year ago
Updated 19 January 2024, 10 months ago

Problem/Motivation

This is a child issue of 📌 Add declare(strict_types=1) to all unit tests Active . After fixing strict type issues we can add the strict_types declaration to all Unit tests.

Steps to reproduce

Add to phpcs.xml.dist:

  <rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
    <properties>
      <property name="spacesCountAroundEqualsSign" value="0" />
    </properties>
    <include-pattern>*/tests/src/Unit/*</include-pattern>
    <include-pattern>./tests/Drupal/Tests/*/*.php</include-pattern>
    <exclude-pattern>./tests/Drupal/Tests/Listeners/*</exclude-pattern>
    <exclude-pattern>./tests/Drupal/Tests/*/Fixture/*</exclude-pattern>
    <exclude-pattern>./tests/Drupal/Tests/*/fixtures/*</exclude-pattern>
  </rule>

Run phpcs:

composer phpcs

Proposed resolution

Run phpcbf:

composer phpcbf

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

📌 Task
Status

Fixed

Version

10.2

Component
PHPUnit 

Last updated about 16 hours ago

Created by

🇦🇺Australia mstrelan

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

Merge Requests

Comments & Activities

  • Issue created by @mstrelan
  • Status changed to Postponed about 1 year ago
  • 🇦🇺Australia mstrelan

    Postponing on parent issue.

  • Merge request !5303Add declare(strict_types=1) to unit tests → (Closed) created by mstrelan
  • Status changed to Needs review about 1 year ago
  • Status changed to Needs work about 1 year ago
  • 🇺🇸United States smustgrave

    Seems to have test failures but not sure if this is also postponed on the trait issue landing with the rule.

  • Status changed to Postponed about 1 year ago
  • 🇺🇸United States dww

    In practice, probably blocked on the Traits one, but not formally. Either of these issues could land first to add the (restricted) phpcs rule, and then the other would expand the rule to cover more kinds of files.

    However, definitely blocked on #3303206: Define a standard for adding declare(strict_types=1) , since we need to decide the standard for how we want to add this declaration.

  • Status changed to Needs review 12 months ago
  • 🇦🇺Australia mstrelan

    Simplified the steps to not require rector. No longer postponed.

  • Status changed to Needs work 12 months ago
  • 🇺🇸United States smustgrave

    So when I ran locally I got 854 not 896 (minus one for phpcs.xml file). Is this mixing two tickets by chance?

  • Status changed to Postponed 12 months ago
  • 🇺🇸United States dww

    The process for the coding standards change still needs to run its course with a formal blog announcement and a 2 week period for community comments. Still postponed for now.

  • Status changed to RTBC 12 months ago
  • 🇺🇸United States xjm

    The coding standards issue is settled.

    There are comments above about test failures, but I don't see any. Setting RTBC and requeuing.

  • 🇺🇸United States xjm

    Similar merge conflict to the other:

    ++<<<<<<< HEAD
     +    <!-- @todo broaden this in https://www.drupal.org/project/drupal/issues/3400434 -->
     +    <!-- <include-pattern>*/tests/*</include-pattern> -->
     +    <include-pattern>*/tests/src/Unit/*</include-pattern>
     +    <include-pattern>./tests/Drupal/Tests/*/*.php</include-pattern>
     +    <exclude-pattern>./tests/Drupal/Tests/Listeners/*</exclude-pattern>
     +    <exclude-pattern>./tests/Drupal/Tests/*/Fixture/*</exclude-pattern>
     +    <exclude-pattern>./tests/Drupal/Tests/*/fixtures/*</exclude-pattern>
     +    <!-- @todo remove once https://www.drupal.org/project/drupal/issues/3401994 is in -->
     +    <exclude-pattern>./tests/Drupal/Tests/Traits/*</exclude-pattern>
     +    <exclude-pattern>./tests/Drupal/Tests/*Trait.php</exclude-pattern>
    ++=======
    +     <include-pattern>./tests/Drupal/BuildTests/*</include-pattern>
    ++>>>>>>> 11.x

    ...why I again just resolved by alphabetizing. It'll need to be updated again since I think the traits issue will likely go in first; I just want to re-run the check to make sure we're not missing any tests.

  • Status changed to Needs review 12 months ago
  • 🇺🇸United States xjm

    Better status while tests run.

  • Status changed to Needs work 12 months ago
  • 🇺🇸United States xjm

    Several more to fix:

    ----------------------------------------------------------------------
    Time: 12.05 secs; Memory: 6MB
    PHP CODE SNIFFER REPORT SUMMARY
    ----------------------------------------------------------------------
    FILE                                                  ERRORS  WARNINGS
    ----------------------------------------------------------------------
    .../modules/update/tests/src/Unit/UpdateMailTest.php  1       0
    .../Tests/Core/Session/AccessPolicyProcessorTest.php  1       0
    ...ts/Core/Session/CalculatedPermissionsItemTest.php  1       0
    .../Tests/Core/Session/CalculatedPermissionsTest.php  1       0
    ...re/Session/RefinableCalculatedPermissionsTest.php  1       0
    ----------------------------------------------------------------------
    A TOTAL OF 5 ERRORS AND 0 WARNINGS WERE FOUND IN 5 FILES
    ----------------------------------------------------------------------
    PHPCBF CAN FIX 5 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
    ----------------------------------------------------------------------
    

    Since this issue might be a bit like bailing water, I think it might be best to postpone until the shorter related issues land, but up to contributors.

  • Status changed to Needs review 12 months ago
  • Status changed to RTBC 11 months ago
  • 🇺🇸United States smustgrave

    Even though this one is easy to read still a very large patch with lost of changes. Think it would be best to land now and if any stragglers they could be handled in a final ticket for the meta?

  • Status changed to Needs work 11 months ago
  • 🇳🇿New Zealand quietone

    This needs to be rebased locally. :-(

  • Status changed to Needs review 11 months ago
  • 🇦🇺Australia mstrelan

    Merged in 11.x and re-ran phpcbf

  • Status changed to RTBC 11 months ago
  • 🇺🇸United States smustgrave

    Rebase seems goode

  • Status changed to Needs work 11 months ago
  • 🇬🇧United Kingdom longwave UK
    FILE: core/tests/Drupal/Tests/Core/EventSubscriber/FinishResponserSubscriberTest.php
    ----------------------------------------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    ----------------------------------------------------------------------------------------------------------------------------------
     1 | ERROR | [x] Missing declare(strict_types=1).
       |       |     (SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing)
    
  • 🇬🇧United Kingdom longwave UK

    🐛 Regression from #3295790 content-length header set earlier than expected Fixed will also rename that test so maybe worth holding off until that lands?

  • Status changed to RTBC 11 months ago
  • 🇦🇺Australia mstrelan

    Issue mentioned in #23 landed, back to rtbc

  • 🇦🇺Australia mstrelan

    Updated issue summary to better reflect exclude patterns, since other related issues are in. Merged in 11.x just to be sure.

  • 🇦🇺Australia larowlan 🇦🇺🏝.au GMT+10

    Issue credits

    • larowlan committed 9add7f71 on 11.x
      Issue #3399373 by mstrelan, xjm, longwave, quietone: Add declare(...
  • 🇦🇺Australia larowlan 🇦🇺🏝.au GMT+10

    Committed to 11.x as we're constantly bailing water here.

    Doesn't apply to 10.2

    deleted by us:   core/modules/file/tests/src/Unit/Upload/ContentDispositionFilenameParserTest.php
    deleted by us:   core/tests/Drupal/Tests/Core/Session/AccessPolicyProcessorTest.php
    deleted by us:   core/tests/Drupal/Tests/Core/Session/CalculatedPermissionsItemTest.php
    deleted by us:   core/tests/Drupal/Tests/Core/Session/CalculatedPermissionsTest.php
    deleted by us:   core/tests/Drupal/Tests/Core/Session/RefinableCalculatedPermissionsTest.php
    

    I think at this point we can probably not worry about 10.2

    Thanks for the monster effort here @mstrelan

  • Status changed to Fixed 11 months ago
  • Status changed to Downport 11 months ago
  • 🇬🇧United Kingdom longwave UK

    Discussed this with @catch yesterday and we thought this might be a good idea to backport (minus the change to phpcs.xml.diat) to make cherry picks easier; otherwise bug fixes with any unit test changes may need to be rerolled? As strict types only affects the file it is used in there are no side effects that I could think of.

  • 🇦🇺Australia mstrelan

    Happy to back port, but also since it's isolated to the top of each file it's fairly unlikely to cause a conflict.

  • Status changed to Needs review 11 months ago
  • 🇦🇺Australia mstrelan

    New MR against 10.2.x. I didn't try to apply the 11.x patch, just reapplied the phpcs rules and ran phpcbf.

  • Status changed to RTBC 11 months ago
  • 🇺🇸United States smustgrave

    10.2 reroll seems good

  • 🇬🇧United Kingdom longwave UK

    Thanks for backporting. Agree that this is not too likely to cause a conflict now I think about it more but also as this is tests-only I see no harm and if it prevents a reroll later so much the better.

    Committed without the change to phpcs.xml.dist as we can't change coding standards rules in a patch release.

    Committed b612db8 and pushed to 10.2.x. Thanks!

  • Status changed to Fixed 11 months ago
    • longwave committed b612db8a on 10.2.x
      Issue #3399373 by mstrelan, xjm, longwave, larowlan, quietone: Add...
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024