Trait ContainerStubTrait
A set of methods used for defining container services in test cases.
This trait leverages the Application.buildContainer
event
to inject the mocked services into the container that the
application uses.
Property Summary
-
$_appArgs protected
array|null
The customized application constructor arguments.
-
$_appClass protected
string|null
The customized application class name.
Method Summary
-
cleanupContainer() public
Clears any mocks that were defined and cleans up application class configuration.
-
configApplication() public
Configure the application class to use in integration tests.
-
createApp() protected
Create an application instance.
-
mockService() public
Add a mocked service to the container.
-
modifyContainer() public
Wrap the application's container with one containing mocks.
-
removeMockService() public
Remove a mocked service to the container.
Method Detail
cleanupContainer() ¶ public
cleanupContainer(): void
Clears any mocks that were defined and cleans up application class configuration.
Returns
void
configApplication() ¶ public
configApplication(string $class, array|null $constructorArgs): void
Configure the application class to use in integration tests.
Parameters
-
string
$class The application class name.
-
array|null
$constructorArgs The constructor arguments for your application class.
Returns
void
createApp() ¶ protected
createApp(): Cake\Core\HttpApplicationInterfaceCake\Core\ConsoleApplicationInterface
Create an application instance.
Uses the configuration set in configApplication()
.
Returns
Cake\Core\HttpApplicationInterfaceCake\Core\ConsoleApplicationInterface
mockService() ¶ public
mockService(string $class, Closure $factory): $this
Add a mocked service to the container.
When the container is created the provided classname will be mapped to the factory function. The factory function will be used to create mocked services.
Parameters
-
string
$class The class or interface you want to define.
-
Closure
$factory The factory function for mocked services.
Returns
$this
modifyContainer() ¶ public
modifyContainer(Cake\Event\EventInterface $event, Cake\Core\ContainerInterface $container): Cake\Core\ContainerInterface|null
Wrap the application's container with one containing mocks.
If any mocked services are defined, the application's container will be replaced with one containing mocks. The original container will be set as a delegate to the mock container.
Parameters
-
Cake\Event\EventInterface
$event The event
-
Cake\Core\ContainerInterface
$container The container to wrap.
Returns
Cake\Core\ContainerInterface|null
removeMockService() ¶ public
removeMockService(string $class): $this
Remove a mocked service to the container.
Parameters
-
string
$class The class or interface you want to remove.
Returns
$this