在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
This is the PHP port of Hamcrest MatchersHamcrest is a matching library originally written for Java, butsubsequently ported to many other languages. hamcrest-php is theofficial PHP port of Hamcrest and essentially follows a literaltranslation of the original Java API for Hamcrest, with a fewExceptions, mostly down to PHP language barriers:
** [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 OldJava Objects). UsageHamcrest matchers are easy to use as: Hamcrest_MatcherAssert::assertThat('a', Hamcrest_Matchers::equalToIgnoringCase('A')); Alternatively, you can use the global proxy-functions: $result = true;// with an identifierassertThat("result should be true", $result, equalTo(true));// without an identifierassertThat($result, equalTo(true));// evaluate a boolean expressionassertThat($result === true);// with syntactic sugar is()assertThat(true, is(true)); NOTE: the global proxy-functions aren't autoloaded by default, so you will need to load them first: \Hamcrest\Util::registerGlobalFunctions(); For brevity, all of the examples below use the proxy-functions. DocumentationA tutorial can be found on the Hamcrest site. Available MatchersArray
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()); Collection
$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))));` Core
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))); Object
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")); Numbers
assertThat(3, closeTo(3, 0.5));
assertThat(2, comparesEqualTo(2));
assertThat(2, greaterThan(1));
assertThat(2, greaterThanOrEqualTo(2));
assertThat(3, atLeast(2));
assertThat(2, lessThan(3));
assertThat(2, lessThanOrEqualTo(3));
assertThat(2, atMost(3)); String
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")); Type-checking
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 = <<<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)); |
请发表评论