A lot of classes take the config factory service as a dependency simply because some particular config setting needs to be checked somewhere along the way. For unit testing these classes, it makes sense to use a stub config factory which will return the desired values for whatever configuration will be checked. This is already possible with PHPUnit's MockBuilder, but it'd be really handy to have a helper method in UnitTestCase that creates the stub based on a passed in array of desired return values, so that in your test you could just do:

$config_factory_stub = $this->getConfigFactoryStub(array('some.config.name' => array('some_key' => 'some_value')));

... and then pass that in as a parameter to the class that's actually being tested.
Here's an initial patch...

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

msonnabaum’s picture

Status: Needs review » Reviewed & tested by the community

Looks great.

This will significantly reduce the boilerplate necessary to mock config in unit tests, which will be common.

webchick’s picture

Status: Reviewed & tested by the community » Needs review

Hm. Is it possible to include an example use case? Preferably in a test, so we give this function some test coverage?

Answer: Possibly! Kat is going to look tonight. :)

katbailey’s picture

Title: Add a helper function to the PHPUnit UnitTestCase that returns a config factory stub » Convert PathProcessorTest to PHPUnit
FileSize
17.58 KB

OK, I'm re-purposing this. The attached patch converts the PathProcessorTest to PHPUnit, which entails some cleaning up of the language-based path processor, as well as the addition of a helper function for mocking config.

katbailey’s picture

Noticed a couple of things that needed tidying up. And this time git decided to do a rename for the test diff, which is nice. Don't know why it didn't do that the first time.

katbailey’s picture

msonnabaum pointed out a couple of small things in irc, this fixes them.

dawehner’s picture

We ran into the problem on a DUTB test: #1963080: Use Config MemoryStorage for unit tests

This just mocks the get() method, so I'm wondering whether a full implementation would be useful as well.

katbailey’s picture

This can certainly be added to as use cases crop up. It's not going to be of any help to DUTB tests though.

msonnabaum’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me.

katbailey’s picture

Oh derp - I just realised the language manager isn't even needed in this test. I originally wrote it in the context of the generator patch, which adds language manager as a constructor arg to the language-based path processor. So I will end up putting it back in that patch, but removing it for now.

katbailey’s picture

Rerolled after services got moved to yaml.

RobLoach’s picture

RTBC++

webchick’s picture

Category: feature » task
Status: Reviewed & tested by the community » Fixed

There's no reason for this to be held up by thresholds; just a straight conversion task.

Committed and pushed to 8.x. Thanks!

Status: Fixed » Closed (fixed)

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

jhedstrom’s picture