1. Go to this page and download the library: Download zenstruck/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/ */
zenstruck / assert example snippets
use Zenstruck\Assert;
// passes
Assert::true(true === true, 'The condition was not true.');
// fails
Assert::true(true === false, 'The condition was not true.');
// passes
Assert::false(true === false, 'The condition was not false.');
// fails
Assert::false(true === true, 'The condition was not false.');
use Zenstruck\Assert;
// trigger a "fail"
Assert::fail('This is a failure.');
// trigger a "pass"
Assert::pass();
use Zenstruck\Assert;
$ret = Assert::try(fn() => 'value'); // $ret === 'value'
Assert::try(fn() => throw new \RuntimeException('exception message')); // "fails" with message "exception message"
// customize the failure message
Assert::try(
fn() => throw new \RuntimeException('exception message'),
'Tried to run the code but {exception} with message "{message}" was thrown.'
); // "fails" with message 'Tried to run the code but RuntimeException with message "exception message" was thrown.'
use Zenstruck\Assert;
use Zenstruck\Assert\AssertionFailed;
// failure
Assert::run(function(): void {
if (true) {
AssertionFailed::throw('This failed.');
}
});
// pass
Assert::run(function(): void {
if (false) {
AssertionFailed::throw('This failed.');
}
});
use Zenstruck\Assert;
use Zenstruck\Assert\Type;
Assert::that($something)->is(Type::bool());
Assert::that($something)->is(Type::int());
Assert::that($something)->is(Type::float());
Assert::that($something)->is(Type::numeric());
Assert::that($something)->is(Type::string());
Assert::that($something)->is(Type::callable());
Assert::that($something)->is(Type::iterable());
Assert::that($something)->is(Type::countable());
Assert::that($something)->is(Type::object());
Assert::that($something)->is(Type::resource());
Assert::that($something)->is(Type::array());
Assert::that($something)->is(Type::arrayList()); // [1, 2, 3] passes but ['foo' => 'bar'] does not
Assert::that($something)->is(Type::arrayAssoc()); // ['foo' => 'bar'] passes but [1, 2, 3] does not
Assert::that($something)->is(Type::arrayEmpty()); // [] passes but [1, 2, 3] does not
Assert::that($something)->is(Type::json()); // valid json string
// "Not's"
Assert::that($something)->isNot(Type::bool());
Assert::that($something)->isNot(Type::int());
Assert::that($something)->isNot(Type::float());
Assert::that($something)->isNot(Type::numeric());
Assert::that($something)->isNot(Type::string());
Assert::that($something)->isNot(Type::callable());
Assert::that($something)->isNot(Type::iterable());
Assert::that($something)->isNot(Type::countable());
Assert::that($something)->isNot(Type::object());
Assert::that($something)->isNot(Type::resource());
Assert::that($something)->isNot(Type::array());
Assert::that($something)->isNot(Type::arrayList());
Assert::that($something)->isNot(Type::arrayAssoc());
Assert::that($something)->isNot(Type::arrayEmpty());
Assert::that($something)->isNot(Type::json());
use Zenstruck\Assert;
// the following can all be used within a single PHPUnit test
// fails if exception not thrown
// fails if exception is thrown but not instance of \RuntimeException
Assert::that(fn() => $code->thatThrowsException())->throws(\RuntimeException::class);
// fails if exception not thrown
// fails if exception is thrown but not instance of \RuntimeException
// fails if exception is thrown but exception message doesn't contain "some message"
Assert::that(fn() => $code->thatThrowsException())->throws(\RuntimeException::class, 'some message');
// a callable can be used for the expected exception. The first parameter's type
// hint is used as the expected exception and the callable is executed with the
// caught exception
//
// fails if exception not thrown
// fails if exception is thrown but not instance of CustomException
Assert::that(fn() => $code->thatThrowsException())->throws(
function(CustomException $e) use ($database) {
// make assertions on the exception
Assert::that($e->getMessage())->contains('some message');
Assert::that($e->getSomeValue())->is('value');
// make side effect assertions
Assert::true($database->userTableEmpty(), 'The user table is not empty');
// If using within the context of a PHPUnit test, you can use standard PHPUnit assertions
$this->assertStringContainsString('some message', $e->getMessage());
$this->assertSame('value', $e->getSomeValue());
$this->assertTrue($database->userTableEmpty());
}
);
use Zenstruck\Assert;
// chain expectations on the same "value"
Assert::that(['foo', 'bar'])
->hasCount(2)
->contains('foo')
->contains('bar')
->doesNotContain('baz')
;
// start an additional expectation without breaking
Assert::that(['foo', 'bar'])
->hasCount(2)
->contains('foo')
->and('foobar') // start a new expectation with "foobar" as the new expectation value
->contains('bar')
;
use Zenstruck\Assert\AssertionFailed;
// The `throw()` named constructor creates the exception and immediately throws it
AssertionFailed::throw('Some message');
// a second "context" parameter can be used as sprintf values for the message
AssertionFailed::throw('Expected "%s" but got "%s"', ['value 1', 'value 2']); // Expected "value 1" but got "value 2"
// when an associated array passed as the context parameter, the message is constructed
// with a simple template system
AssertionFailed::throw('Expected "{expected}" but got "{actual}"', [ // Expected "value 1" but got "value 2"
'expected' => 'value 1',
'actual' => 'value 2',
]);
use Zenstruck\Assert;
use Zenstruck\Assert\AssertionFailed;
class StringContains
{
public function __construct(private string $haystack, private string $needle) {}
public function __invoke(): void
{
if (!str_contains($this->haystack, $this->needle)) {
AssertionFailed::throw(
'Expected string "{haystack}" to contain "{needle}" but it did not.',
get_object_vars($this)
]);
}
}
}
// use the above assertion:
// passes
Assert::run(new StringContains('quick brown fox', 'fox'));
// fails
Assert::run(new StringContains('quick brown fox', 'dog'));
use Zenstruck\Assert;
use Zenstruck\Assert\AssertionFailed;
use Zenstruck\Assert\Assertion\Negatable;
class StringContains implements Negatable
{
public function __construct(private string $haystack, private string $needle) {}
public function __invoke(): void
{
if (!str_contains($this->haystack, $this->needle)) {
AssertionFailed::throw(
'Expected string "{haystack}" to contain "{needle}" but it did not.',
get_object_vars($this)
]);
}
}
public function notFailure(): AssertionFailed
{
return new AssertionFailed(
'Expected string "{haystack}" to not contain "{needle}" but it did.',
get_object_vars($this)
);
}
}
// use the above assertion:
// fails
Assert::not(new StringContains('quick brown fox', 'fox'));
// passes
Assert::not(new StringContains('quick brown fox', 'dog'));
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.