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.
Table of Contents
Properties
- $_appArgs : array<string|int, mixed>|null
- The customized application constructor arguments.
- $_appClass : string|null
- The customized application class name.
- $containerServices : array<string|int, mixed>
- The collection of container services.
Methods
- cleanupContainer() : void
- Clears any mocks that were defined and cleans up application class configuration.
- configApplication() : void
- Configure the application class to use in integration tests.
- mockService() : $this
- Add a mocked service to the container.
- modifyContainer() : ContainerInterface|null
- Wrap the application's container with one containing mocks.
- removeMockService() : $this
- Remove a mocked service to the container.
- createApp() : HttpApplicationInterface|ConsoleApplicationInterface
- Create an application instance.
Properties
$_appArgs
The customized application constructor arguments.
protected
array<string|int, mixed>|null
$_appArgs
$_appClass
The customized application class name.
protected
string|null
$_appClass
Tags
$containerServices
The collection of container services.
private
array<string|int, mixed>
$containerServices
= []
Methods
cleanupContainer()
Clears any mocks that were defined and cleans up application class configuration.
public
cleanupContainer() : void
Tags
configApplication()
Configure the application class to use in integration tests.
public
configApplication(string $class, array<string|int, mixed>|null $constructorArgs) : void
Parameters
- $class : string
-
The application class name.
- $constructorArgs : array<string|int, mixed>|null
-
The constructor arguments for your application class.
Tags
mockService()
Add a mocked service to the container.
public
mockService(string $class, Closure $factory) : $this
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
- $class : string
-
The class or interface you want to define.
- $factory : Closure
-
The factory function for mocked services.
Return values
$thismodifyContainer()
Wrap the application's container with one containing mocks.
public
modifyContainer(EventInterface $event, ContainerInterface $container) : ContainerInterface|null
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
- $event : EventInterface
-
The event
- $container : ContainerInterface
-
The container to wrap.
Return values
ContainerInterface|nullremoveMockService()
Remove a mocked service to the container.
public
removeMockService(string $class) : $this
Parameters
- $class : string
-
The class or interface you want to remove.
Return values
$thiscreateApp()
Create an application instance.
protected
createApp() : HttpApplicationInterface|ConsoleApplicationInterface
Uses the configuration set in configApplication()
.