lithium\test\Unit::assertNotException()

public method

Assert that the code passed in a closure does not throw an exception matching the passed expected exception.

The value passed to exepected is either an exception class name or the expected message.

Parameters

  • mixed $expected

    A string indicating what the error text is not expected to be. This can be an exact string, a /-delimited regular expression, or true, indicating that any error text is acceptable.

  • \Closure $closure

    A closure containing the code that should throw the exception.

  • string $message

Returns

boolean

true if the assertion succeeded, false otherwise.

Source

	public function assertNotException($expected, $closure, $message = '{:message}') {
		try {
			$closure();
		} catch (Exception $e) {
			$class = get_class($e);
			$eMessage = $e->getMessage();
			if (is_a($e, $expected)) {
				$result = $class;
				return $this->assert(false, $message, compact('expected', 'result'));
			}
			if ($eMessage === $expected) {
				$result = $eMessage;
				return $this->assert(false, $message, compact('expected', 'result'));
			}
			if (Validator::isRegex($expected) && preg_match($expected, $eMessage)) {
				$result = $eMessage;
				return $this->assert(false, $message, compact('expected', 'result'));
			}
		}
		$message = sprintf('Exception "%s" was not expected.', $expected);
		return $this->assert(true, $message, compact('expected', 'result'));
	}