Generate a string representation of arbitrary data.
public static toString ( string $value, array $options = [] ) : string | ||
$value | string | The data to dump in string. |
$options | array | Available options are: - `'quote'` : dump will quote string data if true (default `true`). - `'object'`: dump options for objects. - `'method'`: default method to call on string instance (default `__toString`). - `'array'` : dump options for arrays. - `'indent'`: level of indent (defaults to `1`). - `'char'`: indentation character. - `'multiplier'`: number of indentation character per indent (default `4`) |
return | string | The dumped string. |
/** * Callback called after a spec execution. * * @param object $log The log object of the whole spec. */ public function specEnd($log = null) { $isOk = $log->passed() ? "ok" : "not ok"; switch ($log->type()) { case 'skipped': case 'pending': case 'excluded': $prefix = "# {$log->type()} "; break; default: $prefix = '- '; break; } $message = $prefix . trim(implode(" ", $log->messages())); $this->_counter++; $this->write("{$isOk} {$this->_counter} {$message}\n"); if ($exception = $log->exception()) { $this->write('# Exception: `' . get_class($exception) . '` Code(' . $exception->getCode() . '):' . "\n"); $this->write('# Message: ' . $exception->getMessage() . "\n"); } else { foreach ($log->children() as $log) { if ($log->passed()) { continue; } $toString = function ($instance) { return 'an instance of `' . get_class($instance) . '`'; }; foreach ($log->data() as $key => $value) { $key = ucfirst($key); $value = Text::toString($value, ['object' => ['method' => $toString]]); $this->write("# {$key}: {$value}\n"); } } } }
/** * Callback called at the end of specs processing. * * @param object $summary The execution summary instance. */ public function end($summary) { $toString = function ($instance) { return 'an instance of `' . get_class($instance) . '`'; }; foreach ($summary->logs() as $log) { if ($log->passed()) { continue; } switch ($log->type()) { case 'failed': foreach ($log->children() as $log) { if ($log->passed()) { continue; } $data = []; foreach ($log->data() as $key => $value) { $data[$key] = Text::toString($value, ['object' => ['method' => $toString]]); } $this->_json['errors'][] = ['spec' => trim(implode(' ', $log->messages())), 'suite' => $log->file(), 'data' => $data]; } break; case 'errored': $exception = $log->exception(); $this->_json['errors'][] = ['spec' => trim(implode(' ', $log->messages())), 'suite' => $log->file(), 'exception' => '`' . get_class($exception) . '` Code(' . $exception->getCode() . ')', 'trace' => $exception->getMessage()]; break; } } $this->_json['summary'] = ['total' => $summary->total(), 'passed' => $summary->passed(), 'pending' => $summary->pending(), 'skipped' => $summary->skipped(), 'excluded' => $summary->excluded(), 'failed' => $summary->failed(), 'errored' => $summary->errored()]; $this->write(json_encode($this->_json)); }
it("exports an array", function () { $dump = Text::toString(['Hello', 'World'], ['quote' => "'"]); $this->expect($dump)->toBe("[\n 0 => 'Hello',\n 1 => 'World'\n]"); }); it("exports an nested array", function () { $dump = Text::toString([['Hello'], ['World']], ['quote' => "'"]); $this->expect($dump)->toBe("[\n 0 => [\n 0 => 'Hello'\n ],\n 1 => [\n 0 => 'World'\n ]\n]"); }); it("exports an array using string as key", function () { $dump = Text::toString(['Hello' => 'World'], ['quote' => "'"]); $this->expect($dump)->toBe("[\n 'Hello' => 'World'\n]"); }); }); context("with no quote", function () { it("exports a string to a non quoted string dump", function () { $dump = Text::toString('Hello', ['quote' => false]); $this->expect($dump)->toBe('Hello'); }); }); }); describe("::dump()", function () { it("dumps null to a string dump", function () { $dump = Text::dump(null); $this->expect($dump)->toBe("null"); }); it("dumps booleans to a string dump", function () { $dump = Text::dump(true); $this->expect($dump)->toBe("true"); $dump = Text::dump(false); $this->expect($dump)->toBe("false"); });
/** * Prints diff of spec's params. * * @param array $report A report array. */ protected function _reportDiff($report) { $params = $report->params(); foreach ($params as $key => $value) { if (preg_match('~actual~', $key)) { $this->write("{$key}:\n", 'yellow'); $this->prefix($this->format(' ', 'n;;91') . ' '); } elseif (preg_match('~expected~', $key)) { $this->write("{$key}:\n", 'yellow'); $this->prefix($this->format(' ', 'n;;92') . ' '); } else { $this->write("{$key}:\n", 'yellow'); } $type = gettype($value); $toString = function ($instance) { return 'an instance of `' . get_class($instance) . '`'; }; $this->write("({$type}) " . Text::toString($value, ['object' => ['method' => $toString]])); $this->prefix(''); $this->write("\n"); } $this->write("\n"); }