CodeIgniter v4.5 API

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

Methods

controller()

Loads the specified controller, and generates any needed dependencies.

public controller(string $name) : $this
Parameters
$name : string
Return values
$this

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
throws
InvalidArgumentException
Return values
TestResponse

withBody()

Set the method's body, with method chaining.

public withBody(string|null $body) : $this
Parameters
$body : string|null
Return values
$this

withConfig()

Set controller's config, with method chaining.

public withConfig(App $appConfig) : $this
Parameters
$appConfig : App
Return values
$this

withLogger()

Set controller's logger, with method chaining.

public withLogger(LoggerInterface $logger) : $this
Parameters
$logger : LoggerInterface
Return values
$this

withUri()

Set the controller's URI, with method chaining.

public withUri(string $uri) : $this
Parameters
$uri : string
Return values
$this

setUpControllerTestTrait()

Initializes required components.

protected setUpControllerTestTrait() : void

        
On this page

Search results