hamcrest/hamcrest-php's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.
Hamcrest-PHP is a library that provides a matching mechanism for various use cases. Initially developed in Java, Hamcrest has been brought to the PHP world through this library as the PHP port of Hamcrest matchers. It is a powerful tool for carrying out assertions in testing scenarios or validations by defining expectations in a readable and reusable manner. While most of the Java features have been translated to PHP, there are some exceptions due to PHP language barriers. It allows dynamic typing for its inputs except for matchers that semantically require a specific type, such as 'stringContains()' and 'greaterThan()'.
To utilize the hamcrest-php library, you have to include the
Hamcrest namespace in your PHP file. A code example summarizing the usage can be found below:
require_once 'vendor/hamcrest/hamcrest-php/hamcrest/Hamcrest.php'; use Hamcrest\Matchers as Hamcrest; //Assuming we have a variable '$value' to be tested $value = 'A'; //Matching the $value to be equal to 'A' in a case-insensitive manner Hamcrest_MatcherAssert::assertThat($value, Hamcrest_Matchers::equalToIgnoringCase('A'));
In another code example, you can also use the global proxy-functions. However, they are not autoloaded by default and you need to load them manually.
\Hamcrest\Util::registerGlobalFunctions(); $result = true; //With an identifier assertThat("Result should be true", $result, equalTo(true)); //Without an identifier assertThat($result, equalTo(true)); //Evaluating a boolean expression assertThat($result === true); //With syntactic sugar is() assertThat(true, is(true));
The Hamcrest-PHP library provides several matchers including but not limited to: Array, Collection, Object, Numbers, Type checking and XML.
Detailed documentation of hamcrest-php, which includes details on Available Matchers and their usage, can be found at the Hamcrest site.