This is a simple library that contains a function to asset conditions.
composer require tebru/assert:~0.2
Using the generic assert functions, you must pass a condition as the first argument.
The easiest and most generic way to use this library is through assertThat()
. This will throw a LogicException
with a default message that is easily customized.
Tebru\assertThat(false);
Tebru\assertThat(1 === 2);
Tebru\assertThat(false, 'My %s %s', 'test', 'message');
If you need to throw specific exceptions, that can be done through assert()
. The method defaults to throwing a LogicException
.
Tebru\assert(false);
Tebru\assert(1 === 2);
Tebru\assert(false, new \InvalidArgumentException('My custom error message'));
There are a number of additional functions that assert specific conditions. Each one accepts a message and a variable number of arguments to be used with an sprintf function as the final arguments. A list is provided below with the method definition:
- assertEqual($expected, $actual, $message = null, $_ = null)
- assertNotEqual($expected, $actual, $message = null, $_ = null)
- assertSame($expected, $actual, $message = null, $_ = null)
- assertNotSame($expected, $actual, $message = null, $_ = null)
- assertTrue($value, $message = null, $_ = null)
- assertFalse($value, $message = null, $_ = null)
- assertNull($value, $message = null, $_ = null)
- assertNotNull($value, $message = null, $_ = null)
- assertArrayKeyExists($key, array $search, $message = null, $_ = null)
- assertArrayKeyNotExists($key, array $search, $message = null, $_ = null)
- assertCount($expected, $countable, $message = null, $_ = null)
Starting with php 5.6, you can import functions
<?php
namespace Foo;
use function Tebru\assert;
assert(false);