I am attempting to write a unit test by extending UnitTestCase. Below is how my code works, but I need a way to create a test (or mock) configuration that can be used to satisfy the testing.
How can I accomplish this?
To keep question short i am only adding pertinent code. Current issue is that my test cases are failing because the configuration is not valid.
Error message:
Drupal\Core\DependencyInjection\ContainerNotInitializedException: \Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container.
mymodule/src/plugin/mymodule_field/provider/MyClass.php
class MyClass extends ProviderPluginBase { /** * {@inheritdoc} */ public static function someFunction($input) { // Get the config. $config = \Drupal::config('mymodule.settings'); $test_value = $config->get('test'); if (!empty($test_value)) { /* process $test value */ return True; } elseif (empty($test_value)) { /* do something else */ return false; } } } mymodule/test/src/Unit/MyModuleValueTest.php
class MyModuleValueTest extends UnitTestCase { public function testIsEmpty($value, $expected) { /* I am stuck here on how to get the config */ $this->assertEquals($expected, MyClass::someFunction($value)); } }