Some unit tests don't call UnitTestCase::setUp()

Created on 5 April 2015, about 10 years ago
Updated 23 January 2023, over 2 years ago

Problem/Motivation

Not all unit tests with a custom setUp() method are currently calling the parent setUp() method, where the Drupal container is unset:

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    // Ensure that an instantiated container in the global state of \Drupal from
    // a previous test does not leak into this test.
    \Drupal::unsetContainer();

    $this->root = dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))));
  }

For now this is inoccuous (as the tests pass anyway), but as it is inconsistent it may lead to confusion down the line, when we put further checks/set up functionality in UnitTestCase::setUp(). Also, if new contributors look at an existing test for "inspiration", they may forget to call the parent setUp() method and have the global state of \Drupal from a previous test leak into their own test.

Proposed resolution

Call parent::setUp() in all unit tests with a custom setUp() method.

Or alternatively: do important checks elsewhere (DrupalStandardsListener?)

Remaining tasks

Review patch

User interface changes

None

API changes

None

Beta phase evaluation

<!--Uncomment the relevant rows for the issue. -->
πŸ“Œ Task
Status

Needs work

Version

10.1 ✨

Component
OtherΒ  β†’

Last updated about 18 hours ago

Created by

πŸ‡³πŸ‡±Netherlands stefan.r

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

Comments & Activities

Not all content is available!

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

Production build 0.71.5 2024