Download the PHP package exeque/fluent-assert without Composer
On this page you can find all versions of the php package exeque/fluent-assert. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package fluent-assert
Fluent Assert
This library is built upon webmozart/assert and provides a fluent interface
for assertions, which allows for a more compact
set of assertions on the same value.
It is a useful tool for those who want to use webmozart/assert but prefer a more fluent interface.
Built with love ❤️
Installation
Use Composer to install the library.
Differences
There are a few differences between this library and webmozart/assert:
- The
all*methods don't use the source methods inwebmozart/assert.- They work as you would expect, but they use
each()internally to apply the assertions to each value. See Iterative Assertions.
- They work as you would expect, but they use
- The exception throw on failures are different.
\ExeQue\FluentAssert\Exceptions\InvalidArgumentExceptionis thrown on failures.- It extends
\Webmozart\Assert\InvalidArgumentException.
- It extends
\ExeQue\FluentAssert\Exceptions\BulkInvalidArgumentExceptionis thrown on grouped assertions- It extends
\ExeQue\FluentAssert\Exceptions\InvalidArgumentException. - It provides multiple exceptions in the
getExceptions()method. - See Grouped Assertions.
- It extends
\ExeQue\FluentAssert\Exceptions\IndexedInvalidArgumentExceptionis thrown on positional or iterative assertions- It extends
\ExeQue\FluentAssert\Exceptions\InvalidArgumentException. - It provides the index of the failing assertion in the
getIndex()method. - It provides the original message in the
getOriginalMessage()method. - See Iterative Assertions.
- It extends
Example
The example from webmozart/assert can be rewritten as:
Grouped Assertions
The fluent interface provides a and() and or() method to group assertions.
This is useful when you want to make multiple assertions at once and a singular combined error.
Iterative Assertions
The each() method allows you to iterate over an iterable and apply assertions to each value.
The errors thrown will have a prefix of failing index applied.
The IndexedInvalidArgumentException can provide the index that failed use getIndex() and the original message using
getOriginalMessage().
Alternative versions keys/forKeys is available to apply assertions to the keys of an iterable.
Positional Assertions
The at()/atMany() methods allows you to assert a value at a specific index in an array, an ArrayAccess instance, or a properties of an object.
It automatically calls keyExists() on the index before applying the assertion.
The errors thrown will have a prefix of failing index applied.
Conditional Assertions
Sometimes you only want to apply certain assertions if a condition is met or not. The when() method allows you to do
this.
Alternative conditions exist as well:
whenAt()- Combineswhen()withat()- An assertion of the value at the index is provided for the$thencallback
Inverted Assertions
The not() method allows you to invert the assertion. This is useful when the assertions MUST fail.
Assertions
All assertions from webmozart/assert are available. See here
An extended implementation of webmozart/assert is available in the ExeQue\FluentAssert\Base class.
The following additional assertions are available:
| Method | Description |
|---|---|
hasIndices($value, string $message = '') |
Check that a value has indices (array or ArrayAccess) |
arrayContains($array, mixed $value, string $message = '') |
Check that a value contains another value |
type($value, string $type, string $message = '') |
Check that a value is of a certain type (using get_debug_type()) |
publicPropertyExists($value, string $name, string $message = '') |
Check that a value has a public property |
protectedPropertyExists($value, string $name, string $message = '') |
Check that a value has a protected property |
privatePropertyExists($value, string $name, string $message = '') |
Check that a value has a private property |
staticPropertyExists($value, string $name, string $message = '') |
Check that a value has a static property |
instancedPropertyExists($value, string $name, string $message = '') |
Check that a value has an instanced property |
enumCaseExists($value, string $enumClass, string $message = '') |
Check that a value is a case in an enum |
enumValueExists($value, string $enumClass, string $message = '') |
Check that a value is a value in an enum |
keysExists($value, array $keys, string $message = '') |
Check that a value has all the specified keys (array or ArrayAccess) |
nullOr($value, callable $callback) |
Execute the callback if the value is not null |
fulfills($value, callable $callback, string $message = '') |
Check that a value fulfills a custom condition |
Macros
You can define your own custom assertions using macros.
Development
The library uses code generation to create definitions for all methods present in webmozart/assert.
To trigger the generation run:
Testing
You can run the tests using the following command:
License
This library is open-sourced software licensed under the MIT license.
Appreciation
Big thanks to Bernard Schussek (and community) for the webmozart/assert.
It is one of the first dependencies I include in any project and I use it extensively.