Test to see if a value is an expectation object. A useful utility method.
public static isExpectation ( mixed $expectation ) : boolean | ||
$expectation | mixed | Hopefully an Expectation class. |
return | boolean | True if descended from this class. |
function assertEltByIdHasAttrOfValue($eltId, $attrName, $attrValueExpected = true) { $matches = array(); $haystack = $this->getBrowser()->getContent(); // preg_match('/(\<[^\>]\s+id\s*=\s*"'.$eltId.'"\s+[^\>]*\>)/',$this->getBrowser()->getContent(),$matches); preg_match('/(\\<[^\\>]*\\s+id\\s*=\\s*"' . $eltId . '"\\s*[^\\>]*\\>)/', $haystack, $matches); // echo $matches[1]; if (!$this->assertTrue(isset($matches[1]), "Element with id [{$eltId}] should exist")) { return false; } $haystack = $matches[1]; $matches = array(); preg_match('/\\s+(' . $attrName . ')\\s*=\\s*"([^"]*)"/', $haystack, $matches); if (!$this->assertTrue(isset($matches[1]) && isset($matches[2]), "Element with id [{$eltId}] should have attribute of [{$attrName}]")) { return false; } if ($attrValueExpected === true) { return true; } if (!SimpleExpectation::isExpectation($attrValueExpected)) { $attrValueExpected = new IdenticalExpectation($attrValueExpected); } $haystack = $matches[2]; if ($attrValueExpected->test($haystack)) { return true; } return $this->assert($attrValueExpected, $haystack, "Element with id [{$eltId}] attribute [{$attrName}] value does not match- " . $attrValueExpected->testMessage($haystack)); }
function _coerceToExpectation($expected) { if (SimpleExpectation::isExpectation($expected)) { return $expected; } return new IdenticalExpectation($expected); }
/** * Checks that a cookie is set for the current page * and optionally checks the value. * @param string $name Name of cookie to test. * @param string $expected Expected value as a string or * false if any value will do. * @param string $message Message to display. * @return boolean True if pass. * @access public */ function assertCookie($name, $expected = false, $message = '%s') { $value = $this->getCookie($name); if (!$expected) { return $this->assertTrue($value, sprintf($message, "Expecting cookie [{$name}]")); } if (!SimpleExpectation::isExpectation($expected)) { $expected = new EqualExpectation($expected); } return $this->assert($expected, $value, "Expecting cookie [{$name}] -> {$message}"); }
/** * Sets up an expectation of an exception. * This has the effect of intercepting an * exception that matches. * @param SimpleExpectation $expected Expected exception to match. * @param string $message Message to display. * @access public */ function expectException($expected = false, $message = '%s') { if ($expected === false) { $expected = new AnythingExpectation(); } if (!SimpleExpectation::isExpectation($expected)) { $expected = new ExceptionExpectation($expected); } $this->expected = $expected; $this->message = $message; }
/** * Turns an expected exception into a SimpleExpectation object. * @param mixed $exception Exception, expectation or * class name of exception. * @return SimpleExpectation Expectation that will match the * exception. */ private function coerceToExpectation($exception) { if ($exception === false) { return new AnythingExpectation(); } if (!SimpleExpectation::isExpectation($exception)) { return new ExceptionExpectation($exception); } return $exception; }