PHP Class FUnit

ファイルを表示 Open project: funkatron/funit Class Usage Examples

Public Properties

Property Type Description
$DEBUG debug mode
$DEBUG_COLOR
$INFO_COLOR
$SILENCE boolean if true, nothing will be output but the report
$all_run_tests $tests['name'] => array( 'run'=>false, 'skipped'=>false, 'pass'=>false, 'test'=>null, 'assertions'=>array('func_name'=>'foo', 'func_args'=array('a','b'), 'result'=>$result, 'msg'=>'blahblah'), 'timing' => array('setup'=>ts, 'run'=>ts, 'teardown'=>ts, 'total'=ts),
$current_suite_name
$disable_reporting boolean if true, will not output a report
$disable_run boolean this is used by the test runner utility to suppress FUnit::run() calls in required files
$errors
$fixtures
$setup_func
$suites $suites['name'] => \FUnit\TestSuite
$teardown_func

Protected Properties

Property Type Description
$TERM_COLORS

Public Methods

Method Description
__callStatic ( string $name, array $arguments ) : [type] We use this magic method to map various assertion calls to assert_{$name}() This is so we can break out the call to add_assertion_result() and test the assertion methods properly
after ( Closure $after ) register a function to run after the current suite's tests
assert_all_ok ( array | Traversable $a, callable $callback, string $msg = null ) Iterate over all the items in $a and pass each to $callback. If the callback returns true for all, it passes -- otherwise it fails
assert_equal ( mixed $a, mixed $b, string $msg = null ) assert that $a is equal to $b. Uses == for comparison
assert_expect_fail ( string $msg = null ) Fail an assertion in an expected way
assert_fail ( string $msg = null, $expected = false ) Force a failed assertion
assert_has ( string $needle, array | object $haystack, string $msg = null ) assert that $haystack has a key or property named $needle. If $haystack is neither, returns false
assert_not_equal ( mixed $a, mixed $b, string $msg = null ) assert that $a is not equal to $b. Uses != for comparison
assert_not_has ( string $needle, array | object $haystack, string $msg = null ) assert that $haystack does not have a key or property named $needle. If $haystack is neither an array or object, returns false
assert_not_ok ( mixed $a, string $msg = null ) assert that $a is falsy. Casts $a to boolean for result
assert_not_strict_equal ( mixed $a, mixed $b, string $msg = null ) assert that $a is strictly not equal to $b. Uses !== for comparison
assert_ok ( mixed $a, string $msg = null ) assert that $a is truthy. Casts $a to boolean for result
assert_pass ( string $msg = null ) Force a successful assertion
assert_strict_equal ( mixed $a, mixed $b, string $msg = null ) assert that $a is strictly equal to $b. Uses === for comparison
assert_throws ( callable $callback, array $params, string $exception = null, string $msg = null ) : boolean assert that $callback throws an exception of type $exception
assertion_stats ( array $tests, string $test_name = null ) : array Retrieves stats about assertions run. returns an array with the keys 'total', 'pass', 'fail', 'expected_fail'
before ( Closure $before ) register a function to run before the current suite's tests
debug_out ( $str )
error_handler ( $num, $msg, $file, $line, $vars ) custom error handler to catch errors triggered while running tests. this is registered at the start of FUnit::run() and deregistered at stop
exception_handler ( Exception $e ) : array custom exception handler, massaging the format into the same we use for Errors
exit_code ( ) : integer Retrieve the exit code. It scans all suites for their exit codes, and if AND of them are 1, returns 1. Else 0.
fixture ( string $key, mixed $val = null ) : mixed helper to deal with scoping fixtures. To store a fixture: FUnit::fixture('foo', 'bar'); to retrieve a fixture: FUnit::fixture('foo');
get_current_suite ( ) : FUnit\TestSuite | null get the current suite. If none current, return null
get_suite ( string $name ) : FUnit\TestSuite get an FUnit\TestSuite by name
info_out ( $str )
parse_backtrace ( array $bt_raw ) : array this generates an array of formatted strings to represent the backtrace
report ( string $format = 'text', array $tests_data = [] ) Output a report
report_all_tests ( string $format ) output a report for all tests than have been run from all suites
report_out ( $str )
reset_fixtures ( ) removes all fixtures. This won't magically close connections or files, tho
run ( boolean $report = true, string $filter = null, $report_format = null ) Run the registered tests, and output a report
run_tests ( string $filter = null ) Normally you would not call this method directly
set_debug ( boolean $state ) if true, debugging info will be output.
set_disable_reporting ( boolean $state ) Passing true will disable the reporting output
set_disable_run ( boolean $state ) Passing true will disable the FUnit::run() method. This is used by the test runner utility to avoid calls to run tests within scripts
set_silence ( boolean $state ) if $SILENCE is true, only the report will be output -- no progress etc.
setup ( Closure $setup ) register a function to run at the start of each test
str_truncate ( $str_val, integer $maxlen ) : string truncates a string. If no second param is passed, no change is made
suite ( string $name = self::DEFAULT_SUITE_NAME ) initialize a new test suite. adds a suite to \FUnit::$suites and sets \FUnit::$current_suite_name to the new suite's name
teardown ( Closure $teardown ) register a function to run at the end of each test
test ( string $name, Closure $test ) add a test to be run
test_stats ( array $tests ) : array Retrieves stats about tests run. returns an array with the keys 'total', 'pass', 'run'
val_to_string ( mixed $val, integer $maxlen = 50 ) : string converts all known PHP types into a string representation. Generally would be less verbose with objects and arrays than FUnit::var_export() because it uses json_encode()
var_export ( mixed $val, integer $maxlen = 50 ) : string uses var_export to get string representation of a value. This differs from the standard var_export by removing newlines and allowing optional truncation

Protected Methods

Method Description
add_assertion_result ( TestSuite $suite, string $func_name, array $func_args, mixed $result, $file, $line, $fail_info, string $msg = null, boolean $expected_fail = false ) add the result of an assertion
add_error_data ( array $edata ) adds error data to the main $errors var property and the current test's error array
add_suite ( string $name = self::DEFAULT_SUITE_NAME ) add a test suite
add_test ( string $name, Closure $test ) add a test to be executed
check_current_suite ( ) check if a current suite exists. If not, create a new one and assign its name to static::$current_suite_name
color ( $txt, string $color = 'DEFAULT' ) Format a line for printing. Detects if the script is being run from the command line or from a browser; also detects TTY for color (so pipes work).
out ( $str )
report_text ( array $tests ) Output a report as text
report_xunit ( array $tests ) Output a report as xunit format (Jenkins-compatible)
run_test ( string $name ) Normally you would not call this method directly

Method Details

__callStatic() public static method

We use this magic method to map various assertion calls to assert_{$name}() This is so we can break out the call to add_assertion_result() and test the assertion methods properly
public static __callStatic ( string $name, array $arguments ) : [type]
$name string the assertion short name
$arguments array arguments to pass to "\FUnit::{$assert_name}()"
return [type]

add_assertion_result() protected static method

Normally you would not call this method directly
See also: FUnit::ok()
See also: FUnit::equal()
See also: FUnit::not_equal()
See also: FUnit::strict_equal()
See also: FUnit::not_strict_equal()
protected static add_assertion_result ( TestSuite $suite, string $func_name, array $func_args, mixed $result, $file, $line, $fail_info, string $msg = null, boolean $expected_fail = false )
$suite FUnit\TestSuite the suite to add the result to
$func_name string the name of the assertion function
$func_args array the arguments for the assertion. Really just the $a (actual) and $b (expected)
$result mixed this is expected to be truthy or falsy, and is converted into FUnit::PASS or FUnit::FAIL
$msg string optional message describing the assertion
$expected_fail boolean optional expectation of the assertion to fail

add_error_data() protected static method

adds error data to the main $errors var property and the current test's error array
See also: FUnit::$errors
See also: FUnit::error_handler()
See also: FUnit::exception_handler()
protected static add_error_data ( array $edata )
$edata array ['datetime', 'num', 'type', 'msg', 'file', 'line']

add_suite() protected static method

add a test suite
protected static add_suite ( string $name = self::DEFAULT_SUITE_NAME )
$name string the name associated with the suite

add_test() protected static method

Normally you would not call this method directly
protected static add_test ( string $name, Closure $test )
$name string the name of the test
$test Closure the function to execute for the test

after() public static method

register a function to run after the current suite's tests
public static after ( Closure $after )
$after Closure an anon function

assert_all_ok() public static method

Iterate over all the items in $a and pass each to $callback. If the callback returns true for all, it passes -- otherwise it fails
public static assert_all_ok ( array | Traversable $a, callable $callback, string $msg = null )
$a array | Traversable an array or Traversable (iterable) object
$callback callable [description]
$msg string optional description of assertion

assert_equal() public static method

assert that $a is equal to $b. Uses == for comparison
public static assert_equal ( mixed $a, mixed $b, string $msg = null )
$a mixed the actual value
$b mixed the expected value
$msg string optional description of assertion

assert_expect_fail() public static method

Fail an assertion in an expected way
See also: FUnit::fail()
public static assert_expect_fail ( string $msg = null )
$msg string optional description of assertion

assert_fail() public static method

Force a failed assertion
public static assert_fail ( string $msg = null, $expected = false )
$msg string optional description of assertion

assert_has() public static method

assert that $haystack has a key or property named $needle. If $haystack is neither, returns false
public static assert_has ( string $needle, array | object $haystack, string $msg = null )
$needle string the key or property to look for
$haystack array | object the array or object to test
$msg string optional description of assertion

assert_not_equal() public static method

assert that $a is not equal to $b. Uses != for comparison
public static assert_not_equal ( mixed $a, mixed $b, string $msg = null )
$a mixed the actual value
$b mixed the expected value
$msg string optional description of assertion

assert_not_has() public static method

assert that $haystack does not have a key or property named $needle. If $haystack is neither an array or object, returns false
public static assert_not_has ( string $needle, array | object $haystack, string $msg = null )
$needle string the key or property to look for
$haystack array | object the array or object to test
$msg string optional description of assertion

assert_not_ok() public static method

assert that $a is falsy. Casts $a to boolean for result
public static assert_not_ok ( mixed $a, string $msg = null )
$a mixed the actual value
$msg string optional description of assertion

assert_not_strict_equal() public static method

assert that $a is strictly not equal to $b. Uses !== for comparison
public static assert_not_strict_equal ( mixed $a, mixed $b, string $msg = null )
$a mixed the actual value
$b mixed the expected value
$msg string optional description of assertion

assert_ok() public static method

assert that $a is truthy. Casts $a to boolean for result
public static assert_ok ( mixed $a, string $msg = null )
$a mixed the actual value
$msg string optional description of assertion

assert_pass() public static method

Force a successful assertion
public static assert_pass ( string $msg = null )
$msg string optional description of assertion

assert_strict_equal() public static method

assert that $a is strictly equal to $b. Uses === for comparison
public static assert_strict_equal ( mixed $a, mixed $b, string $msg = null )
$a mixed the actual value
$b mixed the expected value
$msg string optional description of assertion

assert_throws() public static method

If $params is an array, it is passed as arguments to the callback. Otherwise, it is assumed no arguments need to be passed.
public static assert_throws ( callable $callback, array $params, string $exception = null, string $msg = null ) : boolean
$callback callable Callback that should throw an exception
$params array Callback that should throw an exception
$exception string The exception class that should be thrown
$msg string
return boolean

assertion_stats() public static method

If called without passing a test name, retrieves info about all assertions. Else just for the named test
public static assertion_stats ( array $tests, string $test_name = null ) : array
$tests array a set of test results
$test_name string optional the name of the test about which to get assertion stats
return array has keys 'total', 'pass', 'fail', 'expected_fail'

before() public static method

register a function to run before the current suite's tests
public static before ( Closure $before )
$before Closure an anon function

check_current_suite() protected static method

check if a current suite exists. If not, create a new one and assign its name to static::$current_suite_name
protected static check_current_suite ( )

color() protected static method

Colouring code loosely based on http://www.zend.com//code/codex.php?ozid=1112&single=1
See also: FUnit::$TERM_COLORS
protected static color ( $txt, string $color = 'DEFAULT' )
$color string default is 'DEFAULT'

debug_out() public static method

public static debug_out ( $str )

error_handler() public static method

custom error handler to catch errors triggered while running tests. this is registered at the start of FUnit::run() and deregistered at stop
See also: FUnit::run()
public static error_handler ( $num, $msg, $file, $line, $vars )

exception_handler() public static method

We don't actually use this as a proper exception handler, so we can continue execution.
See also: FUnit::run_test()
public static exception_handler ( Exception $e ) : array
$e Exception
return array ['datetime', 'num', 'type', 'msg', 'file', 'line']

exit_code() public static method

If any test fails, the exit code will be set to 1. Otherwise 0
public static exit_code ( ) : integer
return integer 0 or 1

fixture() public static method

I wish we didn't have to do this. In PHP 5.4 we may just be able to bind the tests to an object and access fixtures via $this
See also: FUnit::setup()
public static fixture ( string $key, mixed $val = null ) : mixed
$key string the key to set or retrieve
$val mixed the value to assign to the key. OPTIONAL
return mixed the value of the $key passed.

get_current_suite() public static method

get the current suite. If none current, return null
public static get_current_suite ( ) : FUnit\TestSuite | null
return FUnit\TestSuite | null

get_suite() public static method

get an FUnit\TestSuite by name
public static get_suite ( string $name ) : FUnit\TestSuite
$name string
return FUnit\TestSuite

info_out() public static method

public static info_out ( $str )

out() protected static method

protected static out ( $str )

parse_backtrace() public static method

this generates an array of formatted strings to represent the backtrace
See also: FUnit::error_handler()
See also: FUnit::exception_handler()
public static parse_backtrace ( array $bt_raw ) : array
$bt_raw array the raw backtrace array
return array an array of strings

report() public static method

Output a report
See also: FUnit::report_text()
See also: FUnit::report_xunit()
public static report ( string $format = 'text', array $tests_data = [] )
$format string 'text' (default) or 'xunit'.
$tests_data array set of test data

report_all_tests() public static method

output a report for all tests than have been run from all suites
See also: FUnit::report_text()
See also: FUnit::report_xunit()
public static report_all_tests ( string $format )
$format string 'text' (default) or 'xunit'.

report_out() public static method

public static report_out ( $str )

report_text() protected static method

Normally you would not call this method directly
See also: FUnit::report()
See also: FUnit::run()
protected static report_text ( array $tests )
$tests array

report_xunit() protected static method

This should definitely use one of the xml-specific build/output methods, but really... life is too short
See also: FUnit::report()
See also: FUnit::run()
protected static report_xunit ( array $tests )
$tests array

reset_fixtures() public static method

removes all fixtures. This won't magically close connections or files, tho
See also: FUnit::fixture()
See also: FUnit::teardown()
public static reset_fixtures ( )

run() public static method

Run the registered tests, and output a report
See also: FUnit::run_tests()
See also: FUnit::report()
public static run ( boolean $report = true, string $filter = null, $report_format = null )
$report boolean whether or not to output a report after tests run. Default true.
$filter string optional test case name filter

run_test() protected static method

Run a single test of the passed $name
See also: FUnit::run_tests()
See also: FUnit::setup()
See also: FUnit::teardown()
See also: FUnit::test()
protected static run_test ( string $name )
$name string the name of the test to run

run_tests() public static method

Run all of the registered tests
See also: FUnit::run()
See also: FUnit::run_test()
public static run_tests ( string $filter = null )
$filter string optional test case name filter

set_debug() public static method

Note that $SILENCE will override the $DEBUG state
public static set_debug ( boolean $state )
$state boolean

set_disable_reporting() public static method

Passing true will disable the reporting output
public static set_disable_reporting ( boolean $state )
$state boolean

set_disable_run() public static method

Passing true will disable the FUnit::run() method. This is used by the test runner utility to avoid calls to run tests within scripts
public static set_disable_run ( boolean $state )
$state boolean

set_silence() public static method

This will override the $DEBUG state
public static set_silence ( boolean $state )
$state boolean

setup() public static method

typically you'd use the passed function to register some fixtures
See also: FUnit::fixture()
public static setup ( Closure $setup )
$setup Closure an anon function

str_truncate() public static method

truncates a string. If no second param is passed, no change is made
public static str_truncate ( $str_val, integer $maxlen ) : string
$maxlen integer if > 0, truncate the string rep (default 0)
return string

suite() public static method

initialize a new test suite. adds a suite to \FUnit::$suites and sets \FUnit::$current_suite_name to the new suite's name
public static suite ( string $name = self::DEFAULT_SUITE_NAME )
$name string default is FUnit::DEFAULT_SUITE_NAME

teardown() public static method

typically you'd use the passed function to close/clean-up any fixtures you made
See also: FUnit::fixture()
See also: FUnit::reset_fixtures()
public static teardown ( Closure $teardown )
$teardown Closure an anon function

test() public static method

add a test to be run
public static test ( string $name, Closure $test )
$name string the name for the test
$test Closure the test function

test_stats() public static method

Retrieves stats about tests run. returns an array with the keys 'total', 'pass', 'run'
public static test_stats ( array $tests ) : array
$tests array a set of test results
return array has keys 'total', 'pass', 'run', 'error'

val_to_string() public static method

converts all known PHP types into a string representation. Generally would be less verbose with objects and arrays than FUnit::var_export() because it uses json_encode()
public static val_to_string ( mixed $val, integer $maxlen = 50 ) : string
$val mixed the value to get as a string rep
$maxlen integer if > 0, truncate the string rep (default 50)
return string

var_export() public static method

uses var_export to get string representation of a value. This differs from the standard var_export by removing newlines and allowing optional truncation
public static var_export ( mixed $val, integer $maxlen = 50 ) : string
$val mixed the value to get as a string rep
$maxlen integer if > 0, truncate the string rep (default 50)
return string

Property Details

$DEBUG public_oe static_oe property

debug mode
public static $DEBUG

$DEBUG_COLOR public_oe static_oe property

public static $DEBUG_COLOR

$INFO_COLOR public_oe static_oe property

public static $INFO_COLOR

$SILENCE public_oe static_oe property

if true, nothing will be output but the report
public static bool $SILENCE
return boolean

$TERM_COLORS protected_oe static_oe property

protected static $TERM_COLORS

$all_run_tests public_oe static_oe property

$tests['name'] => array( 'run'=>false, 'skipped'=>false, 'pass'=>false, 'test'=>null, 'assertions'=>array('func_name'=>'foo', 'func_args'=array('a','b'), 'result'=>$result, 'msg'=>'blahblah'), 'timing' => array('setup'=>ts, 'run'=>ts, 'teardown'=>ts, 'total'=ts),
public static $all_run_tests

$current_suite_name public_oe static_oe property

public static $current_suite_name

$disable_reporting public_oe static_oe property

if true, will not output a report
public static bool $disable_reporting
return boolean

$disable_run public_oe static_oe property

this is used by the test runner utility to suppress FUnit::run() calls in required files
public static bool $disable_run
return boolean

$errors public_oe static_oe property

public static $errors

$fixtures public_oe static_oe property

public static $fixtures

$setup_func public_oe static_oe property

public static $setup_func

$suites public_oe static_oe property

$suites['name'] => \FUnit\TestSuite
public static $suites

$teardown_func public_oe static_oe property

public static $teardown_func