Assertions

This page lists all of the assertions built in to BeBat/Verify.

Important

All of the examples on this page use positive conjunctions. If you want to verify the inverse of any of these assertions you should use a negative conjunction instead.

Value Assertions

To make assertions about the value of some entity you should pass it to verify() and then chain your assertion(s) after a conjunction.

Equality

identicalTo()

Assert that subject has the same type and value as some other entity
verify($subject)->is()->identicalTo('some value');

equalTo()

Assert that subject has the same value as some other entity
verify($subject)->is()->equalTo('some value');

Note

The behavior of equalTo() can be changed using the within(), withoutOrder(), withoutCase(), and withoutLinEndings() modifiers.

equalToFile()

Assert that subject has the same value as the contents of a file
verify($subject)->is()->equalToFile('/path/to/file.txt');

Note

The behavior of equalToFile() can be changed using the withoutCase() and withoutLineEndings() modifiers.

Truthiness

true()

Assert that subject is true
verify($subject)->is()->true();

false()

Assert that subject is false
verify($subject)->is()->false();

null()

Assert that subject is null
verify($subject)->is()->null();

empty()

Assert that subject is empty
verify($subject)->is()->empty();

Type

instanceOf()

Assert that subject is an instance of some class
verify($subject)->is()->instanceOf(MyClass::class);

array()

Assert that subject is an array
verify($subject)->is()->array();

bool()

Assert that subject is a boolean
verify($subject)->is()->bool();

callable()

Assert that subject is callable
verify($subject)->is()->callable();

closed()

Assert that subject is a closed resource
verify($subject)->is()->closed();

Attention

The closed() assertion requires PHPUnit 9 or later.

float()

Assert that subject is a floating point number
verify($subject)->is()->float();

int()

Assert that subject is an integer number
verify($subject)->is()->int();

iterable()

Assert that subject is an iterable type
verify($subject)->is()->iterable();

numeric()

Assert that subject is a numeric type
verify($subject)->is()->numeric();

object()

Assert that subject is an object
verify($subject)->is()->object();

resource()

Assert that subject is a resource
verify($subject)->is()->resource();

scalar()

Assert that subject is a scalar value
verify($subject)->is()->scalar();

string()

Assert that subject is a string
verify($subject)->is()->string();

Numeric Values

lessThan()

Assert that subject is less than some value
verify($subject)->is()->lessThan($value);

lessOrEqualTo()

Assert that subject is less than or equal to some value
verify($subject)->is()->lessOrEqualTo($value);

greaterThan()

Assert that subject is greater than some value
verify($subject)->is()->greaterThan($value);

greaterOrEqualTo()

Assert that subject is greater than or equal to some value
verify($subject)->is()->greaterOrEqualTo($value);

finite()

Assert that subject is a finite value
verify($subject)->is()->finite();

infinite()

Assert that subject is an infinite value
verify($subject)->is()->infinite();

nan()

Assert that subject is a NaN (or “not a number”) value
verify($subject)->is()->nan();

String Values

contain()

Assert that subject contains a value
verify($subject)->will()->contain('value');

Note

The behavior of contain() can be changed using the withoutCase() and withoutLinEndings() modifiers.

startWith()

Assert that subject starts with some value
verify($subject)->wil()->startWith('value');

endWith()

Assert that subject ends with some value
verify($subject)->will()->endWith('value');

matchRegExp()

Assert that subject matches a regular expression
verify($subject)->will()->matchRegExp('/myregexp/');

matchFormat()

Assert that subject matches a format pattern
verify($subject)->will()->matchFormat('%i');

See also

See PHPUnit’s assertStringMatchesFormat() for details on format placeholders.

matchFormatFile()

Assert that subject matches a format pattern from a file
verify($subject)->will()->matchFormatFile('/path/to/format.txt');

Array Values

contain()

Assert that subject contains some value
verify($subject)->will()->contain('value');

Note

Unlike PHP and PHPUnit, BeBat/Verify’s contain() performs strict comparison by default for both objects and internal types. If your test(s) require loose type checking you must use a modifier.

Note

The behavior of contain() can be changed using the withoutType() and withoutIdentity() modifiers.

key()

Assert that subject has a given key
verify($subject)->has()->key('value');

count()

Assert that subject has a certain number of elements
verify($subject)->has()->count(4);

sameSizeAs()

Assert that subject has the same number of elements as another array or traversable value
verify($subject)->is()->sameSizeAs(['some', 'array']);

containOnly()

Assert that subject only contains values of a given type
verify($subject)->will()->containOnly('string');
verify($subject)->will()->containOnly(SomeClass::class);

Note

The containOnly() assertion works for both internal types and classes.

list()

Assert that subject is a list (all keys are consecutive numbers starting at 0)
verify($subject)->is()->list();

Attention

The list() assertion requires PHPUnit 10 or later.

Object & Class Properties

attribute()

Assert that subject has some attribute/property
verify($subject)->has()->attribute('attributeName');
verify(MyClass::class)->has()->attribute('attributeName');

Note

The attribute() assertion works with both classes and object instances.

staticAttribute()

Assert that subject has a static attribute/property
verify(MyClass::class)->has()->staticAttribute('attributeName');

JSON

json()

Assert that subject is a valid JSON string
verify($subject)->is()->json();

equalToJsonString()

Assert that subject is equal to a JSON string
verify($subject)->is()->equalToJsonString('{"json": "string"}');

equalToJsonFile()

Assert that subject is equal to a JSON value from a file
verify($subject)->is()->equalToJsonFile('/path/to/file.json');

File Assertions

BeBat/Verify includes assertions specific to filesystem entries. To make assertions about a filesystem entity, pass the path to verify_file() and then chain your assertion(s) after a conjunction.

State & Type

exist()

Assert that a path exists in the filesystem
verify_file($path)->does()->exist();

file()

Assert that a path is a regular file
verify_file($path)->is()->file();

directory()

Assert that a path is a directory
verify_file($path)->is()->directory();

Contents & Equality

equalTo()

Assert that the file’s contents are equal to some string
verify_file($file)->is()->equalTo('value');

Note

The behavior of equalTo() can be changed using the withoutCase() and withoutLinEndings() modifiers.

equalToFile()

Assert that the file’s contents are equal to another file’s
verify_file($file)->is()->equalToFile('/path/to/file.txt');

Note

The behavior of equalToFile() can be changed using the withoutCase() and withoutLineEndings() modifiers.

contain()

Assert that the file’s contents contains some value
verify_file($file)->will()->contain('value');

Note

The behavior of contain() can be changed using the withoutCase() and withoutLinEndings() modifiers.

containFiles()

Assert that a directory contains some files
verify_file($directory)->will()->containFiles(['File1', 'File2']);

linkTarget()

Assert that a symbolic link points to a particular file
verify_file($link)->has()->linkTarget('/some/other/file');

Permissions

readable()

Assert that a file is readable
verify_file($file)->is->readable();

writable()

Assert that a file is writable
verify_file($file)->is()->writable()

executable()

Assert that a file is executable
verify_file($file)->is()->executable()

permission()

Assert that a file has some permission value
verify_file($file)->has()->permission(0755);
verify_file($file)->has()->permission('644');

Note

The permission() assertion accepts permissions in octal format as either strings or integers.

Note

The behavior of permission() can be changed use the matching() modifier.

Ownership

owner()

Assert that a file is owned by a given user
verify_file($file)->has()->owner(501);
verify_file($file)->has()->owner('username');

Note

The owner() assertion supports both user names and IDs.

group()

Assert that a file belongs to a given group
verify_file($file)->has()->group(1001);
verify_file($file)->has()->group('groupname');

Note

The group() assertion supports both group names and IDs.

JSON

json()

Assert that the contents of a file are valid JSON
verify_file($file)->is()->json();

equalToJsonString()

Assert that the contents of a file are equal to a given JSON string
verify_file($file)->is()->equalToJsonString('{"json": "string"');

equalToJsonFile()

Assert that the contents of a file are equal to a different JSON file
verify_file($file)->is()->equalToJsonFile('/path/to/file.json');