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\InvalidArgumentException
is thrown on failures.- It extends
\Webmozart\Assert\InvalidArgumentException
.
- It extends
\ExeQue\FluentAssert\Exceptions\BulkInvalidArgumentException
is 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\IndexedInvalidArgumentException
is 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 array or an ArrayAccess
object 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()
.
An alternative version eachKey
is available to apply assertions to the keys of an iterable.
php use ExeQue\FluentAssert\Assert;
// Works with integer indices $assert = Assert::for(['foo', 'bar', 'baz']); $assert->at(0, fn (Assert $assert) => $assert->eq('foo'));
// Works with string indices $assert = Assert::for(['foo' => 'bar', 'baz' => 'qux']); $assert->at('baz', fn (Assert $assert) => $assert->eq('qux'));
$assert = Assert::for(['foo' => 'bar']); $assert->at('foo', fn (Assert $assert) => $assert->eq('fizz')); // -> ExeQue\FluentAssert\Exceptions\IndexedInvalidArgumentException: // [foo]: Expected a value equal to "fizz". Got: "bar" bash composer build bash composer test
## License
This library is open-sourced software licensed under the [MIT license](LICENSE.md).
## 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.