Skip to content

PHPUnit Constraints

In PHPUnit, assertThat is a more flexible assertion method that allows you to use constraint objects to define complex or custom assertions.

The signature looks like:

$this->assertThat(mixed $value, PHPUnit\Framework\Constraint $constraint, string $message = '')

โœ… Example: Using a built-in constraint

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Constraint\StringContains;
class MyTest extends TestCase
{
public function testUsingAssertThat()
{
$value = "Hello World";
// Equivalent to: $this->assertStringContainsString("Hello", $value);
$this->assertThat($value, new StringContains("Hello"));
}
}

โœ… Common constraints you can use

use PHPUnit\Framework\Constraint\LogicalAnd;
use PHPUnit\Framework\Constraint\StringContains;
$this->assertThat(
"Hello World",
new LogicalAnd(
new StringContains("Hello"),
new StringContains("World")
)
);

๐Ÿงฉ Custom constraint example

You can also make your own constraint by extending PHPUnit\Framework\Constraint\Constraint:

use PHPUnit\Framework\Constraint\Constraint;
class IsEven extends Constraint
{
public function matches($other): bool
{
return $other % 2 === 0;
}
public function toString(): string
{
return 'is an even number';
}
}

Usage:

$this->assertThat(4, new IsEven());

Constraints

๐Ÿ”ค Type and Equality Constraints

Constraint ClassPurposeEquivalent Assertion
IsType('type')Checks variable type (string, int, etc.)assertIsString(), assertIsInt(), etc.
IsEqual($value)Strict equality with valueassertEquals()
IsIdentical($value)Identity (===) checkassertSame()
IsNotEqual($value)InequalityassertNotEquals()
IsNotIdentical($value)Not identical (!==)assertNotSame()

๐Ÿ” String Constraints

Constraint ClassPurposeEquivalent Assertion
StringContains($needle)Checks substring presenceassertStringContainsString()
StringStartsWith($prefix)Checks if string starts with prefixassertStringStartsWith()
StringEndsWith($suffix)Checks if string ends with suffixassertStringEndsWith()
MatchesRegularExpression($pattern)Regex matchassertMatchesRegularExpression()

๐Ÿ“ Comparison Constraints

Constraint ClassPurposeEquivalent Assertion
GreaterThan($value)Greater thanassertGreaterThan()
GreaterThanOrEqual($value)Greater than or equalassertGreaterThanOrEqual()
LessThan($value)Less thanassertLessThan()
LessThanOrEqual($value)Less than or equalassertLessThanOrEqual()

๐Ÿ“š Array and Count Constraints

Constraint ClassPurposeEquivalent Assertion
Count($expectedCount)Checks countable has expected sizeassertCount()
ArrayHasKey($key)Checks if array has keyassertArrayHasKey()
ArraySubset($subset)Checks array contains a subset(deprecated)

โš™๏ธ Object & Class Constraints

Constraint ClassPurposeEquivalent Assertion
IsInstanceOf($className)Checks object instanceassertInstanceOf()
ObjectHasAttribute($attrName)Checks object has attributeassertObjectHasAttribute()
ClassHasAttribute($attrName)Checks class has attributeassertClassHasAttribute()
ClassHasStaticAttribute($attrName)Checks static attribute existsassertClassHasStaticAttribute()

๐Ÿงช Boolean and Null Constraints

Constraint ClassPurposeEquivalent Assertion
IsTrue()Asserts trueassertTrue()
IsFalse()Asserts falseassertFalse()
IsNull()Asserts nullassertNull()
IsNotNull()Asserts not nullassertNotNull()
IsEmpty()Asserts empty (string, array, etc.)assertEmpty()
IsNotEmpty()Asserts not emptyassertNotEmpty()

๐Ÿงฎ File System Constraints

Constraint ClassPurposeEquivalent Assertion
FileExists()Checks file existsassertFileExists()
FileDoesNotExist()Checks file does not existassertFileDoesNotExist()
IsReadable()File is readableassertIsReadable()
IsWritable()File is writableassertIsWritable()

๐Ÿงฉ Logical Constraints (combinators)

Constraint ClassPurposeNotes
LogicalAnd($c1, $c2)Passes if both constraints passCombines constraints
LogicalOr($c1, $c2)Passes if either constraint passes
LogicalNot($c)Inverts constraint result
LogicalXor($c1, $c2)One passes, one fails