Attribute Assertions

BeBat/Verify has the ability to test the value of object and class properties (or “attributes”), even those that are protected or private. While writing assertions about a subject’s internal state is not generally good practice, there are times when inspecting a protected value may be the simplest way of checking your code. The attribute you wish to check can be tacked on after calling verify(), just like if you were accessing it as a public value.

For example, if you had an object called $user with a first_name property that should be equal to 'Alice', you can assert that with the following code:

verify($user)->first_name->is()->equalTo('Alice');

A similar assertion about a class’s static properties might look like the following:

verify(Model::class)->dbc->is()->resource();

If you would rather explicitly identify your attribute/property, you can do so with the attributeNamed() method:

verify($obj)->attributeNamed('fooBar')->is()->false();

All of BeBat/Verify’s assertions should be compatible with reading object or class attributes. In addition, attributes fully support chaining and assertion modifiers. The only exception is that once your chain contains an attribute, you can no longer add assertions about their containing object. Put another way, always write your assertions about an object first before writing any about its attributes. For example:

verify($model)->isNot()->null()
    ->and()->is()->instanceOf(MyModelClass::class)
    ->and()->first_name->withoutCase()->is()->equalTo('sally')
    ->and()->last_name->withoutCase()->doesNot()->contain('smith')
    ->and()->gpa->within(0.01)->is()->equalTo(4.0);