Problem/Motivation

Throughout the module there are a variety of static method calls using either \Drupal::* instead of dependency injection. This makes unit testing those classes troublesome and they could benefit from the relatively low effort cleanup to replace them.

Another example of static method calls making testing difficult is the use of Pattern::create() throughout the module. This is a shortcut to the entity type manager which loads the storage handler, but in the use cases we control within the module, injecting the entity type manager and storage handler instead will make unit testing much easier.

Before

$pattern = Pattern::create($pattern_config);

After

// Inject this into the service instead.
$entityTypeManager = \Drupal::service('entity_type.manager');
$patternStorage = $entityTypeManager->getStorage('patternkit_pattern');
$pattern = $patternStorage->create($pattern_config);

This approach allows the entity type manager and pattern storage to be mocked and injected into classes for testing instead of having to mock them and load them into the container for indirect access.

Proposed resolution

Replace static method calls and usage of Pattern::create() throughout the module to use DI.

User interface changes

None.

API changes

None.

Data model changes

None.

Issue fork patternkit-3300299

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

slucero created an issue. See original summary.

slucero’s picture

Status: Active » Needs review
slucero’s picture

Assigned: slucero » Unassigned
slucero’s picture

Status: Needs review » Reviewed & tested by the community

  • slucero committed 741b63d on 9.1.x
    Issue #3300299 by slucero: Replace Static Method Calls with Dependency...
slucero’s picture

Status: Reviewed & tested by the community » Fixed
Related issues: +#3300226: \Drupal calls should be avoided in classes using dependency injection.

Status: Fixed » Closed (fixed)

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