hamcrest-php Documentation

repository·master·Indexed 27 days ago

https://github.com/hamcrest/hamcrest-php

The official PHP port of the Hamcrest matching library, providing a rich set of matchers for assertions in tests. Includes documentation on using the MatcherAssert class, global proxy functions, and specialized matchers for arrays, collections, objects, numbers, strings, XML, and type-checking.

Tokens
1.5K
Snippets
7
Records
10
Agent score
42%

What's inside hamcrest-php

  1. Use Hamcrest Matchers in PHP

    master

    Hamcrest 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));
  2. Prevent 'Risky Test' warnings in PHPUnit

    master

    To prevent tests from being marked as 'Risky' (due to the This test did not perform any assertions message), you must manually update the assertion count in your test case's tearDown method using the MatcherAssert class.

    $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount());
    \Hamcrest\MatcherAssert::resetCount();
  3. Number Matchers Reference

    master

    Matchers for numeric comparisons.

    * `closeTo` - check value close to a range
    * `comparesEqualTo` - check with '=='
    * `greaterThan` - check '>'
    * `greaterThanOrEqualTo` - check '>='
    * `atLeast` - The value is >= given value
    * `lessThan` - check '<'
    * `lessThanOrEqualTo` - check '<='
    * `atMost` - The value is <= given value
  4. Array Matchers Reference

    master

    Use these matchers to validate PHP arrays.

    * `anArray` - evaluates an array
    * `hasItemInArray` - check if item exists in array
    * `hasValue` - alias of `hasItemInArray`
    * `arrayContainingInAnyOrder` - check if array contains elements in any order
    * `containsInAnyOrder` - alias of `arrayContainingInAnyOrder`
    * `arrayContaining` - An array with elements that match the given matchers in the same order.
    * `contains` - check array in same order
    * `hasKeyInArray` - check if array has given key
    * `hasKey` - alias of `hasKeyInArray`
    * `hasKeyValuePair` - check if array has given key, value pair
    * `hasEntry` - same as `hasKeyValuePair`
    * `arrayWithSize` - check array has given size
    * `emptyArray` - check if array is empty
    * `nonEmptyArray` - check if array is not empty
  5. Object Matchers Reference

    master

    Matchers for validating object properties, types, and identity.

    * `hasToString` - check `__toString` or `toString` method
    * `equalTo` - compares two instances using comparison operator '=='
    * `identicalTo` - compares two instances using identity operator '==='
    * `anInstanceOf` - check instance is an instance|sub-class of given class
    * `any` - alias of `anInstanceOf`
    * `nullValue` - check null
    * `notNullValue` - check not null
    * `sameInstance` - check for same instance
    * `typeOf` - check type
    * `notSet` - check if instance property is not set
    * `set` - check if instance property is set