PHPackages                             tebru/assert - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. tebru/assert

ActiveLibrary[Testing &amp; Quality](/categories/testing)

tebru/assert
============

Simple function to assert conditions

v0.2.0(10y ago)258.9k↑81.3%13MITPHPPHP &gt;= 5.3

Since Feb 1Pushed 10y ago2 watchersCompare

[ Source](https://github.com/tebru/assert)[ Packagist](https://packagist.org/packages/tebru/assert)[ RSS](/packages/tebru-assert/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (2)Versions (3)Used By (3)

[![Build Status](https://camo.githubusercontent.com/9693ebc826d2c3dd9aa348750ab8bd810223ab8a75c9d6e0b0fe7cf5ec9cbabc/68747470733a2f2f7472617669732d63692e6f72672f74656272752f6173736572742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/tebru/assert)[![Coverage Status](https://camo.githubusercontent.com/6dc59aa7ca63d9c74c8ced9a8a0daab88c249b13fa8df1fbccbfbb29e7398d54/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f74656272752f6173736572742f62616467652e7376673f6272616e63683d6d617374657226736572766963653d676974687562)](https://coveralls.io/github/tebru/assert?branch=master)

Assert
======

[](#assert)

This is a simple library that contains a function to asset conditions.

Installation
------------

[](#installation)

```
composer require tebru/assert:~0.2

```

Usage
-----

[](#usage)

Using the generic assert functions, you must pass a condition as the first argument.

### Simple

[](#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

[](#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

[](#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

[](#advanced-usage)

Starting with php 5.6, you can import functions

```
