Assert
The assert
interface is a non-chainable interface for writing assertions.
->equal(actual, expected, [message])
- @param
mixed
$actual - @param
mixed
$expected - @param
string
$message [optional]
Asserts that actual
is loosely equal to expected
.
<?php
$assert->equal(1, 1);
$assert->equal('1', 1);
?>
->notEqual(actual, expected, [message])
- @param
mixed
$actual - @param
mixed
$expected - @param
string
$message [optional]
Asserts that actual
does not equal expected
.
<?php
$assert->notEqual(1, 2);
$assert->notEqual('hi', 'bye');
?>
->throws(fn, exceptionType, exceptionMessage, [message])
- @param
callable
$fn - @param
string
$exceptionType - @param
string
$exceptionMessage - @param
string
$message [optional]
Assert that fn
throws an exception of type exceptionType
when invoked. Optionally asserts that the exception message is equal to exceptionMessage
.
<?php
$assert->throws([$obj, 'method'], 'RuntimeException');
$assert->throws(function() {
throw new Exception('hello');
}, 'Exception', 'hello');
?>
->doesNotThrow(fn, exceptionType, exceptionMessage, [message])
- @param
callable
$fn - @param
string
$exceptionType - @param
string
$exceptionMessage - @param
string
$message [optional]
Asserts that fn
does not throw an exception of type exceptionType
. Optionally asserts that the exception message does not equal exceptionMessage
.
<?php
$assert->doesNotThrow([$obj, 'method'], 'RuntimeException');
?>
->ok(value, [message])
- @param
mixed
$value - @param
string
$message [optional]
Asserts that value
is truthy. That is it is true when cast to (bool)
.
<?php
$assert->ok(true);
$assert->ok('not empty');
?>
->notOk(value, [message])
- @param
mixed
$value - @param
string
$message [optional]
Asserts that value
is not truthy.
<?php
$assert->notOk(false);
$assert->notOk([]);
$assert->notOk("");
?>
->strictEqual(actual, expected, [message])
- @param
mixed
$actual - @param
mixed
$expected - @param
string
$message [optional]
Asserts that actual
is strictly equal (===) to expected
.
<?php
$assert->strictEqual(1, 1);
$array = [1,2,3];
$assert->strictEqual($array, $array);
?>
->notStrictEqual(actual, expected, [message])
- @param
mixed
$actual - @param
mixed
$expected - @param
string
$message [optional]
Asserts that actual
is not strictly equal (===) to expected
.
<?php
$assert->notStrictEqual(1, 2);
$assert->notStrictEqual([1,2], [1,2]);
?>
->match(string, pattern, [message])
- @param
string
$string - @param
string
$pattern - @param
string
$message [optional]
Asserts that pattern
matches string
.
<?php
$assert->match('hello', '/^he/');
?>
->notMatch(string, pattern, [message])
- @param
string
$string - @param
string
$pattern - @param
string
$message [optional]
Asserts that pattern
does not match string
.
<?php
$assert->notMatch('goodbye', '/hi/');
?>
->operator(left, operator, right, [message])
- @param
mixed
$left - @param
string
$operator - @param
mixed
$right - @param
string
$message [optional]
Compares the left
and right
operands with the assertion identified by operator
.
<?php
$assert->operator(1, '<', 2);
$assert->operator('hi', '==', 'hi');
$assert->operator(2, '>=', 1);
?>
->lengthOf(countable, length, [message])
- @param
string|array|Countable
$countable - @param
int
$length - @param
string
$message [optional]
Asserts that countable
has length equal to length
.
<?php
$assert->lengthOf('leo', 3);
$assert->lengthOf([1,2,3], 3);
?>
->include(haystack, needle, [message])
- @param
array|string
$haystack - @param
mixed
$needle - @param
string
$message [optional]
Asserts that the string, array, or object implementing `ArrayAccess` haystack
includes the substring or value identified by needle
.
<?php
$assert->include([1,2,3], 2);
$assert->include('goodbye', 'good');
?>
->notInclude(haystack, needle, [message])
- @param
array|string
$haystack - @param
mixed
$needle - @param
string
$message [optional]
Asserts that string or array haystack
does not include the substring or value identified by needle
.
<?php
$assert->notInclude([1,2,3], 4);
$assert->notInclude('hello', 'good');
?>
->instanceOf(object, class, [message])
- @param
object
$object - @param
string
$class - @param
string
$message [optional]
Asserts that object
is an instance of class
.
<?php
$assert->instanceOf($request, 'Symfony\Component\HttpFoundation\Request');
?>
->notInstanceOf(object, class, [message])
- @param
object
$object - @param
string
$class - @param
string
$message [optional]
Asserts that object
is not an instance of class
.
<?php
$assert->notInstanceOf($response, 'Symfony\Component\HttpFoundation\Request');
?>
->property(object, property, [message])
- @param
object|array
$object - @param
string|int
$property - @param
string
$message [optional]
Asserts that object or array object
contains the index or property identified by property
.
<?php
$person = new stdClass();
$person->name = "name";
$array = [1,2,3];
$assert->property($person, 'name');
$assert->property($array, 2);
?>
->notProperty(object, property, [message])
- @param
object|array
$object - @param
string|int
$property - @param
string
$message [optional]
Asserts that object or array object
does not contain the index or property identified by property
.
<?php
$person = new stdClass();
$person->name = "name";
$array = [1,2,3];
$assert->notProperty($person, 'age');
$assert->notProperty($array, 5);
?>
->deepProperty(object, property, [message])
- @param
object|array
$object - @param
string|int
$property - @param
string
$messsage [optional]
Asserts that object or array object
contains a deep property or index identified by property
.
<?php
$person = new stdClass();
$person->name = new stdClass();
$person->name->first = 'brian';
$array = [0,1,[1,2,[2,3]]];
$assert->deepProperty($person, 'name->first');
$assert->deepProperty($array, '[2][2][1]');
?>
->notDeepProperty(object, property, [message])
- @param
object|array
$object - @param
string|int
$property - @param
string
$messsage [optional]
Asserts that object or array object
does not contain a deep property or index identified by property
.
<?php
$person = new stdClass();
$person->name = new stdClass();
$person->name->first = 'brian';
$array = [0,1,[1,2,[2,3]]];
$assert->notDeepProperty($person, 'name->last');
$assert->notDeepProperty($array, '[2][2][4]');
?>
->propertyVal(object, property, value, [message])
- @param
object|array
$object - @param
string|int
$property - @param
mixed
$value - @param
string
$message [optional]
Asserts that object or array object
contains the index or property identified by property
and that property has a value of value
.
<?php
$person = new stdClass();
$person->name = "brian";
$array = [1,2,3];
$assert->propertyVal($person, 'name', 'brian');
$assert->propertyVal($array, 2, 3);
?>
->propertyNotVal(object, property, value, [message])
- @param
object|array
$object - @param
string|int
$property - @param
mixed
$value - @param
string
$message [optional]
Asserts that object or array object
contains the index or property identified by property
and that property does not have a value of value
.
<?php
$person = new stdClass();
$person->name = "brian";
$array = [1,2,3];
$assert->propertyNotVal($person, 'name', 'austin');
$assert->propertyNotVal($array, 2, 4);
?>
->deepPropertyVal(object, property, value, [message])
- @param
object|array
$object - @param
string|int
$property - @param
mixed
$value - @param
string
$messsage [optional]
Asserts that object or array object
contains a deep property or index identified by property
and that property has a value of value
.
<?php
$person = new stdClass();
$person->name = new stdClass();
$person->name->first = 'brian';
$array = [0,1,[1,2,[2,3]]];
$assert->deepPropertyVal($person, 'name->first', 'brian');
$assert->deepPropertyVal($array, '[2][2][1]', 3);
?>
->deepPropertyNotVal(object, property, value, [message])
- @param
object|array
$object - @param
string|int
$property - @param
mixed
$value - @param
string
$messsage [optional]
Asserts that object or array object
contains a deep property or index identified by property
and that property does not have a value of value
.
<?php
$person = new stdClass();
$person->name = new stdClass();
$person->name->first = 'brian';
$array = [0,1,[1,2,[2,3]]];
$assert->deepPropertyNotVal($person, 'name->first', 'austin');
$assert->deepPropertyNotVal($array, '[2][2][1]', 7);
?>
->typeOf(actual, expected, [message])
- @param
mixed
$actual - @param
string
$expected - @param
string
$message [optional]
Asserts that actual
is of type epxected
.
<?php
$assert->typeOf([], 'array');
$assert->typeOf(tmpfile(), 'resource');
?>
->notTypeOf(actual, expected, [message])
- @param
mixed
$actual - @param
string
$expected - @param
string
$message [optional]
Asserts that actual
is not of type epxected
.
<?php
$assert->notTypeOf([], 'resource');
$assert->notTypeOf(tmpfile(), 'string');
?>
->isTrue(value, [message])
- @param
mixed
$value - @param
string
$messasge [optional]
Asserts that value
is exactly true
.
<?php
$assert->isTrue(true);
?>
->isFalse(value, [message])
- @param
mixed
$value - @param
string
$messasge [optional]
Asserts that value
is exactly false
.
<?php
$assert->isFalse(false);
?>
->isNull(value, [message])
- @param
mixed
$value - @param
string
$message [optional]
Asserts that value
is null
.
<?php
$assert->isNull(null);
?>
->isNotNull(value, [message])
- @param
mixed
$value - @param
string
$messasge [optional]
Asserts that value
is not null
.
<?php
$assert->isNotNull('string');
?>
->isCallable(value, [message])
- @param
mixed
$value - @param
string
$messasge [optional]
Asserts that value
is of type callable
.
<?php
$assert->isCallable(function() {});
?>
->isNotCallable(value, [message])
- @param
mixed
$value - @param
string
$messasge [optional]
Asserts that value
is not of type callable
.
<?php
$assert->isNotCallable('string');
?>
->isObject(value, [message])
- @param
mixed
$value - @param
string
$messasge [optional]
Asserts that value
is an object.
<?php
$assert->isObject(new stdClass());
?>
->isNotObject(value, [message])
- @param
mixed
$value - @param
string
$messasge [optional]
Asserts that value
is not an object.
<?php
$assert->isNotObject(4);
?>
->isArray(value, [message])
- @param
mixed
$value - @param
string
$messasge [optional]
Asserts that value
is an array.
<?php
$assert->isArray([1,2,3]);
?>
->isNotArray(value, [message])
- @param
mixed
$value - @param
string
$messasge [optional]
Asserts that value
is not an array.
<?php
$assert->isNotArray([1,2,3]);
?>
->isString(value, [message])
- @param
mixed
$value - @param
string
$messasge [optional]
Asserts that value
is a string.
<?php
$assert->isString('hello');
?>
->isNotString(value, [message])
- @param
mixed
$value - @param
string
$messasge [optional]
Asserts that value
is not a string.
<?php
$assert->isString(47);
?>
->isNumeric(value, [message])
- @param
mixed
$value - @param
string
$messasge [optional]
Asserts that value
is numeric.
<?php
$assert->isNumeric(7);
$assert->isNumeric('1');
$assert->isNumeric(7.82);
?>
->isNotNumeric(value, [message])
- @param
mixed
$value - @param
string
$messasge [optional]
Asserts that value
is not numeric.
<?php
$assert->isNotNumeric('one');
?>
->isInteger(value, [message])
- @param
mixed
$value - @param
string
$messasge [optional]
Asserts that value
is an integer.
<?php
$assert->isInteger(1);
?>
->isNotInteger(value, [message])
- @param
mixed
$value - @param
string
$messasge [optional]
Asserts that value
is not an integer.
<?php
$assert->isNotInteger(1.787654);
?>
->isDouble(value, [message])
- @param
mixed
$value - @param
string
$messasge [optional]
Asserts that value
is a double.
<?php
$assert->isDouble(7.898);
?>
->isNotDouble(value, [message])
- @param
mixed
$value - @param
string
$messasge [optional]
Asserts that value
is not a double.
<?php
$assert->isNotDouble(7);
?>
->isResource(value, [message])
- @param
mixed
$value - @param
string
$messasge [optional]
Asserts that value
is a resource.
<?php
$assert->isResource(tmpfile());
?>
->isNotResource(value, [message])
- @param
mixed
$value - @param
string
$messasge [optional]
Asserts that value
is not a resource.
<?php
$assert->isNotResource('just a string');
?>
->isBoolean(value, [message])
- @param
mixed
$value - @param
string
$messasge [optional]
Asserts that value
is a boolean.
<?php
$assert->isBoolean(true);
?>
->isNotBoolean(value, [message])
- @param
mixed
$value - @param
string
$messasge [optional]
Asserts that value
is not a boolean.
<?php
$assert->isNotBoolean('true');
?>