### 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 = << 1 2 XML; $doc = new DOMDocument; $doc->loadXML($xml); assertThat($doc, hasXPath("book", 2)); ``` -------------------------------- ### TypeOf and Type-Specific Matchers Source: https://github.com/hamcrest/hamcrest-php/blob/master/CHANGES.txt Asserts the built-in PHP type of a value using `gettype()` or specific type matchers like `integerValue()` or `stringValue()`. ```php assertThat($count, typeOf('integer')); assertThat(3.14159, typeOf('double')); assertThat(array('foo', 'bar'), typeOf('array')); assertThat(new stdClass(), typeOf('object')); // Type-specific matchers: assertThat($count, integerValue()); assertThat(3.14159, floatValue()); assertThat('foo', stringValue()); ``` -------------------------------- ### Array Matcher: arrayWithSize Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Checks if an array has a specific number of elements. ```php assertthat([2, 4, 6], arrayWithSize(3)); ``` -------------------------------- ### Scalar Value Matcher Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Asserts that a value is a scalar type (integer, float, string, or boolean). The `scalarValue` matcher checks for this. ```php assertThat(1, scalarValue()); ``` -------------------------------- ### Array Matcher: arrayContaining Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Checks if an array contains elements that match the given matchers in the exact same order. ```php assertThat([2, 4, 6], arrayContaining([2, 4, 6])); assertthat([2, 4, 6], not(arrayContaining([6, 4, 2]))); ``` -------------------------------- ### Integer Type Matchers Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Verifies if a value is an integer. `integerValue` and its alias `intValue` can be used for this assertion. ```php assertThat(1, integerValue()); ``` -------------------------------- ### HasItem and HasItems Matchers Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Checks if an array contains a specific item (using a matcher) or multiple items. ```php assertThat([2, 4, 6], hasItem(equalTo(2))); ``` ```php assertThat([1, 3, 5], hasItems(equalTo(1), equalTo(3))); ``` -------------------------------- ### Substring Matchers Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Checks if a string contains a specific substring. `containsString` performs a case-sensitive check, while `containsStringIgnoringCase` ignores case. ```php assertThat("foobar", containsString("foo")); assertThat("fooBar", containsStringIgnoringCase("bar")); ``` -------------------------------- ### Object Type Matchers Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Checks if a value is an object. Both `objectValue` and `anObject` can be used for this assertion. ```php $obj = new stdClass; assertThat($obj, objectValue()); assertThat($obj, anObject()); ``` -------------------------------- ### Array Matcher: hasKeyValuePair Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Checks if an array contains a specific key-value pair. ```php assertThat(['name'=> 'foobar'], hasKeyValuePair('name', 'foobar')); ``` -------------------------------- ### Float/Double Type Matchers Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Checks if a value is a floating-point number. Both `doubleValue` and `floatValue` are available for this. ```php assertThat(3.14, doubleValue()); assertThat(3.14, floatValue()); ``` -------------------------------- ### Scalar Value Matcher Source: https://github.com/hamcrest/hamcrest-php/blob/master/CHANGES.txt Asserts that a value is scalar, meaning it is of type bool, int, float, double, or string. ```php assertThat($count, scalarValue()); assertThat('foo', scalarValue()); ``` -------------------------------- ### String Type Matcher Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Checks if a value is of type string. The `stringValue` matcher is used for this assertion. ```php assertThat("", stringValue()); ``` -------------------------------- ### Array Type Matcher Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Verifies if the value under test is an array. The `arrayValue` matcher is used for this type check. ```php assertThat([], arrayValue()); ``` -------------------------------- ### Array Matcher: hasKeyInArray Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Checks if an array contains a specific key. ```php assertThat(['name'=> 'foobar'], hasKeyInArray('name')); ``` -------------------------------- ### Array Matcher: hasItemInArray Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Checks if an item exists within an array. ```php $list = range(2, 7, 2); $item = 4; assertThat($list, hasItemInArray($item)); ``` -------------------------------- ### Boolean Type Matchers Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Checks if a value is a boolean. Both `booleanValue` and its alias `boolValue` can be used. ```php assertThat(true, booleanValue()); ``` -------------------------------- ### Array Matcher: arrayContainingInAnyOrder Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Checks if an array contains specific elements, regardless of their order. ```php assertThat([2, 4, 6], arrayContainingInAnyOrder([6, 4, 2])); assertThat([2, 4, 6], arrayContainingInAnyOrder([4, 2, 6])); ``` -------------------------------- ### Array Matcher: anArray Source: https://github.com/hamcrest/hamcrest-php/blob/master/README.md Checks if the evaluated value is an array. ```php assertThat([], anArray()); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.