Download the PHP package hamcrest/hamcrest-php without Composer
On this page you can find all versions of the php package hamcrest/hamcrest-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package hamcrest-php
This is the PHP port of Hamcrest Matchers
Hamcrest is a matching library originally written for Java, but subsequently ported to many other languages. hamcrest-php is the official PHP port of Hamcrest and essentially follows a literal translation of the original Java API for Hamcrest, with a few Exceptions, mostly down to PHP language barriers:
-
instanceOf($theClass)
is actuallyanInstanceOf($theClass)
-
both(containsString('a'))->and(containsString('b'))
is actuallyboth(containsString('a'))->andAlso(containsString('b'))
-
either(containsString('a'))->or(containsString('b'))
is actuallyeither(containsString('a'))->orElse(containsString('b'))
-
Unless it would be non-semantic for a matcher to do so, hamcrest-php allows dynamic typing for it's input, in "the PHP way". Exception are where semantics surrounding the type itself would suggest otherwise, such as stringContains() and greaterThan().
-
Several official matchers have not been ported because they don't make sense or don't apply in PHP:
typeCompatibleWith($theClass)
eventFrom($source)
hasProperty($name)
**samePropertyValuesAs($obj)
**
- When most of the collections matchers are finally ported, PHP-specific aliases will probably be created due to a difference in naming conventions between Java's Arrays, Collections, Sets and Maps compared with PHP's Arrays.
** [Unless we consider POPO's (Plain Old PHP Objects) akin to JavaBeans]
- The POPO thing is a joke. Java devs coin the term POJO's (Plain Old Java Objects).
Usage
Hamcrest matchers are easy to use as:
Alternatively, you can use the global proxy-functions:
:warning: NOTE: the global proxy-functions aren't autoloaded by default, so you will need to load them first:
For brevity, all of the examples below use the proxy-functions.
Documentation
A tutorial can be found on the Hamcrest site.
Available Matchers
- Array
- Collection
- Object
- Numbers
- Type checking
- XML
Array
-
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
Collection
-
emptyTraversable
- check if traversable is empty -
nonEmptyTraversable
- check if traversable isn't empty traversableWithSize
Core
-
allOf
- Evaluates to true only if ALL of the passed in matchers evaluate to true. -
anyOf
- Evaluates to true if ANY of the passed in matchers evaluate to true. -
noneOf
- Evaluates to false if ANY of the passed in matchers evaluate to true. -
both
+andAlso
- This is useful for fluently combining matchers that must both pass. -
either
+orElse
- This is useful for fluently combining matchers where either may pass, -
describedAs
- Wraps an existing matcher and overrides the description when it fails. -
everyItem
- A matcher to apply to every element in an array. -
hasItem
- check array has given item, it can take a matcher argument hasItems
- check array has given items, it can take multiple matcher as arguments
Object
-
hasToString
- check__toString
ortoString
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 ofanInstanceOf
-
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
Numbers
-
closeTo
- check value close to a range -
comparesEqualTo
- check with '==' -
greaterThan
- check '>' -
greaterThanOrEqualTo
-
atLeast
- The value is >= given value -
lessThan
-
lessThanOrEqualTo
atMost
- The value is <= given value
String
-
emptyString
- check for empty string -
isEmptyOrNullString
-
nullOrEmptyString
-
isNonEmptyString
-
nonEmptyString
-
equalToIgnoringCase
-
equalToIgnoringWhiteSpace
-
matchesPattern
- matches with regex pattern -
containsString
- check for substring -
containsStringIgnoringCase
-
stringContainsInOrder
-
endsWith
- check string that ends with given value startsWith
- check string that starts with given value
Type-checking
-
arrayValue
- check array type -
booleanValue
-
boolValue
- alias of booleanValue -
callableValue
- check if value is callable -
doubleValue
-
floatValue
-
integerValue
-
intValue
- alias ofintegerValue
-
numericValue
- check if value is numeric -
objectValue
- check for object -
anObject
-
resourceValue
- check resource type -
scalarValue
- check for scalar value stringValue
XML
hasXPath
- check xml with a xpath