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...
Comment | File | Size | Author |
---|---|---|---|
#10 | 1957302.path_processors_phpunit.10.patch | 13 KB | katbailey |
#9 | 1957302.path_processors_phpunit.9.patch | 13.28 KB | katbailey |
#9 | interdiff.txt | 906 bytes | katbailey |
#5 | 1957302.path_processors_phpunit.5.patch | 13.61 KB | katbailey |
#5 | interdiff.txt | 1.04 KB | katbailey |
Comments
Comment #1
msonnabaum CreditAttribution: msonnabaum commentedLooks great.
This will significantly reduce the boilerplate necessary to mock config in unit tests, which will be common.
Comment #2
webchickHm. 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. :)
Comment #3
katbailey CreditAttribution: katbailey commentedOK, 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.
Comment #4
katbailey CreditAttribution: katbailey commentedNoticed 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.
Comment #5
katbailey CreditAttribution: katbailey commentedmsonnabaum pointed out a couple of small things in irc, this fixes them.
Comment #6
dawehnerWe 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.
Comment #7
katbailey CreditAttribution: katbailey commentedThis can certainly be added to as use cases crop up. It's not going to be of any help to DUTB tests though.
Comment #8
msonnabaum CreditAttribution: msonnabaum commentedLooks good to me.
Comment #9
katbailey CreditAttribution: katbailey commentedOh 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.
Comment #10
katbailey CreditAttribution: katbailey commentedRerolled after services got moved to yaml.
Comment #11
RobLoachRTBC++
Comment #12
webchickThere's no reason for this to be held up by thresholds; just a straight conversion task.
Committed and pushed to 8.x. Thanks!
Comment #14
jhedstrom