本文整理汇总了PHP中Assert::fail方法的典型用法代码示例。如果您正苦于以下问题:PHP Assert::fail方法的具体用法?PHP Assert::fail怎么用?PHP Assert::fail使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Assert
的用法示例。
在下文中一共展示了Assert::fail方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: error
/**
* Checks if the function generates PHP error or throws exception.
* @param callable
* @param int|string|array
* @param string message
* @return null|Exception
*/
public static function error($function, $expectedType, $expectedMessage = NULL)
{
if (is_string($expectedType) && !preg_match('#^E_[A-Z_]+\\z#', $expectedType)) {
return static::exception($function, $expectedType, $expectedMessage);
}
$expected = is_array($expectedType) ? $expectedType : array(array($expectedType, $expectedMessage));
foreach ($expected as &$item) {
list($expectedType, $expectedMessage) = $item;
if (is_int($expectedType)) {
$item[2] = Helpers::errorTypeToString($expectedType);
} elseif (is_string($expectedType)) {
$item[0] = constant($item[2] = $expectedType);
} else {
throw new \Exception('Error type must be E_* constant.');
}
}
set_error_handler(function ($severity, $message, $file, $line) use(&$expected) {
if (($severity & error_reporting()) !== $severity) {
return;
}
$errorStr = Helpers::errorTypeToString($severity) . ($message ? " ({$message})" : '');
list($expectedType, $expectedMessage, $expectedTypeStr) = array_shift($expected);
if ($expectedType === NULL) {
restore_error_handler();
Assert::fail("Generated more errors than expected: {$errorStr} was generated in file {$file} on line {$line}");
} elseif ($severity !== $expectedType) {
restore_error_handler();
Assert::fail("{$expectedTypeStr} was expected, but {$errorStr} was generated in file {$file} on line {$line}");
} elseif ($expectedMessage && !Assert::isMatching($expectedMessage, $message)) {
restore_error_handler();
Assert::fail("{$expectedTypeStr} with a message matching %2 was expected but got %1", $message, $expectedMessage);
}
});
reset($expected);
call_user_func($function);
restore_error_handler();
if ($expected) {
self::fail('Error was expected, but was not generated');
}
}
示例2: noError
/**
* Checks if the function does neither generate PHP error nor throw Exception
* @param callable
* @param string
* @return null|\Exception
*/
public static function noError($function, $message = NULL)
{
self::$counter++;
set_error_handler(function ($severity, $msg, $file, $line) use(&$expected, $message) {
if (($severity & error_reporting()) !== $severity) {
return;
}
$errorStr = Helpers::errorTypeToString($severity) . ($msg ? " ({$msg})" : '');
restore_error_handler();
Assert::fail(($message ? "{$message}: " : '') . "occurred error {$errorStr}");
});
try {
call_user_func($function);
} catch (\Exception $e) {
if ($e instanceof AssertException) {
// assertion exception already thrown by error handler, re-throw
throw $e;
}
restore_error_handler();
Assert::fail(($message ? "{$message}: " : '') . sprintf('%s was thrown. Code: %d Message: %s', get_class($e), $e->getCode(), $e->getMessage()));
}
restore_error_handler();
}