Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 291521

[policy, no patch] Stop using $this->container in web tests

$
0
0

At the moment in Simpletest in order to access services and the like we do the following:

  $config = $this->container->get('config.factory')->get('whatever');
  $config->set('key', $value)->save();

This has several downsides:

  • $config is not a typed so you have no idea what interfaces it implements and IDEs can't help
  • We are not taking advantage of being able to manipulate the container in tests and in we do we still have update the container in \Drupal just incase
  • $this->container is not always maintained leading to issues (@berdir to provide nid)
  • it's unnecessarily verbose

I propose the we either

1. use \Drupal class

  $config = \Drupal::config('config.factory')->get('whatever');
  $config->set('key', $value)->save();

2. implement helper methods.

  $config = $this->configFactory()->get('whatever');
  $config->set('key', $value)->save();

3. Use the methods automatically created on the container

  $config = $this->container->getConfig_FactoryService()->get('whatever');
  $config->set('key', $value)->save();

I prefer the option 1 because we do a good job of keeping the container that's injected into \Drupal up to date - and it'll be used all over any remaining procedural code.


Viewing all articles
Browse latest Browse all 291521

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>