A simple collection of custom Hamcrest matchers for use with hamcrest/hamcrest-php
.
Install with composer:
composer require --dev garethellis/hamcrest-matchers
Matches a valid UUID. Example usage:
<?php
/** ... **/
assertThat($uuid, is(aUUID()));
To match an array of UUIDs:
<?php
/** ... **/
assertThat([$uuid1, $uuid2, $uuid3], is(anArrayOfUUIDs()));
This matcher uses the callback matcher (see below) from Nils Luxton.
Matches a string containing HTML. Example usage;
<?php
/** ... **/
assertThat($html, containsHTML());
Matches a valid JSON string. Example usage:
<?php
/** .. **/
assertThat($json, is(validJSON()));
Match the values of an array or Traversable
instance. Note - ignores keys.
assertThat($aTraversableInstance, hasEqualValuesTo($anArray));
Callback matching is achieved thanks to Nils Luxton and his callback matcher lib (included as a composer dependency in this library). Example usage:
assertThat("hello", matchesUsing(function($value) { return $value === "hello"; }));
You can create your own custom matchers with the callback matcher by using describedAs()
to provide a better
description for the expectation.
function isSomething()
{
return describedAs('a custom value', new CallbackMatcher(
function($value) {
return $value === 'my custom value';
}
)
}
assertThat($foo, isSomething());