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

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

sci/assert
==========

Extensible lightweight assertion library.

1.0.2(10y ago)0393[1 PRs](https://github.com/DrSchimke/assert/pulls)1MITPHPPHP &gt;=5.4.0

Since Jun 26Pushed 8y ago2 watchersCompare

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

READMEChangelogDependencies (3)Versions (6)Used By (1)

Extensible assertion library
============================

[](#extensible-assertion-library)

[![Build Status](https://camo.githubusercontent.com/d6de088cd6044b3741be724d0542e6028fb12f218dcfbfbee7b5a27af070ea37/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f4472536368696d6b652f6173736572742e706e67)](http://travis-ci.org/DrSchimke/assert)[![Build Status](https://camo.githubusercontent.com/f994dfe373da8b6685b07f845ac3682f9e8095ac41ff4310ad67b3958c53b476/68747470733a2f2f7374796c6563692e696f2f7265706f732f33363837373037342f736869656c64)](https://styleci.io/repos/36877074)

PHP library heavily inspired by [beberlei/assert](https://github.com/beberlei/assert).

The purpose is a lightweight php library mainly for validating method parameters. The library's API is fluent [DSL](https://en.wikipedia.org/wiki/Domain-specific_language) like.

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

[](#installation)

Using [composer](https://getcomposer.org/download/):

```
composer require sci/assert dev-master

```

Motivation
----------

[](#motivation)

Just as an example, php is not able to typehint array-ish arguments, i.e. array or \\Traversable:

```
function foobar($values)
{
    foreach ($values as $value) {
        // ...
    }
}

$a = array(1, 2, 3);
$b = new \ArrayIterator($a);
$c = 'not that iterable';

foobar($a); // fine
foobar($b); // fine, too
foobar($c); // Invalid argument supplied for foreach()
```

Ignoring this and relying on @param-Annotations (`@param int[]` or `@param array|\Traversable`) is quite bad.

Having an explicit guard is verbose and needs a separate unit test to achieve code coverage:

```
function foobar($values)
{
    if (!is_array($values) && !$values instanceof \Traversable) {
        throw new \InvalidArgumentException(/* ... */);
    }

    // ...
}
```

The solution may be this:

```
use Sci\Assert\Assert;

function foobar($values)
{
    Assert::that($values)->isTraversable();

    // ...
}
```

Examples
--------

[](#examples)

```
use Sci\Assert\Assert;

// be it a string, matching a regular expression
Assert::that($value)->isString()->machesRegExp('/[A-Z][a-z+]/');

// be it a collection of strings, matching a regular expression
Assert::that($values)->all()->isString()->machesRegExp('/[A-Z][a-z+]/');

// be it a \DateTime and in year 2015 ('2015-01-01' isInstanceOf('\DateTime')
    ->greaterThanOrEqual(new \DateTime('2015-01-01'))
    ->lessThan(new \DateTime('2016-01-01'));

// ... or, in a different way:
Assert::that($date)
  ->isInstanceOf('\DateTime')
  ->between(new \DateTime('2015-01-01 00:00:00'), new \DateTime('2015-12-31 23:59:59'));

// be it a collection of \DateTime objects, each beeing in future
Assert::that($dates)->all()->isInstanceOf('\DateTime')->greaterThan(new \DateTime('now'));

// be it null, or a collection ...
Assert::that($dates)->nullOr()->isInstanceOf('\DateTime');
```

Extending the library
---------------------

[](#extending-the-library)

The Assert library can be extended by subclassing. An example can be found here: [`NumberAssert`](lib/NumberAssert.php), which adds two changes to the [`Assert`](lib/Assert.php) base-class. First the `Assert::equal()` base method is extendes by a delta/tolerance argument, when used with numeric values: [`NumberAssert::equal()`](lib/NumberAssert.php#L21). Second, a prime number assertion is added: [`NumberAssert::prime()`](lib/NumberAssert.php#L40).

```
use Sci\Assert\NumberAssert;

NumberAssert::that(3.1415)->equal(M_PI, .001);
NumberAssert::that(997)->prime();
```

or, for a better readability:

```
use Sci\Assert\NumberAssert as Assert;

Assert::that(3.1415)->equal(M_PI, .001);
Assert::that(997)->prime();
```

Complete assertion list
-----------------------

[](#complete-assertion-list)

```
use Sci\Assert\Assert;

// base assertions
Assert::that($value)->isString();
Assert::that($value)->isInteger();
Assert::that($value)->isNumeric();
Assert::that($value)->isScalar();
Assert::that($value)->isResource();
Assert::that($value)->isTrue();
Assert::that($value)->isTraversable();
Assert::that($value)->isInstanceOf('\DateTime');

// comparison assertions
Assert::that($value)->equal($valueRepeated);
Assert::that($value)->strictEqual($valueRepeated);

Assert::that($value)->lessThan(10);        // Assert::that($value)->lt(10);
Assert::that($value)->lessThanOrEqual(10); // Assert::that($value)->lte(10);

Assert::that($value)->greaterThan(10);        // Assert::that($value)->gt(10);
Assert::that($value)->greaterThanOrEqual(10); // Assert::that($value)->gte(10);

Assert::that($value)->between(10, 20); // same as Assert::that($value)->gte(10)->lte(20);
Assert::that($value)->between('aaaa', 'bbbbb');

// string assertions
Assert::that($value)->hasMinLength(8);
Assert::that($value)->matches('/^[A-Z][a-z]+$/');

// meta assertions
Assert::that($value)->all()->isString();
Assert::that($value)->nullOr()->isString();
```

```
use Sci\Assert\StringAssert;

StringAssert::that($value)->isIpAddress();
StringAssert::that($value)->isIpAddress(FILTER_FLAG_IPV4);

StringAssert::that($value)->isUrl(FILTER_FLAG_QUERY_REQUIRED | FILTER_FLAG_PATH_REQUIRED);
StringAssert::that($value)->isEmail();
StringAssert::that($value)->isMac();
```

```
use Sci\Assert\FileSystemAssert;

FileSystemAssert::that($filename)->exists();
FileSystemAssert::that($filename)->isFile();
FileSystemAssert::that($filename)->isDir();
FileSystemAssert::that($filename)->isLink();
```

Differences
-----------

[](#differences)

While beberlei's fluent API is function-based (`\Assert\that()`), the API of sci/assert uses a static method (`Assert::that()`).

```
\Assert\that(1)->integer()->min(-10)->max(10);
```

```
use Sci\Assert\Assert;

Assert::that(1)->isInteger()->greaterThanOrEqual(-10)->lessThanOrEqual(10);
Assert::that(1)->isInteger()->gte(-10)->lte(10);
```

Although looking like an unimportant detail, the later solution is easier to extend by subclassing. See [here](#extending-the-library).

License
-------

[](#license)

All contents of this package are licensed under the [MIT license](LICENSE).

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 98.6% 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 ~74 days

Total

4

Last Release

3757d ago

Major Versions

0.0.1-alpha → 1.0.02015-11-21

PHP version history (2 changes)0.0.1-alphaPHP &gt;=5.4.0

1.0.0PHP &gt;=5.5.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/5d3765b40a043c8c7c8ead0ae3cb9691ab6c053d272ec9db1d9bcec0218fb69a?d=identicon)[DrSchimke](/maintainers/DrSchimke)

---

Top Contributors

[![DrSchimke](https://avatars.githubusercontent.com/u/3299009?v=4)](https://github.com/DrSchimke "DrSchimke (70 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[phpspec/prophecy

Highly opinionated mocking framework for PHP 5.3+

8.5k551.7M682](/packages/phpspec-prophecy)[brianium/paratest

Parallel testing for PHP

2.5k118.8M754](/packages/brianium-paratest)[beberlei/assert

Thin assertion library for input validation in business models.

2.4k96.9M570](/packages/beberlei-assert)[mikey179/vfsstream

Virtual file system to mock the real file system in unit tests.

1.4k108.0M2.7k](/packages/mikey179-vfsstream)[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.1k](/packages/orchestra-testbench)[phpspec/phpspec

Specification-oriented BDD framework for PHP 7.1+

1.9k36.7M3.1k](/packages/phpspec-phpspec)

PHPackages © 2026

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