PHPackages                             dgame/php-expectation - 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. dgame/php-expectation

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

dgame/php-expectation
=====================

php ensurance

v0.1.0(9y ago)164.4k↑63.9%MITPHPPHP &gt;=7.0

Since May 2Pushed 6y ago1 watchersCompare

[ Source](https://github.com/Dgame/php-expectation)[ Packagist](https://packagist.org/packages/dgame/php-expectation)[ Docs](https://github.com/php-expectation)[ RSS](/packages/dgame-php-expectation/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

php-expectation
===============

[](#php-expectation)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/01a63aa1966553fb10950acdf8a96e12cf40c7e92e0d73bfa41873a9b2ac7409/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4467616d652f7068702d6578706563746174696f6e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Dgame/php-expectation/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/267b2f097babdc9376a3d42628ede81da08f1ae6f5379b9b396c4280e70cb462/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4467616d652f7068702d6578706563746174696f6e2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Dgame/php-expectation/?branch=master)[![Build Status](https://camo.githubusercontent.com/9e442ef1cd143007e72de754ec4ff01c6f928c98648d2cdc7e65ff878e3be29d/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4467616d652f7068702d6578706563746174696f6e2f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Dgame/php-expectation/build-status/master)[![SensioLabsInsight](https://camo.githubusercontent.com/56170678d52a94fc149373c4bc0bce83ca05dbaa969203653b90d84e9aec9f9b/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f33353637316336372d653233652d346135382d386438312d6463633437656438376235622f6d696e692e706e67)](https://insight.sensiolabs.com/projects/35671c67-e23e-4a58-8d81-dcc47ed87b5b)[![StyleCI](https://camo.githubusercontent.com/9bf51bfed4e2d7cbea6fbe5e8bce31792349f708c97a7989e436ca566d7b37bf/68747470733a2f2f7374796c6563692e696f2f7265706f732f38393734323830362f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/89742806)[![Build Status](https://camo.githubusercontent.com/67ba333dc8849d1bcf77c38cc3a6ff22882bfec087df3c0f0ff35f9f89ae2ac9/68747470733a2f2f7472617669732d63692e6f72672f4467616d652f7068702d6578706563746174696f6e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Dgame/php-expectation)

---

Bind expectations to your values and offer default values if the expectation don't apply.

isInt
-----

[](#isint)

```
$this->assertEquals(42, expect(42)->isInt()->else(23));
$this->assertEquals(1337, expect('foo')->isInt()->else(1337));
```

isFloat
-------

[](#isfloat)

```
$this->assertEquals(4.2, expect(4.2)->isFloat()->else(2.3));
$this->assertEquals(13.37, expect('foo')->isFloat()->else(13.37));
```

isScalar
--------

[](#isscalar)

```
$this->assertEquals('bar', expect('bar')->isScalar()->else('foo'));
$this->assertEquals(42, expect(42)->isScalar()->else('bar'));
```

isNumeric
---------

[](#isnumeric)

```
$this->assertEquals(42, expect(42)->isNumeric()->else('foo'));
$this->assertEquals('1337', expect('1337')->isNumeric()->else('bar'));
```

isString
--------

[](#isstring)

```
$this->assertEquals('foo', expect('foo')->isString()->else('bar'));
$this->assertEquals('bar', expect(42)->isString()->else('bar'));
```

isBool
------

[](#isbool)

```
$this->assertTrue(expect(true)->isBool()->else(false));
$this->assertFalse(expect(1)->isBool()->else(false));
```

isArray
-------

[](#isarray)

```
$this->assertEquals([1, 2, 3], expect([1, 2, 3])->isArray()->else([]));
$this->assertEquals([2], expect('abc')->isArray()->else([2]));
$this->assertEquals([], expect(null)->isArray()->else([]));
```

isObject
--------

[](#isobject)

```
$this->assertEquals($this, expect($this)->isObject()->else(null));
$this->assertEquals(null, expect(null)->isObject()->else(null));
$this->assertEquals(23, expect(42)->isObject()->else(23));
```

isInstanceOf
------------

[](#isinstanceof)

```
$this->assertEquals($this, expect($this)->isInstanceOf(self::class)->else(null));
$this->assertEquals(null, expect(new Exception())->isInstanceOf(self::class)->else(null));
$this->assertEquals(null, expect(null)->isInstanceOf(self::class)->else(null));
```

isCallable
----------

[](#iscallable)

```
$this->assertEquals('trim', expect('trim')->isCallable()->else(null));
$this->assertEquals(null, expect('empty')->isCallable()->else(null));
```

isDir
-----

[](#isdir)

```
$this->assertEquals(__DIR__, expect(__DIR__)->isDir()->else('src'));
$this->assertEquals('src', expect('test')->isDir()->else('src'));
```

isFile
------

[](#isfile)

```
$this->assertEquals(__FILE__, expect(__FILE__)->isFile()->else(null));
$this->assertEquals(null, expect('src')->isFile()->else(null));
```

isEmpty
-------

[](#isempty)

```
$this->assertEmpty(expect(null)->isEmpty()->else('abc'));
$this->assertNull(expect('abc')->isEmpty()->else(null));
```

isNotEmpty
----------

[](#isnotempty)

```
$this->assertNotEmpty(expect(null)->isNotEmpty()->else('abc'));
$this->assertNotNull(expect('abc')->isNotEmpty()->else(null));
```

isNull
------

[](#isnull)

```
$this->assertNull(expect(null)->isNull()->else('abc'));
$this->assertEquals('foobar', expect('abc')->isNull()->else('foobar'));
```

isNotNull
---------

[](#isnotnull)

```
$this->assertNotNull(expect(null)->isNotNull()->else('abc'));
$this->assertNull(expect(null)->isNotNull()->else(null));
```

isTrue
------

[](#istrue)

```
$this->assertTrue(expect(false)->isTrue()->else(true));
$this->assertTrue(expect(true)->isTrue()->else(false));
$this->assertFalse(expect(false)->isTrue()->else(false));
```

isFalse
-------

[](#isfalse)

```
$this->assertFalse(expect(false)->isFalse()->else(true));
$this->assertFalse(expect(true)->isFalse()->else(false));
$this->assertTrue(expect(true)->isFalse()->else(true));
```

isEqual
-------

[](#isequal)

```
$this->assertEquals('abc', expect('abc')->isEqual('abc')->else('foo'));
$this->assertEquals('42', expect('42')->isEqual('42')->else(null));
$this->assertEquals(42, expect('42')->isEqual(42)->else(null));
```

isNotEqual
----------

[](#isnotequal)

```
$this->assertEquals('foo', expect('abc')->isNotEqual('abc')->else('foo'));
$this->assertEquals(null, expect('42')->isNotEqual('42')->else(null));
$this->assertEquals(42, expect('42')->isEqual(42)->else(null));
```

isIdenticalTo
-------------

[](#isidenticalto)

```
$this->assertEquals('abc', expect('abc')->isIdenticalTo('abc')->else('foo'));
$this->assertEquals('42', expect('42')->isIdenticalTo('42')->else(null));
$this->assertEquals(null, expect('42')->isIdenticalTo(42)->else(null));
$this->assertEquals(42, expect(42)->isIdenticalTo(42)->else(null));
```

isNotIdenticalTo
----------------

[](#isnotidenticalto)

```
$this->assertEquals('foo', expect('abc')->isNotIdenticalTo('abc')->else('foo'));
$this->assertEquals(null, expect('42')->isNotIdenticalTo('42')->else(null));
$this->assertEquals('42', expect('42')->isNotIdenticalTo(42)->else(null));
$this->assertEquals(null, expect(42)->isNotIdenticalTo(42)->else(null));
```

isBetween
---------

[](#isbetween)

```
$this->assertEquals(7, expect(7)->isBetween(0, 8)->else(null));
$this->assertEquals(7, expect(7)->isBetween(0, 7)->else(null));
$this->assertEquals(0, expect(0)->isBetween(0, 7)->else(null));
$this->assertEquals('nope', expect(12)->isBetween(15, 20)->else('nope'));
```

hasLength
---------

[](#haslength)

```
$this->assertEquals('foo', expect('foo')->hasLength(3)->else('aye'));
$this->assertEquals('nope', expect('foobar')->hasLength(4)->else('nope'));

$this->assertEquals([1, 2, 3], expect([1, 2, 3])->hasLength(3)->else([]));
$this->assertEquals([42, 23], expect([])->hasLength(2)->else([42, 23]));
```

isIn
----

[](#isin)

```
$this->assertEquals(42, expect(42)->isIn([1, 2, 23, 36, 42, 44, 48])->else(null));
$this->assertEquals(42, expect(42)->isIn([1, 2, 23, 36, 42])->else(null));
$this->assertEquals(null, expect(42)->isIn([1, 2, 23, 36])->else(null));
```

isKeyOf
-------

[](#iskeyof)

```
$this->assertEquals('foo', expect('foo')->isKeyOf(['a' => 23, 'foo' => 42])->else(null));
$this->assertEquals(null, expect('foo')->isKeyOf(['a' => 23])->else(null));
```

isBelow
-------

[](#isbelow)

```
$this->assertEquals(3, expect(3)->isBelow(4)->else(42));
$this->assertEquals(42, expect(6)->isBelow(4)->else(42));
```

isAbove
-------

[](#isabove)

```
$this->assertEquals(42, expect(3)->isAbove(4)->else(42));
$this->assertEquals(6, expect(6)->isAbove(4)->else(42));
```

isBelowOrEqual
--------------

[](#isbeloworequal)

```
$this->assertEquals(23, expect(23)->isBelowOrEqual(23)->else(null));
$this->assertEquals(23, expect(23)->isBelowOrEqual(42)->else(null));
$this->assertEquals(null, expect(23)->isBelowOrEqual(22)->else(null));
```

isAboveOrEqual
--------------

[](#isaboveorequal)

```
$this->assertEquals(42, expect(42)->isAboveOrEqual(42)->else(null));
$this->assertEquals(42, expect(42)->isAboveOrEqual(23)->else(null));
$this->assertEquals(null, expect(42)->isAboveOrEqual(256)->else(null));
```

isPositive
----------

[](#ispositive)

```
$this->assertEquals(42, expect(-1)->isPositive()->else(42));
$this->assertEquals(0, expect(0)->isPositive()->else(42));
```

isNegative
----------

[](#isnegative)

```
$this->assertEquals(-1, expect(-1)->isNegative()->else(42));
$this->assertEquals(42, expect(0)->isNegative()->else(42));
```

isEven
------

[](#iseven)

```
$this->assertEquals(42, expect(42)->isEven()->else(23));
$this->assertEquals(42, expect(23)->isEven()->else(42));
```

isOdd
-----

[](#isodd)

```
$this->assertEquals(23, expect(42)->isOdd()->else(23));
$this->assertEquals(23, expect(23)->isOdd()->else(42));
```

match
-----

[](#match)

```
$this->assertEquals('abc', expect('abc')->match('/a*b{1}c?d?/')->else('foo'));
$this->assertEquals('foo', expect('ac')->match('/a*b{1}c?d?/')->else('foo'));
```

then
----

[](#then)

```
$this->assertEquals('foo', expect(42)->isEven()->then('foo'));
$this->assertEquals(42, expect(42)->isOdd()->then('foo'));
```

---

You don't like global functions? Use a static method:

```
$this->assertEquals(42, Expect::that(42)->isInt()->else(null));
```

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

3304d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2a9fa98c1a3e70a521430fc2fba266657b2c981b5d8a36bf236fad01f9846dcd?d=identicon)[Dgame](/maintainers/Dgame)

---

Top Contributors

[![Dgame](https://avatars.githubusercontent.com/u/2406877?v=4)](https://github.com/Dgame "Dgame (24 commits)")

---

Tags

assertionEnforcementEnsuranceDesign by contract

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/dgame-php-expectation/health.svg)

```
[![Health](https://phpackages.com/badges/dgame-php-expectation/health.svg)](https://phpackages.com/packages/dgame-php-expectation)
```

###  Alternatives

[beberlei/assert

Thin assertion library for input validation in business models.

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

Standalone, lightweight, framework agnostic, test assertion library.

8214.9M8](/packages/zenstruck-assert)[fr3d/swagger-assertions

Test your API requests and responses against your swagger definition

138850.9k5](/packages/fr3d-swagger-assertions)[elliotchance/concise

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

45223.8k4](/packages/elliotchance-concise)[matthiasnoback/phpunit-asynchronicity

Library for asserting things that happen asynchronously with PHPUnit

36229.0k7](/packages/matthiasnoback-phpunit-asynchronicity)[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)
