FUnit::val_to_string PHP Method

val_to_string() public static method

converts all known PHP types into a string representation. Generally would be less verbose with objects and arrays than FUnit::var_export() because it uses json_encode()
public static val_to_string ( mixed $val, integer $maxlen = 50 ) : string
$val mixed the value to get as a string rep
$maxlen integer if > 0, truncate the string rep (default 50)
return string
    public static function val_to_string($val, $maxlen = 50)
    {
        $type = gettype($val);
        switch ($type) {
            case "boolean":
                if ($val) {
                    $val = 'true';
                } else {
                    $val = 'false';
                }
                break;
            case "integer":
                $val = (string) $val;
                break;
            case "double":
                $val = (string) $val;
                break;
            case "string":
                $val = "'" . $val . "'";
                break;
            case "array":
                $val = json_encode($val);
                break;
            case "object":
                $val = get_class($val) . " " . json_encode($val);
                break;
            case "resource":
                $val = get_resource_type($val);
                break;
            case "NULL":
                $val = 'NULL';
                break;
            default:
                $val = "'" . (string) $val . "'";
        }
        return static::str_truncate("({$type})" . $val, $maxlen);
    }

Usage Example

Beispiel #1
0
 /**
  * @see \FUnit::add_assertion_result()
  */
 public function addAssertionResult($func_name, $func_args, $result, $file, $line, $fail_info, $msg = null, $expected_fail = false)
 {
     \FUnit::debug_out("Adding assertion result for '{$func_name}' to suite '" . $this->getName() . "'");
     $result = $result ? \FUnit::PASS : \FUnit::FAIL;
     $refl_meth = new \ReflectionMethod($func_name);
     $args_strs = array();
     foreach ($refl_meth->getParameters() as $key => $value) {
         $param = $value->name;
         if (array_key_exists($key, $func_args) && $param !== 'msg') {
             $param_val = \FUnit::val_to_string($func_args[$key]);
             $args_strs[] = "\${$param}={$param_val}";
         }
     }
     $this->tests[$this->current_test_name]['assertions'][] = compact('func_name', 'func_args', 'args_strs', 'result', 'msg', 'expected_fail', 'file', 'line', 'fail_info');
 }