tebru / assert
Simple function to assert conditions
Installs: 55 444
Dependents: 4
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: >= 5.3
Requires (Dev)
- phpunit/phpunit: 4.4.*
- satooshi/php-coveralls: ^0.6.1
This package is auto-updated.
Last update: 2024-12-24 11:56:32 UTC
README
Assert
This is a simple library that contains a function to asset conditions.
Installation
composer require tebru/assert:~0.2
Usage
Using the generic assert functions, you must pass a condition as the first argument.
Simple
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');
Changing exceptions
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'));
Additional Functions
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)
Advanced Usage
Starting with php 5.6, you can import functions
<?php
namespace Foo;
use function Tebru\assert;
assert(false);