/** * Make an assertion and throw {@link Hamcrest_AssertionError} if it fails. * * The first parameter may optionally be a string identifying the assertion * to be included in the failure message. * * If the third parameter is not a matcher it is passed to * {@link Hamcrest_Core_IsEqual#equalTo} to create one. * * Example: * <pre> * // With an identifier * assertThat("apple flavour", $apple->flavour(), equalTo("tasty")); * // Without an identifier * assertThat($apple->flavour(), equalTo("tasty")); * // Evaluating a boolean expression * assertThat("some error", $a > $b); * assertThat($a > $b); * </pre> */ public static function assertThat() { $args = func_get_args(); switch (count($args)) { case 1: self::$_count++; if (!$args[0]) { throw new Hamcrest_AssertionError(); } break; case 2: self::$_count++; if ($args[1] instanceof Hamcrest_Matcher) { self::doAssert('', $args[0], $args[1]); } elseif (!$args[1]) { throw new Hamcrest_AssertionError($args[0]); } break; case 3: self::$_count++; self::doAssert($args[0], $args[1], Hamcrest_Util::wrapValueWithIsEqual($args[2])); break; default: throw new InvalidArgumentException('assertThat() requires one to three arguments'); } }
/** * Does traversable size satisfy a given matcher? * * @factory */ public static function traversableWithSize($size) { return new self(Hamcrest_Util::wrapValueWithIsEqual($size)); }
/** * Test if the value is an array containing this matcher. * * Example: * <pre> * assertThat(array('a', 'b'), hasItem(equalTo('b'))); * //Convenience defaults to equalTo() * assertThat(array('a', 'b'), hasItem('b')); * </pre> * * @factory ... */ public static function hasItem() { $args = func_get_args(); $firstArg = array_shift($args); return new self(Hamcrest_Util::wrapValueWithIsEqual($firstArg)); }
public function testWrapValueWithIsEqualWrapsPrimitive() { $matcher = Hamcrest_Util::wrapValueWithIsEqual('foo'); $this->assertInstanceOf('Hamcrest_Core_IsEqual', $matcher); $this->assertTrue($matcher->matches('foo')); }
/** * Evaluates to true if any item in an array satisfies the given matcher. * * @param mixed $item as a {@link Hamcrest_Matcher} or a value. * * @factory hasValue */ public static function hasItemInArray($item) { return new self(Hamcrest_Util::wrapValueWithIsEqual($item)); }
/** * Decorates another Matcher, retaining the behavior but allowing tests * to be slightly more expressive. * * For example: assertThat($cheese, equalTo($smelly)) * vs. assertThat($cheese, is(equalTo($smelly))) * * @factory */ public static function is($value) { return new self(Hamcrest_Util::wrapValueWithIsEqual($value)); }
/** * Evaluates to true if any key in an array matches the given matcher. * * @param mixed $key as a {@link Hamcrest_Matcher} or a value. * * @factory hasKey */ public static function hasKeyInArray($key) { return new self(Hamcrest_Util::wrapValueWithIsEqual($key)); }
/** * Does array size satisfy a given matcher? * * @factory */ public static function hasToString($matcher) { return new self(Hamcrest_Util::wrapValueWithIsEqual($matcher)); }
/** * Test if an array has both an key and value in parity with each other. * * @factory hasEntry */ public static function hasKeyValuePair($key, $value) { return new self(Hamcrest_Util::wrapValueWithIsEqual($key), Hamcrest_Util::wrapValueWithIsEqual($value)); }