PHP code example of k2gl / phpunit-fluent-assertions

1. Go to this page and download the library: Download k2gl/phpunit-fluent-assertions 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/ */

    

k2gl / phpunit-fluent-assertions example snippets


// arrange
$user = UserFactory::createOne([
    'phone' => $phoneBefore = faker()->e164PhoneNumber;
]);

// act
$user->setPhone(
    $phoneAfter = faker()->e164PhoneNumber
);

// assert

// traditional PHPUnit assertions
self::assertSame(expected: $phoneAfter, actual: $user->getPhone());
self::assertNotSame(expected: $phoneBefore, actual: $user->getPhone());

// fluent assertions
fact($user->getPhone())
    ->is($phoneAfter)
    ->equals($phoneAfter)
    ->not($phoneBefore)
    ->true()
    ->notTrue()
    ->false()
    ->notFalse()
    ->null()
    ->notNull()
    ->matchesRegularExpression('#^\d+$#')
    ->notMatchesRegularExpression('#^\D+$#')
    ->containsString('alpha')
    ->notContainsString('alpha')
    ->containsStringIgnoringCase('beta')
    ->notContainsStringIgnoringCase('beta')
    ->count(5)
    ->notCount(5)
    ->arrayHasKey('echo')
    ->arrayNotHasKey('echo')
    ->instanceOf(UserFactory::class)
    ->notInstanceOf(UserFactory::class)
    ->ulid() // Universally Unique Lexicographically Sortable Identifier https://github.com/ulid/spec
    ...
    ;

fact(
    [
        'a' => ['any' => 'thing'],
        'b' => ['any' => 'thing', 'type' => 'candy', 'color' => 'green'],
        'c' => ['miss' => 'kiss', 'foo' => 'bar', 'any' => 'thing'],
        'd' => ['any' => 'thing'],
    ]
)->arrayContainsAssociativeArray(
    [
        'c' => ['foo' => 'bar', 'miss' => 'kiss'],
        'b' => ['color' => 'green'],
    ]
); // true

self::assert...($x, $y)