ApplicationTestCase
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
Simulates an HTTP GET request to a given URI by resolving and executing the corresponding controller and action, capturing the output.
Makes JSON Response for error payloads.
Sends a JSON response with headers and status code.
Inject JSON body by providing payload parameter. Also performs cleanup to avoid leaking into other tests.
Assert that a record exists in the specified database table with the given conditions.
Assert that no record exists in the specified database table with the given conditions.
Asserts that a property exists on the View object captured via controllerOutput().
Clean buffer to avoid risky test error.
Simulates a controller action based on URL-style input and captures its output.
Simulates a DELETE request to a specified URI. This sets the request method to DELETE and runs the matching controller action.
Set JsonResponse::$testing to true so we can test responses.
Simulate a session where a function we are testing expects a user to be authenticated.
Simulates a JSON request to a controller/action route (bypassing the Router) and returns a {TestResponse} containing the captured output and status.
Create a mock file for actions that require file input in form submissions.
Simulates a PATCH request to a specified URI. This sets the request method to PATCH and runs the matching controller action.
Simulates a POST request by setting $_POST data and executing the specified controller and action. Returns a TestResponse with the output and status.
Simulates a PUT request to a specified URI. This sets the request method to PUT and runs the matching controller action.
Simulates a form-style request to a controller/action route (bypassing the Router) and returns a {TestResponse} containing the captured output and status.
Resolves action based on controller name and action name found in segments array.
Resolves params found in segments array.
Simulates a JSON request through the full framework routing layer and returns a {TestResponse} containing the captured output and status.
Simulates a non-JSON request through the full framework routing layer and returns a {TestResponse} containing the captured output and status.
Simulates a request.
Implements setUp function from TestCase class.
Details
in
JsonResponse at line 47
bool
apiCsrfCheck()
Checks if CSRF token has been tampered with.
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)
in
JsonResponse at line 97
void
jsonError(string $message, int $status = 400, array $errors = [])
Makes JSON Response for error payloads.
in
JsonResponse at line 112
void
jsonResponse(mixed $data, int $status = 200, array $extraHeaders = [])
Sends a JSON response with headers and status code.
in
JsonResponse at line 161
void
preflight()
Respond to CORS preflight.
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.
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.
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.
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.
at line 131
static private void
cleanBuffer()
Clean buffer to avoid risky test error.
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.
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.
at line 187
protected void
enableJsonTestingMode()
Set JsonResponse::$testing to true so we can test responses.
at line 197
protected void
ensureSessionStarts()
Simulate a session where a function we are testing expects a user to be authenticated.
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()}.
at line 299
protected void
mockFile(string $files)
Create a mock file for actions that require file input in form submissions.
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.
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.
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.
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()}.
at line 399
static private array
resolveControllerAction(array $segments)
Resolves action based on controller name and action name found in segments array.
at line 413
static private array
resolveParams(array $segments)
Resolves params found in segments array.
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.
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.
at line 544
static private void
simulateRequest(string $method, string $pathInfo)
Simulates a request.
at line 555
protected void
setUp()
Implements setUp function from TestCase class.