Comparison Assertions
Comparison assertions validate equality and identity between values.
Available Assertions
Section titled “Available Assertions”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');same()
Section titled “same()”Assert identity using strict comparison (===).
Assertion::same($actual, $expected);Assertion::same(123, 123); // PassesAssertion::same(123, '123'); // Fails (different types)notEq()
Section titled “notEq()”Assert values are NOT equal (loose comparison).
Assertion::notEq($actual, $unwanted);Assertion::notEq($status, 'deleted', 'Status cannot be deleted');notSame()
Section titled “notSame()”Assert values are NOT identical (strict comparison).
Assertion::notSame($actual, $unwanted);Assertion::notSame($newPassword, $oldPassword, 'New password must be different');Loose vs Strict Comparison
Section titled “Loose vs Strict Comparison”Loose Comparison (eq/notEq)
Section titled “Loose Comparison (eq/notEq)”Uses == operator with type coercion:
Assertion::eq(123, '123'); // PassAssertion::eq(1, true); // PassAssertion::eq(0, false); // PassStrict Comparison (same/notSame)
Section titled “Strict Comparison (same/notSame)”Uses === operator without type coercion:
Assertion::same(123, '123'); // FailAssertion::same(1, true); // FailAssertion::same(0, false); // FailChaining Comparison Assertions
Section titled “Chaining Comparison Assertions”use Cline\Assert\Assert;
Assert::that($result) ->integer() ->same(42);
Assert::that($status) ->string() ->notEq('deleted') ->notEq('archived');Common Patterns
Section titled “Common Patterns”Expected Value Validation
Section titled “Expected Value Validation”Assert::that($response->status) ->integer() ->same(200, 'Expected HTTP 200 status');State Validation
Section titled “State Validation”Assert::that($order->status) ->string() ->notEq('cancelled', 'Order is cancelled') ->notEq('refunded', 'Order is refunded');Preventing Duplicate Values
Section titled “Preventing Duplicate Values”Assert::that($newEmail) ->email() ->notSame($currentEmail, 'New email must be different');When to Use Which
Section titled “When to Use Which”Use eq() when:
Section titled “Use eq() when:”- Comparing form input (strings) with expected values
- Type flexibility is acceptable
Use same() when:
Section titled “Use same() when:”- Type safety is important
- Comparing computed values
- Validating configuration values
Use notSame() when:
Section titled “Use notSame() when:”- Ensuring different passwords or tokens
- Type-safe blacklisting
Best Practices
Section titled “Best Practices”Prefer Strict Comparison
Section titled “Prefer Strict Comparison”// Loose comparison can hide bugsAssertion::eq($count, '0');
// Strict comparison catches type issuesAssertion::same($count, 0);Combine with Type Checks
Section titled “Combine with Type Checks”Assert::that($value) ->integer() ->same(42);Next Steps
Section titled “Next Steps”- Numeric Assertions - Range comparisons
- Type Assertions - Type validation
- Array Assertions - Array comparisons