Add declare(strict_types=1) to all tests

Created on 3 October 2024, about 2 months ago

As per the core issue πŸ“Œ Add declare(strict_types=1) to all tests Needs work

πŸ“Œ Task
Status

Active

Version

2.1

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

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

Merge Requests

Comments & Activities

  • Issue created by @tr
  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    Test results show that using strict typing results in one additional test failure. But that's good - that's why we turn on strict typing, to find potential problems with the code:

    Drupal\Tests\views_aggregator\Kernel\Plugin\ViewsAggregatorStyleTableUnitTest::testViewsAggregatorTable TypeError: strpos(): Argument #1 ($haystack) must be of type string, Drupal\Core\Render\Markup given

  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    Oh, and another reason to change the test ... this is a really screwy assert, with a double negative.

    $this->assertFalse(strpos($output, 'views-field-name') !== FALSE)

  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    This is one of the three asserts that throws an error with strict typing turned on:

    $this->assertFalse(strpos($output, 'views-field-name') !== FALSE)

    What this does is search for the string 'views-field-name' (the needle) somewhere inside the $output text (the haystack).

    A return value from strpos() that is both boolean and FALSE means the needle was NOT found in the haystack. So a return value !== FALSE means that the needle IS found in the haystack.

    But we're using assertFalse(), which means that we're testing if it's FALSE that "the needle IS found in the haystack".

    So in other words, we expect the needle to not be found?

    A more appropriate assert is then:
    assertStringNotContainsString('views-field-name', (string) $output)

    This is not only more readable but also expresses better exactly what we're trying to test.

  • Pipeline finished with Skipped
    about 2 months ago
    #299635
    • tr β†’ committed 8c65ad99 on 2.1.x
      Issue #3478389 by tr: Add declare(strict_types=1) to all tests
      
  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    And that solved the problem described in #3. Merged.

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024