PHP code example of hamcrest / hamcrest-php

1. Go to this page and download the library: Download hamcrest/hamcrest-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hamcrest / hamcrest-php example snippets


Hamcrest_MatcherAssert::assertThat('a', Hamcrest_Matchers::equalToIgnoringCase('A'));

$result = true;
// with an identifier
assertThat("result should be true", $result, equalTo(true));

// without an identifier
assertThat($result, equalTo(true));

// evaluate a boolean expression
assertThat($result === true);

// with syntactic sugar is()
assertThat(true, is(true));

\Hamcrest\Util::registerGlobalFunctions();

assertThat([], anArray());

$list = range(2, 7, 2);
$item = 4;
assertThat($list, hasItemInArray($item));

assertThat([2, 4, 6], arrayContainingInAnyOrder([6, 4, 2]));
assertThat([2, 4, 6], arrayContainingInAnyOrder([4, 2, 6]));

assertThat([2, 4, 6], arrayContaining([2, 4, 6]));
assertthat([2, 4, 6], not(arrayContaining([6, 4, 2])));

assertThat([2, 4, 6], contains([2, 4, 6]));

assertThat(['name'=> 'foobar'], hasKeyInArray('name'));

assertThat(['name'=> 'foobar'], hasKeyValuePair('name', 'foobar'));

assertthat([2, 4, 6], arrayWithSize(3));

assertThat([], emptyArray());

assertThat([1], nonEmptyArray());

$empty_it = new EmptyIterator;
assertThat($empty_it, emptyTraversable());

$non_empty_it = new ArrayIterator(range(1, 10));
assertThat($non_empty_it, nonEmptyTraversable());
a

$non_empty_it = new ArrayIterator(range(1, 10));
assertThat($non_empty_it, traversableWithSize(count(range(1, 10))));
`

assertThat([2,4,6], allOf(hasValue(2), arrayWithSize(3)));

assertThat([2, 4, 6], anyOf(hasValue(8), hasValue(2)));

assertThat([2, 4, 6], noneOf(hasValue(1), hasValue(3)));

assertThat([2, 4, 6], both(hasValue(2))->andAlso(hasValue(4)));

assertThat([2, 4, 6], either(hasValue(2))->orElse(hasValue(4)));
 
$expected = "Dog";
$found = null;
// this assertion would result error message as Expected: is not null but: was null
//assertThat("Expected {$expected}, got {$found}", $found, is(notNullValue()));
// and this assertion would result error message as Expected: Dog but: was null
//assertThat($found, describedAs($expected, notNullValue()));

assertThat([2, 4, 6], everyItem(notNullValue()));

assertThat([2, 4, 6], hasItem(equalTo(2)));

assertThat([1, 3, 5], hasItems(equalTo(1), equalTo(3)));

class Foo {
    public $name = null;

    public function __toString() {
        return "[Foo]Instance";
    }
}
$foo = new Foo;
assertThat($foo, hasToString(equalTo("[Foo]Instance")));

$foo = new Foo;
$foo2 = new Foo;
assertThat($foo, equalTo($foo2));

assertThat($foo, is(not(identicalTo($foo2))));

assertThat($foo, anInstanceOf(Foo::class));

assertThat(null, is(nullValue()));

assertThat("", notNullValue());

assertThat($foo, is(not(sameInstance($foo2))));
assertThat($foo, is(sameInstance($foo)));
 
assertThat(1, typeOf("integer"));

assertThat($foo, notSet("name"));

$foo->name = "bar";
assertThat($foo, set("name"));

assertThat(3, closeTo(3, 0.5));

assertThat(2, comparesEqualTo(2));

assertThat(2, greaterThanOrEqualTo(2));

assertThat(3, atLeast(2));

assertThat(2, lessThan(3));

assertThat(2, lessThanOrEqualTo(3));

assertThat(2, atMost(3));

assertThat("", emptyString());

assertThat(null, isEmptyOrNullString());

assertThat("", nullOrEmptyString());

assertThat("foo", isNonEmptyString());

assertThat("foo", nonEmptyString());

assertThat("Foo", equalToIgnoringCase("foo"));

assertThat(" Foo ", equalToIgnoringWhiteSpace("Foo"));

assertThat("foobarbaz", matchesPattern('/(foo)(bar)(baz)/'));

assertThat("foobar", containsString("foo"));

assertThat("fooBar", containsStringIgnoringCase("bar"));

assertThat("foo", stringContainsInOrder("foo"));

assertThat("foo", endsWith("oo"));

assertThat("bar", startsWith("ba"));

assertThat([], arrayValue());

assertThat(true, booleanValue());

$func = function () {};
assertThat($func, callableValue());

assertThat(3.14, doubleValue());

assertThat(3.14, floatValue());

assertThat(1, integerValue());

assertThat("123", numericValue());

$obj = new stdClass;
assertThat($obj, objectValue());

assertThat($obj, anObject());

$fp = fopen("/tmp/foo", "w+");
assertThat($fp, resourceValue());

assertThat(1, scalarValue());

assertThat("", stringValue());

$xml = <<<XML
<books>
  <book>
    <isbn>1</isbn>   
  </book>
  <book>
    <isbn>2</isbn>   
  </book>
</books>
XML;

$doc = new DOMDocument;
$doc->loadXML($xml);
assertThat($doc, hasXPath("book", 2));