abstract class ApplicationTestCase extends TestCase

Abstract class for test cases.

Traits

A trait that support operations related to APIs and their associated JSON responses.

Properties

static string|null $rawInputOverride

Raw JSON override for tests

from  JsonResponse
static bool $testing

When true, do not send headers or exit; just echo JSON and return.

from  JsonResponse
static int $lastStatus

Captured status for tests (optional convenience).

from  JsonResponse
static array $lastHeaders

Captured headers for tests (optional convenience).

from  JsonResponse
static array $controllerOutput

The controller output.

Methods

bool
apiCsrfCheck()

Checks if CSRF token has been tampered with.

get(string $uri)

Simulates an HTTP GET request to a given URI by resolving and executing the corresponding controller and action, capturing the output.

void
jsonError(string $message, int $status = 400, array $errors = [])

Makes JSON Response for error payloads.

void
jsonResponse(mixed $data, int $status = 200, array $extraHeaders = [])

Sends a JSON response with headers and status code.

void
preflight()

Respond to CORS preflight.

static void
setRawInputOverride(mixed $payload = null)

Inject JSON body by providing payload parameter. Also performs cleanup to avoid leaking into other tests.

void
assertDatabaseHas(string $table, array $data, string $message = '')

Assert that a record exists in the specified database table with the given conditions.

void
assertDatabaseMissing(string $table, array $data, string $message = '')

Assert that no record exists in the specified database table with the given conditions.

void
assertViewContains(string $property, mixed $expectedValue = null)

Asserts that a property exists on the View object captured via controllerOutput().

static void
cleanBuffer()

Clean buffer to avoid risky test error.

string
controllerOutput(string $controllerSlug, string $actionSlug, array $urlSegments = [])

Simulates a controller action based on URL-style input and captures its output.

delete(string $uri, array $data = [])

Simulates a DELETE request to a specified URI. This sets the request method to DELETE and runs the matching controller action.

void
enableJsonTestingMode()

Set JsonResponse::$testing to true so we can test responses.

void
ensureSessionStarts()

Simulate a session where a function we are testing expects a user to be authenticated.

json(string $method, string $uri, array $data = [])

Simulates a JSON request to a controller/action route (bypassing the Router) and returns a {TestResponse} containing the captured output and status.

void
mockFile(string $files)

Create a mock file for actions that require file input in form submissions.

patch(string $uri, array $data = [])

Simulates a PATCH request to a specified URI. This sets the request method to PATCH and runs the matching controller action.

post(string $uri, array $data = [])

Simulates a POST request by setting $_POST data and executing the specified controller and action. Returns a TestResponse with the output and status.

put(string $uri, array $data = [])

Simulates a PUT request to a specified URI. This sets the request method to PUT and runs the matching controller action.

request(string $method, string $uri, array $data = [])

Simulates a form-style request to a controller/action route (bypassing the Router) and returns a {TestResponse} containing the captured output and status.

static array
resolveControllerAction(array $segments)

Resolves action based on controller name and action name found in segments array.

static array
resolveParams(array $segments)

Resolves params found in segments array.

routeJson(string $method, string $pathInfo, array $payload = [])

Simulates a JSON request through the full framework routing layer and returns a {TestResponse} containing the captured output and status.

routeRequest(string $method, string $pathInfo, array $data = [])

Simulates a non-JSON request through the full framework routing layer and returns a {TestResponse} containing the captured output and status.

static void
simulateRequest(string $method, string $pathInfo)

Simulates a request.

void
setUp()

Implements setUp function from TestCase class.

Details

in JsonResponse at line 47
bool apiCsrfCheck()

Checks if CSRF token has been tampered with.

Return Value

bool

True if token is valid, otherwise we return false.

at line 218
protected TestResponse get(string $uri)

Simulates an HTTP GET request to a given URI by resolving and executing the corresponding controller and action, capturing the output.

Supports URI segments in the form of /controller/action/param1/param2, and maps them to a controller class and action method with optional parameters passed positionally.

Example:

  • get('/') → HomeController::indexAction()
  • get('/products/show/3') → ProductsController::showAction(3)

Parameters

string $uri

The URI string, e.g., '/home/index' or '/products/show/3'

Return Value

TestResponse

The response object containing status and content

in JsonResponse at line 97
void jsonError(string $message, int $status = 400, array $errors = [])

Makes JSON Response for error payloads.

Parameters

string $message

The error message.

int $status

The status code.

array $errors

The array of errors.

Return Value

void

in JsonResponse at line 112
void jsonResponse(mixed $data, int $status = 200, array $extraHeaders = [])

Sends a JSON response with headers and status code.

Parameters

mixed $data

The JSON response.

int $status

The status code.

array $extraHeaders

Any extra headers.

Return Value

void

in JsonResponse at line 161
void preflight()

Respond to CORS preflight.

Return Value

void

in JsonResponse at line 177
static void setRawInputOverride(mixed $payload = null)

Inject JSON body by providing payload parameter. Also performs cleanup to avoid leaking into other tests.

Parameters

mixed $payload

The JSON payload or null.

Return Value

void

at line 40
void assertDatabaseHas(string $table, array $data, string $message = '')

Assert that a record exists in the specified database table with the given conditions.

This method builds a SQL WHERE clause from the provided key-value array and checks whether a matching row exists. It fails the test if no such row is found.

Parameters

string $table

The name of the database table to search.

array $data

An associative array of column => value pairs to match against.

string $message

Optional custom failure message.

Return Value

void

Exceptions

AssertionFailedError

at line 78
void assertDatabaseMissing(string $table, array $data, string $message = '')

Assert that no record exists in the specified database table with the given conditions.

This method builds a SQL WHERE clause from the provided key-value array and verifies that no matching row exists. It fails the test if such a row is found.

Parameters

string $table

The name of the database table to search.

array $data

An associative array of column => value pairs to match against.

string $message

Optional custom failure message.

Return Value

void

Exceptions

AssertionFailedError

at line 110
void assertViewContains(string $property, mixed $expectedValue = null)

Asserts that a property exists on the View object captured via controllerOutput().

Optionally asserts that the value matches.

Parameters

string $property

The view property to check.

mixed $expectedValue

Optional value to compare against.

Return Value

void

at line 131
static private void cleanBuffer()

Clean buffer to avoid risky test error.

Return Value

void

at line 145
protected string controllerOutput(string $controllerSlug, string $actionSlug, array $urlSegments = [])

Simulates a controller action based on URL-style input and captures its output.

Parameters

string $controllerSlug

e.g., 'home'

string $actionSlug

e.g., 'index'

array $urlSegments

Parameters to pass to the action

Return Value

string

Rendered HTML output

Exceptions

FrameworkException

at line 178
protected TestResponse delete(string $uri, array $data = [])

Simulates a DELETE request to a specified URI. This sets the request method to DELETE and runs the matching controller action.

Parameters

string $uri

The URI to simulate, e.g., '/model_name/destroy/10'

array $data

The DELETE data.

Return Value

TestResponse

The test response object

at line 187
protected void enableJsonTestingMode()

Set JsonResponse::$testing to true so we can test responses.

Return Value

void

at line 197
protected void ensureSessionStarts()

Simulate a session where a function we are testing expects a user to be authenticated.

Return Value

void

at line 259
protected TestResponse json(string $method, string $uri, array $data = [])

Simulates a JSON request to a controller/action route (bypassing the Router) and returns a {TestResponse} containing the captured output and status.

This helper is designed for testing API-style controller actions that use the {\Core\Lib\Http\JsonResponse} trait. It injects a raw JSON payload into {\Core\Lib\Http\JsonResponse::get()} via {\Core\Lib\Http\JsonResponse::$rawInputOverride}, enables test mode so JSON responses do not call exit, and sets the request method and JSON content type.

URI mapping behavior:

  • "/" segments are interpreted as "/{controller}/{action}/param1/param2"
  • Default controller is "home"
  • Default action is "index"

Note: This does not invoke the framework Router; it directly instantiates the controller and executes the action via {\Core\Lib\Testing\controllerOutput()}.

Parameters

string $method

HTTP method (e.g., "GET", "POST", "PUT", "PATCH", "DELETE").

string $uri

URI in the form "/controller/action/param1/param2". Leading/trailing slashes are optional.

array $data

JSON payload to provide to {\Core\Lib\Http\JsonResponse::get()}.

Return Value

TestResponse

A response wrapper containing the controller output and an HTTP-like status code.

at line 299
protected void mockFile(string $files)

Create a mock file for actions that require file input in form submissions.

Parameters

string $files

The name of the $_FILES field.

Return Value

void

at line 317
protected TestResponse patch(string $uri, array $data = [])

Simulates a PATCH request to a specified URI. This sets the request method to PATCH and runs the matching controller action.

Parameters

string $uri

The URI to simulate, e.g., '/model_name/patch/10'

array $data

The PATCH data.

Return Value

TestResponse

The test response object

at line 329
protected TestResponse post(string $uri, array $data = [])

Simulates a POST request by setting $_POST data and executing the specified controller and action. Returns a TestResponse with the output and status.

Parameters

string $uri

The URI to simulate, e.g., '/auth/register'

array $data

The POST data to inject (e.g., ['email' => 'foo@bar.com'])

Return Value

TestResponse

The test response object

at line 341
protected TestResponse put(string $uri, array $data = [])

Simulates a PUT request to a specified URI. This sets the request method to PUT and runs the matching controller action.

Parameters

string $uri

The URI to simulate, e.g., '/model_name/update/10'

array $data

The PUT data.

Return Value

TestResponse

The test response object

at line 366
protected TestResponse request(string $method, string $uri, array $data = [])

Simulates a form-style request to a controller/action route (bypassing the Router) and returns a {TestResponse} containing the captured output and status.

This helper populates the superglobals {$_POST} and {$_REQUEST} with the provided data, sets {$_SERVER['REQUEST_METHOD']} to the requested method, and executes the targeted controller action via {\Core\Lib\Testing\controllerOutput()}.

URI mapping behavior:

  • "/" segments are interpreted as "/{controller}/{action}/param1/param2"
  • Default controller is "home"
  • Default action is "index"

Note: This does not invoke the framework Router; it directly instantiates the controller and executes the action via {\Core\Lib\Testing\controllerOutput()}.

Parameters

string $method

HTTP method (e.g., "GET", "POST", "PUT", "PATCH", "DELETE").

string $uri

URI in the form "/controller/action/param1/param2". Leading/trailing slashes are optional.

array $data

Request payload to inject into {$_POST} and {$_REQUEST}.

Return Value

TestResponse

A response wrapper containing the controller output and an HTTP-like status code.

at line 399
static private array resolveControllerAction(array $segments)

Resolves action based on controller name and action name found in segments array.

Parameters

array $segments

Contains segments for controller name, action name, and params.

Return Value

array

An array containing strings for controller name and action name.

at line 413
static private array resolveParams(array $segments)

Resolves params found in segments array.

Parameters

array $segments

Contains segments for controller name, action name, and params.

Return Value

array

An array of parameters.

at line 439
protected TestResponse routeJson(string $method, string $pathInfo, array $payload = [])

Simulates a JSON request through the full framework routing layer and returns a {TestResponse} containing the captured output and status.

This helper is intended for end-to-end API tests where you want to exercise the Router's URL parsing and controller dispatch (i.e., closer to a real HTTP request).

It enables test-mode behavior in {\Core\Lib\Http\JsonResponse} (preventing exit), injects a raw JSON payload via {\Core\Lib\Http\JsonResponse::$rawInputOverride}, sets {$_SERVER['PATH_INFO']} and {$_SERVER['REQUEST_URI']} to the provided path, and executes {\Core\Router::route()} while capturing output buffering.

Status code behavior:

  • If your JsonResponse implementation records the last status (e.g., in JsonResponse::$lastStatus), that value is used.
  • Otherwise, defaults to 200.

Parameters

string $method

HTTP method (e.g., "GET", "POST", "PUT", "PATCH", "DELETE").

string $pathInfo

The routed path (e.g., "/favorites/show", "/favorites/destroy/10").

array $payload

JSON payload to inject into {\Core\Lib\Http\JsonResponse::get()}.

Return Value

TestResponse

A response wrapper containing the routed output and an HTTP-like status code.

at line 494
protected TestResponse routeRequest(string $method, string $pathInfo, array $data = [])

Simulates a non-JSON request through the full framework routing layer and returns a {TestResponse} containing the captured output and status.

This helper is intended for end-to-end tests where you want to exercise the Router's URL parsing and dispatch for traditional controller/view behavior (or form submissions).

It temporarily mutates global request state:

  • {$_SERVER['REQUEST_METHOD']}
  • {$_SERVER['PATH_INFO']} (preferred by the Router)
  • {$_SERVER['REQUEST_URI']} (fallback behavior)
  • {$_GET}/{@see $_POST}/{$_REQUEST} depending on the HTTP method

The Router is then executed via {\Core\Router::route()} while output buffering is captured. All globals are restored in a finally block to reduce cross-test contamination.

Note: This helper returns status 200 on successful router execution. If a {\Throwable} is thrown, the helper returns status 500 with the exception message as content. Many "not found" or ACL flows may redirect rather than throw, depending on your Router design.

Parameters

string $method

HTTP method (e.g., "GET", "POST", "PUT", "PATCH", "DELETE").

string $pathInfo

The routed path (e.g., "/profile/index", "/admindashboard/edit/5").

array $data

Query parameters (GET) or form payload (non-GET) to inject into superglobals.

Return Value

TestResponse

A response wrapper containing the routed output and an HTTP-like status code.

Exceptions

FrameworkException

at line 544
static private void simulateRequest(string $method, string $pathInfo)

Simulates a request.

Parameters

string $method

The request method.

string $pathInfo

The path for the request.

Return Value

void

at line 555
protected void setUp()

Implements setUp function from TestCase class.

Return Value

void