Interim commit.
[yaffs-website] / web / modules / contrib / devel / tests / src / Kernel / DevelDumperTestTrait.php
1 <?php
2
3 namespace Drupal\Tests\devel\Kernel;
4
5 /**
6  * Provides a class for checking dumper output.
7  */
8 trait DevelDumperTestTrait {
9
10   /**
11    * Asserts that the string passed in input is equals to the string
12    * representation of a variable obtained exporting the data.
13    *
14    * Use \Drupal\devel\DevelDumperManager::export().
15    *
16    * @param $dump
17    *   The string that contains the dump output to test.
18    * @param $data
19    *   The variable to dump.
20    * @param null $name
21    *   (optional) The label to output before variable, defaults to NULL.
22    * @param string $message
23    *   (optional) A message to display with the assertion.
24    */
25   public function assertDumpExportEquals($dump, $data, $name = NULL, $message = '') {
26     $output = $this->getDumperExportDump($data, $name);
27     $this->assertEqual(rtrim($dump), $output, $message);
28   }
29
30   /**
31    * Asserts that a haystack contains the dump export output.
32    *
33    * Use \Drupal\devel\DevelDumperManager::export().
34    *
35    * @param $haystack
36    *   The string that contains the dump output to test.
37    * @param $data
38    *   The variable to dump.
39    * @param null $name
40    *   (optional) The label to output before variable, defaults to NULL.
41    * @param string $message
42    *   (optional) A message to display with the assertion.
43    */
44   public function assertContainsDumpExport($haystack, $data, $name = NULL, $message = '') {
45     $output = $this->getDumperExportDump($data, $name);
46     $this->assertContains($output, (string) $haystack, $message);
47   }
48
49   /**
50    * Asserts that the string passed in input is equals to the string
51    * representation of a variable obtained dumping the data.
52    *
53    * Use \Drupal\devel\DevelDumperManager::dump().
54    *
55    * @param $dump
56    *   The string that contains the dump output to test.
57    * @param $data
58    *   The variable to dump.
59    * @param null $name
60    *   (optional) The label to output before variable, defaults to NULL.
61    * @param string $message
62    *   (optional) A message to display with the assertion.
63    */
64   public function assertDumpEquals($dump, $data, $name = NULL, $message = '') {
65     $output = $this->getDumperDump($data, $name);
66     $this->assertEqual(rtrim($dump), $output, $message);
67   }
68
69   /**
70    * Asserts that a haystack contains the dump output.
71    *
72    * Use \Drupal\devel\DevelDumperManager::dump().
73    *
74    * @param $haystack
75    *   The string that contains the dump output to test.
76    * @param $data
77    *   The variable to dump.
78    * @param null $name
79    *   (optional) The label to output before variable, defaults to NULL.
80    * @param string $message
81    *   (optional) A message to display with the assertion.
82    */
83   public function assertContainsDump($haystack, $data, $name = NULL, $message = '') {
84     $output = $this->getDumperDump($data, $name);
85     $this->assertContains($output, (string) $haystack, $message);
86   }
87
88   /**
89    * Returns a string representation of a variable.
90    *
91    * @param mixed $input
92    *   The variable to dump.
93    * @param string $name
94    *   (optional) The label to output before variable, defaults to NULL.
95    *
96    * @return string
97    *   String representation of a variable.
98    *
99    * @see \Drupal\devel\DevelDumperManager::export()
100    */
101   private function getDumperExportDump($input, $name = NULL) {
102     $output = \Drupal::service('devel.dumper')->export($input, $name);
103     return rtrim($output);
104   }
105
106   /**
107    * Returns a string representation of a variable.
108    *
109    * @param mixed $input
110    *   The variable to dump.
111    * @param string $name
112    *   (optional) The label to output before variable, defaults to NULL.
113    *
114    * @return string
115    *   String representation of a variable.
116    *
117    * @see \Drupal\devel\DevelDumperManager::dump()
118    */
119   private function getDumperDump($input, $name = NULL) {
120     ob_start();
121     \Drupal::service('devel.dumper')->dump($input, $name);
122     $output = ob_get_contents();
123     ob_end_clean();
124     return rtrim($output);
125   }
126
127 }