PHPackages                             joebengalen/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. joebengalen/assert

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

joebengalen/assert
==================

Assertion library

1.0.0(10y ago)146MITPHPPHP &gt;=5.4

Since Aug 28Pushed 10y ago1 watchersCompare

[ Source](https://github.com/JoeBengalen/Assert)[ Packagist](https://packagist.org/packages/joebengalen/assert)[ Docs](https://github.com/JoeBengalen/Assert)[ RSS](/packages/joebengalen-assert/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)DependenciesVersions (4)Used By (0)

Assert
======

[](#assert)

[![Build Status](https://camo.githubusercontent.com/ac29ca919f90ab3c1c7e1fcb6d31eca0a3ccf88eba2c46a5c3b20093b35015fb/68747470733a2f2f7472617669732d63692e6f72672f4a6f6542656e67616c656e2f4173736572742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/JoeBengalen/Assert)[![Coverage Status](https://camo.githubusercontent.com/546ce7fb1d2a675d0532d927b2871e62204fe33aa7c0ec1ff61bf33c287e8485/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f4a6f6542656e67616c656e2f4173736572742f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/JoeBengalen/Assert?branch=master)[![SensioLabsInsight](https://camo.githubusercontent.com/655ca04021e0c9da65b077b359705ab4bb0a6920c10cdfbe182ca397df1ac1d7/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f35363362306634352d623839352d343836622d623530342d3236353862316533363732632f6d696e692e706e67)](https://insight.sensiolabs.com/projects/563b0f45-b895-486b-b504-2658b1e3672c)[![Total Downloads](https://camo.githubusercontent.com/24e2bfa321d2c7d2f89852e2e81f986abd9fc3331b973b87a8402318c404cda0/68747470733a2f2f706f7365722e707567782e6f72672f6a6f6562656e67616c656e2f6173736572742f646f776e6c6f616473)](https://packagist.org/packages/joebengalen/assert)[![License](https://camo.githubusercontent.com/ef1c8312537b2ba20788cf0c8f72262b11b3a590cf9e9fa0f1df042cef279336/68747470733a2f2f706f7365722e707567782e6f72672f6a6f6562656e67616c656e2f6173736572742f6c6963656e7365)](LICENSE.md)

As we all know when using an existing library we wish some things would be a little different. To make sure I can use assertions exactly how I feel is best, I created my own implementation.

The assert methods are meant to be used to check whether incoming arguments are what you expect them to be. If the variable does not meet the assertion an `InvalidArgumentException` is thrown.

I needed all available assertions to be actual methods instead of virtual ones in the class PHPDoc, because virtual ones do not work properly with static methods in netbeans.

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

[](#installation)

Via [Composer](https://getcomposer.org)

```
$ composer require joebengalen/assert
```

Usage
-----

[](#usage)

```
use JoeBengalen\Assert\Assert;

/**
 * @param int  $arg1
 * @param bool $arg2
 */
function foo($arg1, $arg2)
{
    // Make sure the arguments are indeed what they should be
    Assert::isInteger($arg1);
    Assert::isBoolean($arg2);

    // Actual code ...
}

// This will be fine ...
foo(12, true);

// ... but this will throw an InvalidArgumentException
foo(2, 4);
```

### Custom exception messages

[](#custom-exception-messages)

Sometimes you may want to throw the exception with a different message. This can be done by passing a second string argument into the assertion method.

The error messages are run trough `sprintf()`, where the passed variable, transformed into a useful string, will be available.

```
Assert::isBoolean(3, 'Custom message: got %s, but expected boolean');
// Throws: "Custom message: got integer, but expected boolean"
```

Some assertion methods take additional arguments which are needed for the assertion. The custom message shall always be the latest argument. Additional arguments will also be passed into `sprintf()`, in the order of the arguments. This means the value to assert will always be the first string passed into `sprintf()`.

```
Assert::isInstanceOf(new Foo, 'Bar', 'Custom message: got %s, but expected instance of %s');
// Throws: "Custom message: got Foo, but expected instance of Bar"
```

If you want to use the arguments in a different order in the message you can swap them by referring to their number.

```
Assert::isInstanceOf(new Foo, 'Bar', 'Custom message: expected instance of %2$s, but got %s');
// Throws: "Custom message: expected instance of Bar, but got Foo"
```

For more information on swapping the arguments checkout the php documentation of [sprintf()](http://php.net/manual/en/function.sprintf.php).

Change log
----------

[](#change-log)

Please see the [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Testing
-------

[](#testing)

This project uses [PHPUnit](https://phpunit.de) for testing. PHPUnit is not included as requirement, so to run the tests phpunit has to be manually installed. The reason for this is that I prefer having phpunit installed globally.

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

3916d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/47732f0e5bc5d319cc52db1313312daa37279fe5fa16abf5bdd5a77e65347b1d?d=identicon)[JoeBengalen](/maintainers/JoeBengalen)

---

Top Contributors

[![JoeBengalen](https://avatars.githubusercontent.com/u/11173689?v=4)](https://github.com/JoeBengalen "JoeBengalen (32 commits)")

---

Tags

assertassertion

### Embed Badge

![Health badge](/badges/joebengalen-assert/health.svg)

```
[![Health](https://phpackages.com/badges/joebengalen-assert/health.svg)](https://phpackages.com/packages/joebengalen-assert)
```

###  Alternatives

[beberlei/assert

Thin assertion library for input validation in business models.

2.4k96.9M570](/packages/beberlei-assert)[spatie/phpunit-snapshot-assertions

Snapshot testing with PHPUnit

69617.9M510](/packages/spatie-phpunit-snapshot-assertions)[zenstruck/assert

Standalone, lightweight, framework agnostic, test assertion library.

8214.9M8](/packages/zenstruck-assert)[elliotchance/concise

Concise is test framework for using plain English and minimal code, built on PHPUnit.

45223.8k4](/packages/elliotchance-concise)[fr3d/swagger-assertions

Test your API requests and responses against your swagger definition

138850.9k5](/packages/fr3d-swagger-assertions)[respect/assertion

The power of Respect\\Validation into an assertion library

2828.0k](/packages/respect-assertion)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
