Skip to content

Comparison Assertions

Comparison assertions validate equality and identity between values.

Assert equality using loose comparison (==).

use Cline\Assert\Assertions\Assertion;
Assertion::eq($actual, $expected);
Assertion::eq(123, '123'); // Passes (loose comparison)
Assertion::eq($result, 42, 'Result must equal 42');

Assert identity using strict comparison (===).

Assertion::same($actual, $expected);
Assertion::same(123, 123); // Passes
Assertion::same(123, '123'); // Fails (different types)

Assert values are NOT equal (loose comparison).

Assertion::notEq($actual, $unwanted);
Assertion::notEq($status, 'deleted', 'Status cannot be deleted');

Assert values are NOT identical (strict comparison).

Assertion::notSame($actual, $unwanted);
Assertion::notSame($newPassword, $oldPassword, 'New password must be different');

Uses == operator with type coercion:

Assertion::eq(123, '123'); // Pass
Assertion::eq(1, true); // Pass
Assertion::eq(0, false); // Pass

Uses === operator without type coercion:

Assertion::same(123, '123'); // Fail
Assertion::same(1, true); // Fail
Assertion::same(0, false); // Fail
use Cline\Assert\Assert;
Assert::that($result)
->integer()
->same(42);
Assert::that($status)
->string()
->notEq('deleted')
->notEq('archived');
Assert::that($response->status)
->integer()
->same(200, 'Expected HTTP 200 status');
Assert::that($order->status)
->string()
->notEq('cancelled', 'Order is cancelled')
->notEq('refunded', 'Order is refunded');
Assert::that($newEmail)
->email()
->notSame($currentEmail, 'New email must be different');
  • Comparing form input (strings) with expected values
  • Type flexibility is acceptable
  • Type safety is important
  • Comparing computed values
  • Validating configuration values
  • Ensuring different passwords or tokens
  • Type-safe blacklisting
// Loose comparison can hide bugs
Assertion::eq($count, '0');
// Strict comparison catches type issues
Assertion::same($count, 0);
Assert::that($value)
->integer()
->same(42);