0

I have a function which does 2-3 things, one of the activity is to save the configuration.

function processProps($data) {
  $config = $this->configFactory->getEditable('mymodule.settings');
  $config
    ->set('some_setting', 'some value')
    ->set('another_setting', 'another value')
    ->save();
  ...
  return TRUE;
}

The test code as follows

public function testSspProcessProperties() {
    $data = $this->getTestData();

    $immutable_config_object = $this->prophesize(ImmutableConfig::class);
    $immutable_config_object->isNew()->willReturn(FALSE);

    $config_object = $this->prophesize(Config::class);
    $config_object
      ->setData([
        'key' => 'value',
      ])
      ->shouldBeCalled();
    $config_object->save(FALSE)->shouldBeCalled();
    $config_object->get()->willReturn([]);

    $this->configFactory->loadMultiple(['mymodule.settings'])
      ->willReturn([]);
    $this->configFactory
      ->get('mymodule.settings')
      ->willReturn($immutable_config_object->reveal());

    $this->configFactory->getEditable('mymodule.settings')
      ->willReturn($config_object->reveal());

    $this->assertTrue($this->myclass->processProps($data));
  }

This test gives error Error: Call to a member function set() on null. When I debug found that $config object is loaded properly. So not able to understand why I say null :(

I checked ConfigEntityStorageTest for the reference.

2
  • One question - your test asserts that the method returns true, but the code provided looks like it will always return true regardless. I assume there is some redacted code there that has return false?
    – Kevin
    Jan 10, 2020 at 14:25
  • 1
    @Kevin It's probably the ..., but it would be clearer if the OP included the code in the post.
    – user72672
    Jan 10, 2020 at 14:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.