1. Go to this page and download the library: Download exeque/fluent-assert library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
exeque / fluent-assert example snippets
use Exeque\FluentAssert\Assert;
$assert = Assert::for('foo bar');
// All methods from webmozart/assert are available
// The arguments are the same except for the first one (the value to assert).
$assert
->string() // Webmozart/Assert::string(..., $message = '')
->startsWith('foo') // Webmozart/Assert::startsWith(..., $prefix, $message = '')
->endsWith('bar'); // Webmozart/Assert::endsWith(..., $suffix, $message = '')
// You can retrieve via the `value()` method.
$value = $assert->value(); // 'foo bar'
use Exeque\FluentAssert\Assert;
class Employee
{
public function __construct($id)
{
Assert::for($id)
->integer('The employee ID must be an integer. Got: %s')
->greaterThan(0, 'The employee ID must be a positive integer. Got: %s');
}
}
new Employee('foo bar');
// -> ExeQue\FluentAssert\Exceptions\InvalidArgumentException:
// The employee ID must be an integer. Got: string
new Employee(-10);
// -> ExeQue\FluentAssert\Exceptions\InvalidArgumentException:
// The employee ID must be a positive integer. Got: -10
use Exeque\FluentAssert\Assert;
$assert = Assert::for('fizz buzz');
// Using `and()` will throw an exception if any of the assertions fail.
// The errors will be combined into a single exception.
$assert->and(
fn (Assert $assert) => $assert->startsWith('foo'),
fn (Assert $assert) => $assert->endsWith('bar'),
);
// -> ExeQue\FluentAssert\Exceptions\BulkInvalidArgumentException:
// Expected a value to start with "foo" (Got: "fizz buzz"), or expected a value to end with "bar" (Got: "fizz buzz").
// Using `or()` will only throw an exception if all the assertions fail.
// The errors will be combined into a single exception.
$assert->or(
fn (Assert $assert) => $assert->startsWith('foo'),
fn (Assert $assert) => $assert->startsWith('fizz'),
):
// -> Does not fail
$assert->or(
fn (Assert $assert) => $assert->startsWith('foo'),
fn (Assert $assert) => $assert->startsWith('bar'),
):
// -> ExeQue\FluentAssert\Exceptions\BulkInvalidArgumentException:
// Expected a value to start with "foo" (Got: "fizz buzz"), or expected a value to start with "bar" (Got: "fizz buzz").
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"
use Exeque\FluentAssert\Assert;
use ExeQue\FluentAssert\ConditionAssert;
$assert = Assert::for(['foo', 'bar', 'baz']);
// The `when()` method takes a callable that may return a boolean value
// or a `ConditionAssert` object.
$assert->when(
// Any truthy value will be considered true
condition: fn (Assert $assert) => true,
// [Required] Assertions to apply if the condition is true
then: fn (Assert $assert) => null,
// [Optional] Assertions to apply if the condition is false
otherwise: fn (Assert $assert) => null,
);
// If the condition uses the `Assert` provided as an argument and
// returns null (or nothing) and the inner assertion did not fail,
// then that is considered true.
$assert = Assert::for('foo bar');
$assert->when(
function (Assert $assert) {
$assert->string(); // Returns nothing
},
fn(Assert $assert) => null, // Is called
);
$assert->when(
function (Assert $assert) {
$assert->string();
return null;
},
fn(Assert $assert) => null, // Is called
);
$assert->when(
function (Assert $assert) {
$assert->isArray();
},
fn(Assert $assert) => null, // Is not called
fn(Assert $assert) => null, // Is called
);
use Exeque\FluentAssert\Assert;
$assert = Assert::for([1, 2, 3, 4]);
$assert->not(
fn (Assert $assert) => $assert->arrayContains(3),
'Input cannot contain the number 3'
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.