ControllerTestTrait
Controller Test Trait
Provides features that make testing controllers simple and fluent.
Example:
$this->withRequest($request) ->withResponse($response) ->withURI($uri) ->withBody($body) ->controller('App\Controllers\Home') ->execute('methodName');
Table of Contents
- $appConfig : App
- Controller configuration.
- $body : string|null
- Request body.
- $controller : Controller
- Initialized controller.
- $logger : LoggerInterface
- Message logger.
- $request : IncomingRequest
- Request.
- $response : Response
- Response.
- $uri : string
- URI of this request.
- controller() : mixed
- Loads the specified controller, and generates any needed dependencies.
- execute() : TestResponse
- Runs the specified method on the controller and returns the results.
- withBody() : mixed
- Set the method's body, with method chaining.
- withConfig() : mixed
- Set controller's config, with method chaining.
- withLogger() : mixed
- Set controller's logger, with method chaining.
- withRequest() : mixed
- Set controller's request, with method chaining.
- withResponse() : mixed
- Set controller's response, with method chaining.
- withUri() : mixed
- Set the controller's URI, with method chaining.
- setUpControllerTestTrait() : void
- Initializes required components.
Properties
$appConfig
Controller configuration.
protected
App
$appConfig
$body
Request body.
protected
string|null
$body
$controller
Initialized controller.
protected
Controller
$controller
$logger
Message logger.
protected
LoggerInterface
$logger
$request
Request.
protected
IncomingRequest
$request
$response
Response.
protected
Response
$response
$uri
URI of this request.
protected
string
$uri
= 'http://example.com'
Methods
controller()
Loads the specified controller, and generates any needed dependencies.
public
controller(string $name) : mixed
Parameters
- $name : string
Return values
mixed —execute()
Runs the specified method on the controller and returns the results.
public
execute(string $method, array<string|int, mixed> ...$params) : TestResponse
Parameters
- $method : string
- $params : array<string|int, mixed>
Tags
Return values
TestResponse —withBody()
Set the method's body, with method chaining.
public
withBody(string|null $body) : mixed
Parameters
- $body : string|null
Return values
mixed —withConfig()
Set controller's config, with method chaining.
public
withConfig(mixed $appConfig) : mixed
Parameters
- $appConfig : mixed
Return values
mixed —withLogger()
Set controller's logger, with method chaining.
public
withLogger(mixed $logger) : mixed
Parameters
- $logger : mixed
Return values
mixed —withRequest()
Set controller's request, with method chaining.
public
withRequest(mixed $request) : mixed
Parameters
- $request : mixed
Return values
mixed —withResponse()
Set controller's response, with method chaining.
public
withResponse(mixed $response) : mixed
Parameters
- $response : mixed
Return values
mixed —withUri()
Set the controller's URI, with method chaining.
public
withUri(string $uri) : mixed
Parameters
- $uri : string
Return values
mixed —setUpControllerTestTrait()
Initializes required components.
protected
setUpControllerTestTrait() : void