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()
verify($subject)->is()->identicalTo('some value');
equalTo()
verify($subject)->is()->equalTo('some value');
Note
The behavior of equalTo()
can be changed using the within()
, withoutOrder()
, withoutCase()
, and withoutLinEndings()
modifiers.
equalToFile()
verify($subject)->is()->equalToFile('/path/to/file.txt');
Note
The behavior of equalToFile()
can be changed using the withoutCase()
and withoutLineEndings()
modifiers.
Truthiness
true()
verify($subject)->is()->true();
false()
verify($subject)->is()->false();
null()
verify($subject)->is()->null();
empty()
verify($subject)->is()->empty();
Type
instanceOf()
verify($subject)->is()->instanceOf(MyClass::class);
array()
verify($subject)->is()->array();
bool()
verify($subject)->is()->bool();
callable()
verify($subject)->is()->callable();
closed()
verify($subject)->is()->closed();
Attention
The closed()
assertion requires PHPUnit 9 or later.
float()
verify($subject)->is()->float();
int()
verify($subject)->is()->int();
iterable()
verify($subject)->is()->iterable();
numeric()
verify($subject)->is()->numeric();
object()
verify($subject)->is()->object();
resource()
verify($subject)->is()->resource();
scalar()
verify($subject)->is()->scalar();
string()
verify($subject)->is()->string();
Numeric Values
lessThan()
verify($subject)->is()->lessThan($value);
lessOrEqualTo()
verify($subject)->is()->lessOrEqualTo($value);
greaterThan()
verify($subject)->is()->greaterThan($value);
greaterOrEqualTo()
verify($subject)->is()->greaterOrEqualTo($value);
finite()
verify($subject)->is()->finite();
infinite()
verify($subject)->is()->infinite();
nan()
verify($subject)->is()->nan();
String Values
contain()
verify($subject)->will()->contain('value');
Note
The behavior of contain()
can be changed using the withoutCase()
and withoutLinEndings()
modifiers.
startWith()
verify($subject)->wil()->startWith('value');
endWith()
verify($subject)->will()->endWith('value');
matchRegExp()
verify($subject)->will()->matchRegExp('/myregexp/');
matchFormat()
verify($subject)->will()->matchFormat('%i');
See also
See PHPUnit’s assertStringMatchesFormat() for details on format placeholders.
matchFormatFile()
verify($subject)->will()->matchFormatFile('/path/to/format.txt');
Array Values
contain()
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()
verify($subject)->has()->key('value');
count()
verify($subject)->has()->count(4);
sameSizeAs()
verify($subject)->is()->sameSizeAs(['some', 'array']);
containOnly()
verify($subject)->will()->containOnly('string');
verify($subject)->will()->containOnly(SomeClass::class);
Note
The containOnly()
assertion works for both internal types and classes.
list()
verify($subject)->is()->list();
Attention
The list()
assertion requires PHPUnit 10 or later.
Object & Class Properties
attribute()
verify($subject)->has()->attribute('attributeName');
verify(MyClass::class)->has()->attribute('attributeName');
Note
The attribute()
assertion works with both classes and object instances.
staticAttribute()
verify(MyClass::class)->has()->staticAttribute('attributeName');
JSON
json()
verify($subject)->is()->json();
equalToJsonString()
verify($subject)->is()->equalToJsonString('{"json": "string"}');
equalToJsonFile()
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()
verify_file($path)->does()->exist();
file()
verify_file($path)->is()->file();
directory()
verify_file($path)->is()->directory();
link()
verify_file($path)->is()->link();
Contents & Equality
equalTo()
verify_file($file)->is()->equalTo('value');
Note
The behavior of equalTo()
can be changed using the withoutCase()
and withoutLinEndings()
modifiers.
equalToFile()
verify_file($file)->is()->equalToFile('/path/to/file.txt');
Note
The behavior of equalToFile()
can be changed using the withoutCase()
and withoutLineEndings()
modifiers.
contain()
verify_file($file)->will()->contain('value');
Note
The behavior of contain()
can be changed using the withoutCase()
and withoutLinEndings()
modifiers.
containFiles()
verify_file($directory)->will()->containFiles(['File1', 'File2']);
linkTarget()
verify_file($link)->has()->linkTarget('/some/other/file');
Permissions
readable()
verify_file($file)->is->readable();
writable()
verify_file($file)->is()->writable()
executable()
verify_file($file)->is()->executable()
permission()
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()
verify_file($file)->has()->owner(501);
verify_file($file)->has()->owner('username');
Note
The owner()
assertion supports both user names and IDs.
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()
verify_file($file)->is()->json();
equalToJsonString()
verify_file($file)->is()->equalToJsonString('{"json": "string"');
equalToJsonFile()
verify_file($file)->is()->equalToJsonFile('/path/to/file.json');