Use Hamcrest Matchers in PHP
masterHamcrest provides two ways to perform assertions: using the MatcherAssert class directly or using global proxy functions.
Method 1: Using MatcherAssert
This is the most explicit way to use the library.
\Hamcrest\MatcherAssert::assertThat('a', \Hamcrest\Matchers::equalToIgnoringCase('A'));Method 2: Using Global Proxy Functions
For a more concise syntax, you can use global functions. Note: These are not autoloaded by default and must be registered manually.
// 1. Register the global functions
\Hamcrest\Util::registerGlobalFunctions();
// 2. Use the proxy functions
// With an identifier (message)
assertThat("result should be true", $result, equalTo(true));
// Without an identifier
assertThat($result, equalTo(true));
// Evaluate a boolean expression
assertThat($result === true);
// Using syntactic sugar 'is()'
assertThat(true, is(true));<?php
\Hamcrest\Util::registerGlobalFunctions();
// Example usage
assertThat("result should be true", $result, equalTo(true));
assertThat($result, equalTo(true));
assertThat($result === true);
assertThat(true, is(true));