Add addFactoryToContainer method#17
Add addFactoryToContainer method#17Naktibalda merged 2 commits intoCodeception:masterfrom olexp:add-factory
Conversation
| * | ||
| * @part services | ||
| */ | ||
| public function addFactoryToContainer(string $name, $factory): void |
There was a problem hiding this comment.
$factory could be documented as * @param string|callable $factory, right?
Could you add a short example and/or a link to some article explaining how using factory helps with testing?
There was a problem hiding this comment.
Correct. Docblock could be updated for $factory parameter. I didn't do that because other methods don't have params described either.
I can add test to https://github.com/Naktibalda/codeception-laminas-tests similar to https://github.com/Naktibalda/codeception-laminas-tests/blob/master/tests/functional/AddServiceToContainerCept.php.
General principle behind it is that whenever you use ServiceManager#build() to build a service, new service instance is returned. In order to have expected service stub built by the ServiceManager we can return it with help of substitution of factory for the service in tests.
There was a problem hiding this comment.
I didn't do that because other methods don't have params described either.
That's because @TavoNiievez went through all methods and replaced @param annotations with scalar typehints.
$factory can't have typehint, so @param provides useful information for IDEs and static analysis tools.
General principle behind it is that whenever you use ServiceManager#build() to build a service, new service instance is returned. In order to have expected service stub built by the ServiceManager we can return it with help of substitution of factory for the service in tests.
I'm not asking for myself, but for people who may want to use it. I tried searching yesterday, but failed to find anything relevant.
There was a problem hiding this comment.
Example:
$I = new FunctionalTester($scenario);
$foo = \Codeception\Stub::makeEmpty(stdClass::class, ['bar' => 1]);
$I->addFactoryToContainer('foo', fn() => $foo);
$I->amOnPage('/edit');
// EditController
//public function editAction()
//{
// // $service is a Stub created earlier
// $service = $this->serviceLocator()->build('foo', ['param1' => 'test']);
//}More info about shared/non-shared services:
https://docs.laminas.dev/laminas-servicemanager/configuring-the-service-manager/#shared
|
Released as 1.2.0 |
This allows to mock non-shared services by overriding their factories.