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,
function($value)
{
if (!is_string($value)) {
throw new \InvalidArgumentException(
'Given value is not a string.'
);
}
return substr($value, 4, 3) === 'foo';
}
);
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));