PHP code example of bovigo / assert

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

    

bovigo / assert example snippets


assertThat(303, equals(303));
assertThat($someArray, isOfSize(3), 'array always must have size 3');

use function bovigo\assert\assertThat;
use function bovigo\assert\predicate\isOfSize;
use function bovigo\assert\predicate\equals;
// ... and so on

assertThat($value, isNull());

assertThat($value, isNotNull());

assertThat($value, isEmpty());

assertThat($value, isNotEmpty());

assertThat($value, isTrue());

assertThat($value, isFalse());

assertThat($value, equals('Roland TB 303'));

assertThat($value, equals(5)->withDelta(0.1));

assertThat($value, isNotEqualTo('Roland TB 303'));

assertThat($value, isNotEqualTo(5)->withDelta(0.1));

assertThat($value, isInstanceOf(\stdClass::class));

assertThat($value, isNotInstanceOf(\stdClass::class));

assertThat($value, isSameAs($anotherValue));

assertThat($value, isNotSameAs($anotherValue));

assertThat($value, isOfSize(3));

assertThat($value, isNotOfSize(3));

assertThat($value, isOfType('resource'));

assertThat($value, isNotOfType('resource'));

assertThat($value, isGreaterThan(3));

assertThat($value, isGreaterThanOrEqualTo(3));

assertThat($value, isLessThan(3));

assertThat($value, isLessThanOrEqualTo(3));

assertThat($value, contains('Roland TB 303'));

assertThat($value, isArray()->and(contains('Roland TB 303')));
assertThat($value, isString()->and(contains('Roland TB 303')));
assertThat($value, isInstanceOf(\Iterator::class)->and(contains('Roland TB 303')));

assertThat($value, doesNotContain('Roland TB 303'));

assertThat($value, hasKey('roland'));

assertThat($value, doesNotHaveKey('roland'));

assertThat($value, containsSubset(['TB-303', 'TR-808']));

assertThat($value, matches('/^([a-z]{3})$/'));

assertThat($value, doesNotMatch('/^([a-z]{3})$/'));

assertThat($value, matchesFormat('%w'));

assertThat($value, doesNotMatchFormat('%w'));

assertThat($value, isExistingFile());
assertThat($value, isExistingFile('/path/to/files'));

assertThat($value, isNonExistingFile());
assertThat($value, isNonExistingFile('/path/to/files'));

assertThat($value, isExistingDirectory());
assertThat($value, isExistingDirectory('/path/to/directories'));

assertThat($value, isNonExistingDirectory());
assertThat($value, isNonExistingDirectory('/path/to/directories'));

assertThat($value, startsWith('foo'));

assertThat($value, startsWith('foo'));

assertThat($value, endsWith('foo'));

assertThat($value, doesNotEndWith('foo'));

assertThat($value, each(isInstanceOf($expectedType));

assertThat($value, isNotEmpty()->and(each(isInstanceOf($expectedType))));

assertThat($value, each('is_nan'));
assertThat($value, each(function($value) { return substr($value, 4, 3) === 'foo'; }));

assertThat($value, eachKey(isOfType('int'));

assertThat($value, isNotEmpty()->and(eachKey(isOfType('int'))));

assertThat($value, eachKey('is_int'));
assertThat($value, eachKey(function($value) { return substr($value, 4, 3) === 'foo'; }));

assertThat($value, not(isTrue()));

assertThat($value, not('is_nan'));
assertThat($value, not(function($value) { return substr($value, 4, 3) === 'foo'; }));

assertThat($value, isNotEmpty()->and(eachKey(isOfType('int'))));

assertThat($value, isNotEmpty()->and('is_string'));

assertThat($value, equals(5)->or(isLessThan(5)));

assertThat($value, isNull()->or('is_finite'));

assertThat($value, 'is_nan');

assertThat(
        $value,
        function($value)
        {
            if (!is_string($value)) {
                throw new \InvalidArgumentException(
                        'Given value is not a string.'
                );
            }

            return substr($value, 4, 3) === 'foo';
        }
);

try {
    somethingThatThrowsFooException();
    fail('Expected ' . FooException::class . ', gone none');
} catch (FooException $fo) {
    // some assertions on FooException
}

expect(function() {
    // some piece of code which is expected to throw SomeException
})->throws(SomeException::class);

expect(function() {
    // some piece of code which is expected to throw any exception
})->throws();

$exception = new \Exception('failure');
expect(function() use ($exception) {
    throw $exception;
})->throws($exception);

expect(function() {
    // some piece of code which is expected to throw SomeException
})
->throws(SomeException::class)
->withMessage('some failure occured');

expect(function() {
    // some piece of code which is expected to throw SomeException
})
->throws(SomeException::class)
->with(
        function(SomeException $e) { return null !== $e->getPrevious(); },
        'exception does have a previous exception'
);

expect(function() {
    // some piece of code which is expected to not throw SomeException
})->doesNotThrow(SomeException::class);

expect(function() {
    // some piece of code which is expected to not throw any exception
})->doesNotThrow();

expect(function() {
    // some piece of code which is expected to trigger an error
})->triggers(E_USER_ERROR);

expect(function() {
    // some piece of code which is expected to trigger an error
})->triggers();

expect(function() {
    // some piece of code which is expected to trigger an error
})
->triggers(E_USER_WARNING)
->withMessage('some error occured');

expect(function() {
    // some piece of code here
})
->after(SomeClass::$value, equals(303));

expect(function() {
    // some piece of code here
})
->doesNotThrow()
->after(SomeClass::$value, equals(303));

expect(function() {
    // some piece of code here
})
->throws(SomeException::class)
->after(SomeClass::$value, equals(303));

outputOf(
        function() { echo 'Hello you!'; },
        equals('Hello world!')
);