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
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
Comment #3
sluceroComment #4
sluceroComment #5
sluceroComment #7
slucero