trait DevelDumperTestTrait

Same name in other branches
  1. 5.x tests/src/Kernel/DevelDumperTestTrait.php \Drupal\Tests\devel\Kernel\DevelDumperTestTrait

Provides a class for checking dumper output.

Hierarchy

File

tests/src/Kernel/DevelDumperTestTrait.php, line 8

Namespace

Drupal\Tests\devel\Kernel
View source
trait DevelDumperTestTrait {
    
    /**
     * Assertion for ensure dump content.
     *
     * Asserts that the string passed in input is equals to the string
     * representation of a variable obtained exporting the data.
     *
     * Use \Drupal\devel\DevelDumperManager::export().
     *
     * @param string $dump
     *   The string that contains the dump output to test.
     * @param mixed $data
     *   The variable to dump.
     * @param string $name
     *   (optional) The label to output before variable, defaults to NULL.
     * @param string $message
     *   (optional) A message to display with the assertion.
     */
    public function assertDumpExportEquals($dump, $data, $name = NULL, $message = '') {
        $output = $this->getDumperExportDump($data, $name);
        $this->assertEquals(rtrim($dump), $output, $message);
    }
    
    /**
     * Asserts that a haystack contains the dump export output.
     *
     * Use \Drupal\devel\DevelDumperManager::export().
     *
     * @param string $haystack
     *   The string that contains the dump output to test.
     * @param mixed $data
     *   The variable to dump.
     * @param string $name
     *   (optional) The label to output before variable, defaults to NULL.
     * @param string $message
     *   (optional) A message to display with the assertion.
     */
    public function assertContainsDumpExport($haystack, $data, $name = NULL, $message = '') {
        // As at 18.04.2020 assertContainsDumpExport() is not actually used in any
        // devel tests in any current code branch.
        $output = $this->getDumperExportDump($data, $name);
        $this->assertStringContainsString($output, (string) $haystack, $message);
    }
    
    /**
     * Assertion for ensure dump content.
     *
     * Asserts that the string passed in input is equals to the string
     * representation of a variable obtained dumping the data.
     *
     * Use \Drupal\devel\DevelDumperManager::dump().
     *
     * @param string $dump
     *   The string that contains the dump output to test.
     * @param mixed $data
     *   The variable to dump.
     * @param string $name
     *   (optional) The label to output before variable, defaults to NULL.
     * @param string $message
     *   (optional) A message to display with the assertion.
     */
    public function assertDumpEquals($dump, $data, $name = NULL, $message = '') {
        $output = $this->getDumperDump($data, $name);
        $this->assertEquals(rtrim($dump), $output, $message);
    }
    
    /**
     * Asserts that a haystack contains the dump output.
     *
     * Use \Drupal\devel\DevelDumperManager::dump().
     *
     * @param string $haystack
     *   The string that contains the dump output to test.
     * @param mixed $data
     *   The variable to dump.
     * @param string $name
     *   (optional) The label to output before variable, defaults to NULL.
     * @param string $message
     *   (optional) A message to display with the assertion.
     */
    public function assertContainsDump($haystack, $data, $name = NULL, $message = '') {
        $output = $this->getDumperDump($data, $name);
        $this->assertStringContainsString($output, (string) $haystack, $message);
    }
    
    /**
     * Returns a string representation of a variable.
     *
     * @param mixed $input
     *   The variable to dump.
     * @param string $name
     *   (optional) The label to output before variable, defaults to NULL.
     *
     * @return string
     *   String representation of a variable.
     *
     * @see \Drupal\devel\DevelDumperManager::export()
     */
    private function getDumperExportDump($input, $name = NULL) {
        $output = \Drupal::service('devel.dumper')->export($input, $name);
        return rtrim($output);
    }
    
    /**
     * Returns a string representation of a variable.
     *
     * @param mixed $input
     *   The variable to dump.
     * @param string $name
     *   (optional) The label to output before variable, defaults to NULL.
     *
     * @return string
     *   String representation of a variable.
     *
     * @see \Drupal\devel\DevelDumperManager::dump()
     */
    private function getDumperDump($input, $name = NULL) {
        ob_start();
        \Drupal::service('devel.dumper')->dump($input, $name);
        $output = ob_get_contents();
        ob_end_clean();
        return rtrim($output);
    }

}

Members

Title Sort descending Modifiers Object type Summary
DevelDumperTestTrait::assertContainsDump public function Asserts that a haystack contains the dump output.
DevelDumperTestTrait::assertContainsDumpExport public function Asserts that a haystack contains the dump export output.
DevelDumperTestTrait::assertDumpEquals public function Assertion for ensure dump content.
DevelDumperTestTrait::assertDumpExportEquals public function Assertion for ensure dump content.
DevelDumperTestTrait::getDumperDump private function Returns a string representation of a variable.
DevelDumperTestTrait::getDumperExportDump private function Returns a string representation of a variable.