### String Start/End Matchers Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Checks if a string starts or ends with a given substring. `startsWith` and `endsWith` are used for these assertions. ```php assertThat("bar", startsWith("ba")); assertThat("foo", endsWith("oo")); ``` -------------------------------- ### Run Unit Tests Source: https://github.com/hamcrest/hamcrest-php/blob/master/CONTRIBUTING.md Executes the unit tests for Hamcrest PHP using PHPUnit with a specific configuration file. ```bash vendor/bin/phpunit -c tests/phpunit.xml.dist ``` -------------------------------- ### Registering Global Proxy-Functions Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Shows how to register the global proxy-functions, which are not autoloaded by default, enabling their use in test cases. ```php \Hamcrest\Util::registerGlobalFunctions(); ``` -------------------------------- ### Clone Repository Source: https://github.com/hamcrest/hamcrest-php/blob/master/CONTRIBUTING.md Clones the Hamcrest PHP repository from GitHub to your local machine. ```bash git clone https://github.com/hamcrest/hamcrest-php ``` -------------------------------- ### Basic Assertion with MatcherAssert Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Demonstrates the fundamental way to use Hamcrest matchers with the MatcherAssert class for assertions. ```php \Hamcrest\MatcherAssert::assertThat('a', \Hamcrest\Matchers::equalToIgnoringCase('A')); ``` -------------------------------- ### Usage of Global Proxy-Functions Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Illustrates how to use the convenient global proxy-functions provided by Hamcrest PHP for assertions, including with and without identifiers, and evaluating boolean expressions. ```php $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)); ``` -------------------------------- ### Update Dependencies Source: https://github.com/hamcrest/hamcrest-php/blob/master/CONTRIBUTING.md Updates project dependencies using Composer, essential for setting up the development environment. ```bash composer update ``` -------------------------------- ### Regex Pattern Matching with Hamcrest PHP Source: https://github.com/hamcrest/hamcrest-php/blob/master/CHANGES.txt Shows how to use the `matchesPattern()` matcher in Hamcrest PHP to verify if a string matches a given regular expression. This is useful for validating string formats. ```php assertThat('foobar', matchesPattern('/o+b/')); ``` -------------------------------- ### Hamcrest PHP Matcher Aliases Source: https://github.com/hamcrest/hamcrest-php/blob/master/CHANGES.txt Lists the added aliases for common Hamcrest PHP matchers, providing shorter and more readable alternatives for frequently used assertions. ```php // Alias for greaterThanOrEqualTo() assertThat(5, atLeast(3)); // Alias for lessThanOrEqualTo() assertThat(3, atMost(5)); ``` -------------------------------- ### Matcher Aliases Source: https://github.com/hamcrest/hamcrest-php/blob/master/CHANGES.txt Provides aliases for common matchers to align with the Java API for consistency. ```php // Aliases: hasEntry() -> hasKeyValuePair() hasValue() -> hasItemInArray() contains() -> arrayContaining() containsInAnyOrder() -> arrayContainingInAnyOrder() ``` -------------------------------- ### Core Matchers (Logical Combinations) Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Matchers for combining other matchers logically: allOf, anyOf, noneOf, both/andAlso, either/orElse. ```php assertThat([2,4,6], allOf(hasValue(2), arrayWithSize(3))); ``` ```php assertThat([2, 4, 6], anyOf(hasValue(8), hasValue(2))); ``` ```php assertThat([2, 4, 6], noneOf(hasValue(1), hasValue(3))); ``` ```php assertThat([2, 4, 6], both(hasValue(2))->andAlso(hasValue(4))); ``` ```php assertThat([2, 4, 6], either(hasValue(2))->orElse(hasValue(4))); ``` -------------------------------- ### Callable Assertion with Hamcrest PHP Source: https://github.com/hamcrest/hamcrest-php/blob/master/CHANGES.txt Demonstrates how to use the `callable()` matcher in Hamcrest PHP to assert if a given value is a callable function or method. This includes various forms of callable definitions in PHP. ```php assertThat('preg_match', callable()); assertThat(array('SomeClass', 'SomeMethod'), callable()); assertThat(array($object, 'SomeMethod'), callable()); assertThat($object, callable()); assertThat(function ($x, $y) { return $x + $y; }, callable()); ``` -------------------------------- ### Number Comparison Matchers Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Matchers for comparing numbers, including equality (comparesEqualTo), range checks (closeTo), and inequality comparisons (greaterThan, lessThan, etc.). ```php assertThat(3, closeTo(3, 0.5)); ``` ```php assertThat(2, comparesEqualTo(2)); ``` ```php assertThat(2, greaterThan(1)); ``` ```php assertThat(2, greaterThanOrEqualTo(2)); ``` ```php assertThat(3, atLeast(2)); ``` ```php assertThat(2, lessThan(3)); ``` ```php assertThat(2, lessThanOrEqualTo(3)); ``` ```php assertThat(2, atMost(3)); ``` -------------------------------- ### Resource Type Matcher Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Verifies if a value is a resource type, such as a file handle. The `resourceValue` matcher is used. ```php $fp = fopen("/tmp/foo", "w+"); assertThat($fp, resourceValue()); ``` -------------------------------- ### Non-Empty String Matchers Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Asserts that a string is not empty. The `isNonEmptyString` and `nonEmptyString` matchers serve this purpose. ```php assertThat("foo", isNonEmptyString()); assertThat("foo", nonEmptyString()); ``` -------------------------------- ### noneOf() Matcher Source: https://github.com/hamcrest/hamcrest-php/blob/master/CHANGES.txt Introduces `noneOf()` as a convenient shortcut for `not(anyOf())`, allowing for assertions that none of a set of conditions are met. ```php assertThat($value, noneOf($matcher1, $matcher2)); ``` -------------------------------- ### DescribedAs Matcher Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Wraps an existing matcher to override its description when an assertion fails, providing more context. ```php $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())); ``` -------------------------------- ### Empty String Matchers Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Checks if a string is empty or null. Includes specific matchers for empty strings and null or empty strings. ```php assertThat("", emptyString()); assertThat(null, isEmptyOrNullString()); assertThat("", nullOrEmptyString()); ``` -------------------------------- ### Object Identity and Type Matchers Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Matchers for comparing object instances by value (equalTo), identity (identicalTo, sameInstance), and type (anInstanceOf). ```php class Foo { public $name = null; public function __toString() { return "[Foo]Instance"; } } $foo = new Foo; $foo2 = new Foo; assertThat($foo, equalTo($foo2)); ``` ```php assertThat($foo, is(not(identicalTo($foo2)))); ``` ```php assertThat($foo, anInstanceOf(Foo::class)); ``` ```php assertThat($foo, is(not(sameInstance($foo2)))); assertThat($foo, is(sameInstance($foo))); ``` -------------------------------- ### EveryItem Matcher Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Applies a given matcher to every element within an array, ensuring all elements satisfy the condition. ```php assertThat([2, 4, 6], everyItem(notNullValue())); ``` -------------------------------- ### Object Property Matchers Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Assertions for checking object properties, including whether they are set or not. ```php $foo->name = "bar"; assertThat($foo, set("name")); ``` ```php assertThat($foo, notSet("name")); ``` -------------------------------- ### Non-Empty Matchers Source: https://github.com/hamcrest/hamcrest-php/blob/master/CHANGES.txt Provides matchers to assert that a collection or string is not empty. These complement existing emptiness-checking forms. ```php assertThat($value, nonEmptyString()); assertThat($value, nonEmptyArray()); assertThat($value, nonEmptyTraversable()); ``` -------------------------------- ### Array Matchers with Variable Arguments Source: https://github.com/hamcrest/hamcrest-php/blob/master/CHANGES.txt Enables array-based matcher factory methods to accept variable arguments, similar to `allOf()`. Non-matcher arguments are wrapped with `IsEqual`. ```php assertThat($array, anArray()); assertThat($array, arrayContainingInAnyOrder($matcher1, $matcher2)); assertThat($array, containsInAnyOrder($matcher1, $matcher2)); assertThat($array, arrayContaining($matcher1, $matcher2)); assertThat($array, contains($matcher1, $matcher2)); assertThat($string, stringContainsInOrder($substring1, $substring2)); ``` -------------------------------- ### Collection Size Matchers Source: https://github.com/hamcrest/hamcrest-php/blob/master/CHANGES.txt Provides matchers to check if a traversable collection is empty or has a specific size. ```php assertThat($iterator, emptyTraversable()); assertThat($iterator, traversableWithSize(5)); ``` -------------------------------- ### Matchers Class Overview Source: https://github.com/hamcrest/hamcrest-php/blob/master/generator/parts/matchers_header.txt The Matchers class in Hamcrest PHP offers static factory methods to create instances of various assertion matchers. This class simplifies the process of writing assertions by providing a convenient way to access commonly used matchers. ```php /** * A series of static factories for all hamcrest matchers. */ class Matchers { // ... static factory methods for matchers ... } ``` -------------------------------- ### Null and Type Checking Matchers Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Matchers for asserting whether a value is null (nullValue) or not null (notNullValue), and for checking the data type (typeOf). ```php assertThat(null, is(nullValue())); ``` ```php assertThat("", notNullValue()); ``` ```php assertThat(1, typeOf("integer")); ``` -------------------------------- ### Handling Risky Tests Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Provides the necessary code to add to a test case's tearDown method to prevent 'Risky' test outcomes when no assertions are made. ```php $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()); \Hamcrest\MatcherAssert::resetCount(); ``` -------------------------------- ### Collection Matchers Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Assertions for checking the state of traversable objects, such as emptiness or size. ```php $empty_it = new EmptyIterator; assertThat($empty_it, emptyTraversable()); ``` ```php $non_empty_it = new ArrayIterator(range(1, 10)); assertThat($non_empty_it, nonEmptyTraversable()); ``` ```php $non_empty_it = new ArrayIterator(range(1, 10)); assertThat($non_empty_it, traversableWithSize(count(range(1, 10)))); ``` -------------------------------- ### Case-Insensitive String Matchers Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Compares strings ignoring case differences. `equalToIgnoringCase` is used for exact matches ignoring case. ```php assertThat("Foo", equalToIgnoringCase("foo")); ``` -------------------------------- ### HasToString Matcher Source: https://github.com/hamcrest/hamcrest-php/blob/master/CHANGES.txt Asserts the return value of an object's `toString()` or `__toString()` method. ```php assertThat($anObject, hasToString('foo')); ``` -------------------------------- ### XPath Matchers Source: https://github.com/hamcrest/hamcrest-php/blob/master/CHANGES.txt Asserts XPath expressions against an XML/HTML DOM, including checking node content and counts. ```php assertThat($dom, hasXPath('books/book/title')); assertThat($dom, hasXPath('books/book[contains(title, "Alice")]', 3)); assertThat($dom, hasXPath('books/book/title', 'Alice in Wonderland')); assertThat($dom, hasXPath('count(books/book)', greaterThan(10))); ``` -------------------------------- ### Whitespace-Insensitive String Matchers Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Compares strings while ignoring leading and trailing whitespace. `equalToIgnoringWhiteSpace` is used for this comparison. ```php assertThat(" Foo ", equalToIgnoringWhiteSpace("Foo")); ``` -------------------------------- ### Regex Pattern Matcher Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Validates if a string matches a given regular expression pattern. The `matchesPattern` matcher takes a regex string as an argument. ```php assertThat("foobarbaz", matchesPattern('/(foo)(bar)(baz)/')); ``` -------------------------------- ### assertThat Function Definition Source: https://github.com/hamcrest/hamcrest-php/blob/master/generator/parts/functions_header.txt Defines the assertThat function, which acts as a wrapper for Hamcrest's assertion mechanism. It handles arguments and delegates the actual assertion to the Hamcrest\MatcherAssert class. This function is defined only if it doesn't already exist to prevent redefinition errors. ```php if (!function_exists('assertThat')) { /** * Make an assertion and throw {@link Hamcrest_AssertionError} if it fails. * * Example: *
* //With an identifier
* assertThat("assertion identifier", $apple->flavour(), equalTo("tasty"));
* //Without an identifier
* assertThat($apple->flavour(), equalTo("tasty"));
* //Evaluating a boolean expression
* assertThat("some error", $a > $b);
*
*/
function assertThat(): void
{
$args = func_get_args();
call_user_func_array(
array('Hamcrest\MatcherAssert', 'assertThat'),
$args
);
}
}
```
--------------------------------
### IsSet and NotSet Matchers
Source: https://github.com/hamcrest/hamcrest-php/blob/master/CHANGES.txt
Asserts whether a specific key is set or not set within an array.
```php
assertThat(array('foo' => 'bar'), set('foo'));
assertThat(array('foo' => 'bar'), notSet('bar'));
```
--------------------------------
### Numeric Type Matcher
Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md
Asserts that a value can be treated as a number, including numeric strings. The `numericValue` matcher is used.
```php
assertThat("123", numericValue());
```
--------------------------------
### Array Matcher: nonEmptyArray
Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md
Checks if an array is not empty.
```php
assertThat([1], nonEmptyArray());
```
--------------------------------
### String Contains Ignoring Case
Source: https://github.com/hamcrest/hamcrest-php/blob/master/CHANGES.txt
Asserts that a string contains a specific substring, ignoring case differences. This uses `stripos()` internally.
```php
assertThat('fOObAr', containsStringIgnoringCase('oba'));
assertThat('fOObAr', containsString('oba')->ignoringCase());
```
--------------------------------
### String Representation Matcher
Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md
Checks the string representation of an object, typically by asserting the result of its __toString() method.
```php
class Foo {
public $name = null;
public function __toString() {
return "[Foo]Instance";
}
}
$foo = new Foo;
assertThat($foo, hasToString(equalTo("[Foo]Instance")));
```
--------------------------------
### Array Matcher: emptyArray
Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md
Checks if an array is empty.
```php
assertThat([], emptyArray());
```
--------------------------------
### Callable Type Matcher
Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md
Asserts that a value is a callable type, such as a function or method. The `callableValue` matcher is used.
```php
$func = function () {};
assertThat($func, callableValue());
```
--------------------------------
### Numeric Value Matcher
Source: https://github.com/hamcrest/hamcrest-php/blob/master/CHANGES.txt
Asserts that a value is numeric, which includes integers, floats/doubles, and strings formatted as numbers.
```php
assertThat(5, numericValue());
assertThat('-5e+3', numericValue());
```
--------------------------------
### Ordered Substring Matcher
Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md
Asserts that a string contains multiple substrings in the specified order. The `stringContainsInOrder` matcher is used for this.
```php
assertThat("foo", stringContainsInOrder("foo"));
```
--------------------------------
### Array Matcher: contains
Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md
An alias for arrayContaining, checking if an array's elements match in the same order.
```php
assertThat([2, 4, 6], contains([2, 4, 6]));
```
--------------------------------
### XML XPath Matcher
Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md
Validates XML content against a given XPath expression, optionally checking the count of matching nodes. The `hasXPath` matcher is used.
```php
$xml = <<