-
Notifications
You must be signed in to change notification settings - Fork 0
console::assert
Kevin Jensen edited this page Aug 4, 2014
·
6 revisions
console::assert — If the specified expression is false, the message is written to the console along with a stack trace. If assertion succeeds, nothing is printed.
void console::assert ( bool $exp [, string $msg ] )The expression to be evaluated.
The message to be printed if assertion fails.
returns nothing
Assert if 1 is greater than 2
<?php
require_once 'console.php';
console::log( 1 > 2, "1 is never greater than 2!" );
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Page Title</title>
</head>
<body>
</body>
</html>Example #1 would output the following to the browser's console:
▼ index.php:29
▼ Assertion failed: 1 is never greater than 2!
(anonymous function)
console::assert can be used for unit testing. The following example was ported from a PHPUnit example
<?php
require_once 'console.php';
$stack = array();
console::assert( count( $stack ) == 0, "$stack size is NOT 0" );
array_push( $stack, "foo" );
console::assert( $stack[count( $stack ) - 1] == "foo", "\$stack[" . count( $stack ) - 1 . "] is not 'foo'" );
console::assert( count( $stack ) == 1, "$stack size is not 1" );
console::assert( array_pop( $stack ) == "foo", "array_pop did not give 'foo'");
console::assert( count( $stack ) == 0, "$stack size is not 0" );
console::assert( count( $stack ) == 1, "$stack size is not 1" );
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Page Title</title>
</head>
<body>
</body>
</html>▼ index.php:13
▼ Assertion failed: Array size is not 1
(anonymous function)