3 namespace Drupal\Tests\devel\Kernel;
6 * Provides a class for checking dumper output.
8 trait DevelDumperTestTrait {
11 * Asserts that the string passed in input is equals to the string
12 * representation of a variable obtained exporting the data.
14 * Use \Drupal\devel\DevelDumperManager::export().
17 * The string that contains the dump output to test.
19 * The variable to dump.
21 * (optional) The label to output before variable, defaults to NULL.
22 * @param string $message
23 * (optional) A message to display with the assertion.
25 public function assertDumpExportEquals($dump, $data, $name = NULL, $message = '') {
26 $output = $this->getDumperExportDump($data, $name);
27 $this->assertEqual(rtrim($dump), $output, $message);
31 * Asserts that a haystack contains the dump export output.
33 * Use \Drupal\devel\DevelDumperManager::export().
36 * The string that contains the dump output to test.
38 * The variable to dump.
40 * (optional) The label to output before variable, defaults to NULL.
41 * @param string $message
42 * (optional) A message to display with the assertion.
44 public function assertContainsDumpExport($haystack, $data, $name = NULL, $message = '') {
45 $output = $this->getDumperExportDump($data, $name);
46 $this->assertContains($output, (string) $haystack, $message);
50 * Asserts that the string passed in input is equals to the string
51 * representation of a variable obtained dumping the data.
53 * Use \Drupal\devel\DevelDumperManager::dump().
56 * The string that contains the dump output to test.
58 * The variable to dump.
60 * (optional) The label to output before variable, defaults to NULL.
61 * @param string $message
62 * (optional) A message to display with the assertion.
64 public function assertDumpEquals($dump, $data, $name = NULL, $message = '') {
65 $output = $this->getDumperDump($data, $name);
66 $this->assertEqual(rtrim($dump), $output, $message);
70 * Asserts that a haystack contains the dump output.
72 * Use \Drupal\devel\DevelDumperManager::dump().
75 * The string that contains the dump output to test.
77 * The variable to dump.
79 * (optional) The label to output before variable, defaults to NULL.
80 * @param string $message
81 * (optional) A message to display with the assertion.
83 public function assertContainsDump($haystack, $data, $name = NULL, $message = '') {
84 $output = $this->getDumperDump($data, $name);
85 $this->assertContains($output, (string) $haystack, $message);
89 * Returns a string representation of a variable.
92 * The variable to dump.
94 * (optional) The label to output before variable, defaults to NULL.
97 * String representation of a variable.
99 * @see \Drupal\devel\DevelDumperManager::export()
101 private function getDumperExportDump($input, $name = NULL) {
102 $output = \Drupal::service('devel.dumper')->export($input, $name);
103 return rtrim($output);
107 * Returns a string representation of a variable.
109 * @param mixed $input
110 * The variable to dump.
111 * @param string $name
112 * (optional) The label to output before variable, defaults to NULL.
115 * String representation of a variable.
117 * @see \Drupal\devel\DevelDumperManager::dump()
119 private function getDumperDump($input, $name = NULL) {
121 \Drupal::service('devel.dumper')->dump($input, $name);
122 $output = ob_get_contents();
124 return rtrim($output);