lithium\test\Unit::assert()

public method

General assert method used by others for common output.

Parameters

  • boolean $expression
  • string|boolean $message

    The message to output. If the message is not a string, then it will be converted to '{:message}'. Use '{:message}' in the string and it will use the $data to format the message with Text::insert().

  • array $data

Returns

boolean

true if the assertion succeeded, false otherwise.

Source

	public function assert($expression, $message = false, $data = []) {
		if (!is_string($message)) {
			$message = '{:message}';
		}
		if (strpos($message, "{:message}") !== false) {
			$params = $data;
			$params['message'] = $this->_message($params);
			$message = Text::insert($message, $params);
		}
		$trace = Debugger::trace([
			'start' => 1, 'depth' => 4, 'format' => 'array', 'closures' => !$expression
		]);
		$methods = $this->methods();
		$i = 1;

		while ($i < count($trace)) {
			if (in_array($trace[$i]['function'], $methods) && $trace[$i - 1]['object'] == $this) {
				break;
			}
			$i++;
		}
		$class = isset($trace[$i - 1]['object']) ? get_class($trace[$i - 1]['object']) : null;
		$method = isset($trace[$i]) ? $trace[$i]['function'] : $trace[$i - 1]['function'];

		$result = compact('class', 'method', 'message', 'data') + [
			'file'      => $trace[$i - 1]['file'],
			'line'      => $trace[$i - 1]['line'],
			'assertion' => $trace[$i - 1]['function']
		];
		$this->_result($expression ? 'pass' : 'fail', $result);
		return $expression;
	}