Verifier API
You can add functionality to BeBat/Verify by creating a custom assertion class, or “verifier”. Your verifier can then be swapped in using the withVerifier()
method. All verifiers must extend BeBat\Verify\API\Base
, which provides common functionality for assertion methods. This page describes the public and protected methods built into BeBat\Verify\API\Base
that are most relevant to creating a verifier, although it is not a complete list of every method that class includes.
- class BeBat\Verify\API\Base
- assert()
- Returns
BeBat\Verify\API\Assert
(extendsPHPUnit\Framework\Assert
)
Get an instance of PHPUnit’s Assert class. This class exposes much of PHPUnit’s functionality for writing tests & assertions, such as causing a test to fail if an error occurs.
- constraintFactory()
- Returns
BeBat\Verify\Constraint\Factory
The constraint factory is used to create constraints in BeBat/Verify. It includes most of the constraints from PHPUnit as well as those from bebat/filesystem-assertions.
- setAssert($assert)
- Parameters
$assert (
PHPUnit\Framework\Assert
) – An instance of PHPUnit’s assertion object
- Returns
void
Inject an instance of
PHPUnit\Framework\Assert
. Useful for unit testing your verifier.
- setConstraintFactory($factory)
- Parameters
$factory (
BeBat\Verify\Constraint\Factory
) – An instance of the BeBat/Verify constraint factory
- Returns
void
Inject an instance of
BeBat\Verify\Constraint\Factory
. Useful for unit testing your verifier.
- constraint($constraint)
- Parameters
$constraint (
PHPUnit\Framework\Constraint\Constraint
) – Constraint to be applied
- Returns
static
Apply a constraint to your verifier’s subject. This is the simplest way to perform an assertion in your verifier.
- performAssertion($constraint, $value)
- Parameters
$constraint (
PHPUnit\Framework\Constraint\Constraint
) – Constraint to be applied$value (
mixed
) – Value the constraint should apply to
- Returns
static
Apply a constraint to a passed value. This method provides a bit more flexibility over
BeBat\Verify\API\Base::constraint
if there is some resolution required to determine the actual value a constraint should apply to.
- performEqualToAssertion($actual, $expected)
- Parameters
$actual (
mixed
) – The actual value under test$expected (
mixed
) – Value$actual
is expected to equal to
- Returns
static
Apply an
EqualTo()
constraint on$actual
with$expected
. This method will take into account the various modifiers that apply toEqualTo()
, including bothwithoutCase()
andwithoutLineEndings()
simultaneously.
- assertConstraint(constraint, $value)
- Parameters
$constraint (
PHPUnit\Framework\Constraint\Constraint
) – Constraint to be applied$value (
mixed
) – Value the constraint should apply to
- Returns
void
Perform a simple assertion with
$constraint
and$value
. This method is useful for interim assertions about some value before your primary constraint (for example, asserting that a file exists before reading it and doing assertions about its contents). TheassertConstraint()
method does not take into consideration any modifiers or whether the current condition is positive or negative, it just applies$constraint
to$value
.
- getActualValue()
- Returns
mixed
Resolve the actual value of the subject. You may override this method in your verifier if there is some additional logic to resolving your subject’s value, such as reading the result from an object property or function call.
- resetParams()
- Returns
void
Reset the modifiers to their default state and clear the description. This method will be called after performing an assertion. If your verifier includes custom modifiers you should override this method to set their value back to default, and call
parent::resentParams()
.
Fluent Design
Your verifier should use a fluent interface, meaning all publicly available methods should return self
. For your assertion methods, the easiest way to do this is to return a call to BeBat\Verify\API\Base::constraint
with your assertion’s constraint. If you need more flexibility with resolving your subject’s value (such as reading it from a file) you may return BeBat\Verify\API\Base::performAssertion
instead. Lastly, if your assertion is that two values are equal, you can us the BeBat\Verify\API\Base::performEqualToAssertion
to simplify handling the various modifiers and edge cases that constraint supports. All three of these methods will also handle negative assertions for you, as well as resetting the classes internal state for the next assertion.