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
Properties
- $appConfig : App
- Controller configuration.
- $body : string|null
- Request body.
- $controller : Controller
- Initialized controller.
- $logger : LoggerInterface
- Message logger.
- $request : IncomingRequest
- Request.
- $response : ResponseInterface
- Response.
- $uri : string
- URI of this request.
Methods
- controller() : $this
- Loads the specified controller, and generates any needed dependencies.
- execute() : TestResponse
- Runs the specified method on the controller and returns the results.
- withBody() : $this
- Set the method's body, with method chaining.
- withConfig() : $this
- Set controller's config, with method chaining.
- withLogger() : $this
- Set controller's logger, with method chaining.
- withRequest() : $this
- Set controller's request, with method chaining.
- withResponse() : $this
- Set controller's response, with method chaining.
- withUri() : $this
- 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
ResponseInterface
$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) : $this
Parameters
- $name : string
Return values
$thisexecute()
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
TestResponsewithBody()
Set the method's body, with method chaining.
public
withBody(string|null $body) : $this
Parameters
- $body : string|null
Return values
$thiswithConfig()
Set controller's config, with method chaining.
public
withConfig(App $appConfig) : $this
Parameters
- $appConfig : App
Return values
$thiswithLogger()
Set controller's logger, with method chaining.
public
withLogger(LoggerInterface $logger) : $this
Parameters
- $logger : LoggerInterface
Return values
$thiswithRequest()
Set controller's request, with method chaining.
public
withRequest(IncomingRequest $request) : $this
Parameters
- $request : IncomingRequest
Return values
$thiswithResponse()
Set controller's response, with method chaining.
public
withResponse(ResponseInterface $response) : $this
Parameters
- $response : ResponseInterface
Return values
$thiswithUri()
Set the controller's URI, with method chaining.
public
withUri(string $uri) : $this
Parameters
- $uri : string
Return values
$thissetUpControllerTestTrait()
Initializes required components.
protected
setUpControllerTestTrait() : void