pfSense-pkg-RESTAPI

TestCase
in package
uses BaseTraits

Defines a TestCase object that can be used to define child TestCase classes that test internal functionality of the API. This is needed because pfSense does not have PHPUnit readily available.

Table of Contents

Properties

$env  : array<string|int, mixed>
$method  : string
$method_docstring  : string
$required_packages  : array<string|int, mixed>

Methods

get_class_fqn()  : string
Obtains the fully qualified name of the called class.
get_class_shortname()  : string
Obtains the shortname of the called class.
get_classes_in_namespace()  : array<string|int, mixed>
Obtains all classes associated with this class's current namespace.
install_packages()  : void
Installs the required packages for this TestCase.
log_error()  : void
Logs an error to the syslog.
run()  : void
Runs this TestCase when the object is created. Any method that starts with `test` will automatically be run.
run_command()  : array<string|int, mixed>
Runs a shell command and returns its output and return code.
setup()  : void
Sets up the test case before tests are run. This can be overridden by your TestCase to setup shared resources required for your tests.
teardown()  : void
Tears down the test case after tests are run. This can be overridden by your TestCase to destroy resources that may have been created during tests.
assert_does_not_throw()  : mixed
Checks if a given callable throws any exception.
assert_equals()  : mixed
Checks if two values are equal.
assert_is_empty()  : mixed
Checks if a given value is empty.
assert_is_false()  : mixed
Checks if a given value is a `false` boolean.
assert_is_greater_than()  : mixed
Checks if a given $value1 is greater than $value2.
assert_is_greater_than_or_equal()  : void
Checks if a given $value1 is greater than or equal to $value2.
assert_is_less_than()  : void
Checks if a given $value1 is less than $value2.
assert_is_less_than_or_equal()  : void
Checks if a given $value1 is less than or equal to $value2.
assert_is_not_empty()  : mixed
Checks if a given value is not empty.
assert_is_true()  : mixed
Checks if a given value is a `true` boolean.
assert_not_equals()  : mixed
Checks if two values are not equal.
assert_str_contains()  : mixed
Checks if a string contains another string.
assert_str_does_not_contain()  : mixed
Checks if a string does not contains another string.
assert_throws()  : mixed
Checks if an embedded callable raises a specific error
assert_throws_response()  : mixed
Checks if an embedded callable raises a specific RESTAPI\Core\Response exception and ensures that the expected 'return' code is given
assert_type()  : mixed
Checks if a given value is of a given type.
get_envs()  : void
Obtains the environment variable with a given name.

Properties

$env

public array<string|int, mixed> $env = []

An array of known environment variables that are available to this TestCase.

$method

public string $method = 'unknown'

Placeholder for the method that is currently being tested.

$method_docstring

public string $method_docstring = ''

Placeholder for the docstring for the method that is currently being tested.

$required_packages

public array<string|int, mixed> $required_packages = []

An array of packages that this TestCase requires to run. These packages will be installed before running the tests.

Methods

get_class_fqn()

Obtains the fully qualified name of the called class.

public get_class_fqn() : string
Return values
string

The FQN for this object's class.

get_class_shortname()

Obtains the shortname of the called class.

public get_class_shortname() : string
Return values
string

The shortname for this object's class.

get_classes_in_namespace()

Obtains all classes associated with this class's current namespace.

public get_classes_in_namespace([bool $shortnames = false ]) : array<string|int, mixed>
Parameters
$shortnames : bool = false
Return values
array<string|int, mixed>

An array of classes currently in this object's namespace

install_packages()

Installs the required packages for this TestCase.

public install_packages() : void

log_error()

Logs an error to the syslog.

public static log_error(string $message) : void
Parameters
$message : string

The error message to write to the syslog

run()

Runs this TestCase when the object is created. Any method that starts with `test` will automatically be run.

public run() : void
Tags
throws
ReflectionException

When ReflectionMethod could not find a test method.

throws
Error|Exception

The exception thrown by failed Tests, typically an AssertionError.

run_command()

Runs a shell command and returns its output and return code.

public run_command(string $command[, bool $trim_whitespace = false ]) : array<string|int, mixed>
Parameters
$command : string

The command to execute.

$trim_whitespace : bool = false

Remove excess whitespace from the command output. This is sometimes helpful when the output of commands that do not have consistent whitespace formatting.

Return values
array<string|int, mixed>

An array where the output key contains the commands output and the result_code key contains the resulting result code of the command.

setup()

Sets up the test case before tests are run. This can be overridden by your TestCase to setup shared resources required for your tests.

public setup() : void

teardown()

Tears down the test case after tests are run. This can be overridden by your TestCase to destroy resources that may have been created during tests.

public teardown() : void

assert_does_not_throw()

Checks if a given callable throws any exception.

protected assert_does_not_throw(mixed $callable) : mixed
Parameters
$callable : mixed

A callable to run and check for thrown exception.

Tags
throws
AssertionError

If the $callable throws any exception.

assert_equals()

Checks if two values are equal.

protected assert_equals(mixed $value1, mixed $value2[, string $message = '' ]) : mixed
Parameters
$value1 : mixed

The first value to evaluate.

$value2 : mixed

The second value to evaluate.

$message : string = ''

Set the error message embedded in any AssertionError thrown by this method.

Tags
throws
AssertionError

When $value1 and $value2 are not equal.

assert_is_empty()

Checks if a given value is empty.

protected assert_is_empty(mixed $value[, string $message = 'Expected value to be empty.' ]) : mixed
Parameters
$value : mixed

The value to evaluate.

$message : string = 'Expected value to be empty.'

Set the error message embedded in any AssertionError thrown by this method.

Tags
throws
AssertionError

When the value is not empty.

assert_is_false()

Checks if a given value is a `false` boolean.

protected assert_is_false(mixed $value[, string $message = "Expected value to equal 'false'." ]) : mixed
Parameters
$value : mixed

The value to evaluate.

$message : string = "Expected value to equal 'false'."

Set the error message embedded in any AssertionError thrown by this method.

Tags
throws
AssertionError

When the value is not false.

assert_is_greater_than()

Checks if a given $value1 is greater than $value2.

protected assert_is_greater_than(mixed $value1, mixed $value2) : mixed
Parameters
$value1 : mixed

The first value to evaluate. This value must be greater than value 2 to pass check.

$value2 : mixed

The second value to evaluate. This value must be less than value 1 to pass check.

assert_is_greater_than_or_equal()

Checks if a given $value1 is greater than or equal to $value2.

protected assert_is_greater_than_or_equal(mixed $value1, mixed $value2) : void
Parameters
$value1 : mixed

The first value to evaluate. This value must be greater than or equal to value 2 to pass check.

$value2 : mixed

The second value to evaluate. This value must be less than or equal to value 1 to pass check.

assert_is_less_than()

Checks if a given $value1 is less than $value2.

protected assert_is_less_than(mixed $value1, mixed $value2) : void
Parameters
$value1 : mixed

The first value to evaluate. This value must be less than value 2 to pass check.

$value2 : mixed

The second value to evaluate. This value must be greater than value 1 to pass check.

assert_is_less_than_or_equal()

Checks if a given $value1 is less than or equal to $value2.

protected assert_is_less_than_or_equal(mixed $value1, mixed $value2) : void
Parameters
$value1 : mixed

The first value to evaluate. This value must be less than or equal to value 2 to pass check.

$value2 : mixed

The second value to evaluate. This value must be greater than or equal to value 1 to pass check.

assert_is_not_empty()

Checks if a given value is not empty.

protected assert_is_not_empty(mixed $value[, string $message = 'Expected value to not be empty.' ]) : mixed
Parameters
$value : mixed

The value to evaluate.

$message : string = 'Expected value to not be empty.'

Set the error message embedded in any AssertionError thrown by this method.

Tags
throws
AssertionError

When the value is not empty.

assert_is_true()

Checks if a given value is a `true` boolean.

protected assert_is_true(mixed $value[, string $message = "Expected value to equal 'true'." ]) : mixed
Parameters
$value : mixed

The value to evaluate.

$message : string = "Expected value to equal 'true'."

Set the error message embedded in any AssertionError thrown by this method.

Tags
throws
AssertionError

When the value is not true.

assert_not_equals()

Checks if two values are not equal.

protected assert_not_equals(mixed $value1, mixed $value2[, string $message = '' ]) : mixed
Parameters
$value1 : mixed

The first value to evaluate.

$value2 : mixed

The second value to evaluate.

$message : string = ''

Set the error message embedded in any AssertionError thrown by this method.

Tags
throws
AssertionError

When $value1 and $value2 are equal.

assert_str_contains()

Checks if a string contains another string.

protected assert_str_contains(string $haystack, string $needle) : mixed
Parameters
$haystack : string

The string to check for the $needle in.

$needle : string

The string to check for within the $haystack.

Tags
throws
AssertionError

When $haystack string did not contain the $needle string.

assert_str_does_not_contain()

Checks if a string does not contains another string.

protected assert_str_does_not_contain(string $haystack, string $needle) : mixed
Parameters
$haystack : string

The string to check for the $needle in.

$needle : string

The string to check for within the $haystack.

Tags
throws
AssertionError

When $haystack string did contain the $needle string.

assert_throws()

Checks if an embedded callable raises a specific error

protected assert_throws(array<string|int, mixed> $exceptions, mixed $callable[, string $message = '' ]) : mixed
Parameters
$exceptions : array<string|int, mixed>

An array of Exceptions this method will expect the callable to raise.

$callable : mixed

An anonymous function that raises one of the $exceptions.

$message : string = ''

Set the error message embedded in any AssertionError thrown by this method.

Tags
throws
AssertionError

When the $callable did not raise one of the $exceptions.

assert_throws_response()

Checks if an embedded callable raises a specific RESTAPI\Core\Response exception and ensures that the expected 'return' code is given

protected assert_throws_response(string $response_id, int $code, mixed $callable) : mixed
Parameters
$response_id : string

The unique response return code string to check the Response object for.

$code : int

The Response object's error code. This typically corresponds with an HTTP status code.

$callable : mixed

An anonymous function that raises one of the $exceptions.

assert_type()

Checks if a given value is of a given type.

protected assert_type(mixed $value, string $type[, string $message = '' ]) : mixed
Parameters
$value : mixed

The value to evaluate.

$type : string

The string representation of the type to expect. This must match a gettype() return value.

$message : string = ''

Set the error message embedded in any AssertionError thrown by this method.

Tags
throws
AssertionError

When the $value's type does not match the given $type.

get_envs()

Obtains the environment variable with a given name.

protected get_envs() : void

        
On this page

Search results