A small assertion library for php that uses a fluent interface and lambda expressions for building assertion rules.
The FluentValidation library for .NET informs the design of this library.
FluentAsserter::assertThat( $amount )
->isGreaterThan(0)
->assert();
FluentAsserter::assertThat( $customer )
->isNotEmpty()
->assert();
$customerDiscount = function( $customer ) {
return $customer->getDiscount();
};
FluentAsserter::assertThat( $customer )
->withClosure( $customerDiscount )
->isGreaterThan( 0 )
->when( function() use ($customer) {
return $customer->hasDiscount();
})
->assert();
$customerAddress = function( $customer ) {
return $customer->getAddress();
};
FluentAsserter::assertThat( $customer )
->withClosure( $customerAddress )
->hasLength( 20, 250 )
->assert();
$customerPostalCode = function( $customer ) {
return $customer->getPostalcode();
};
$beAValidPostcode = function( $postalcode ) {
// postal code validation
};
FluentAsserter::assertThat( $customer )
->withClosure( $customerPostalCode )
->must( $beAValidPostalcode )
->withMessage( "postalcode format is not valid" )
->assert();