function BrowserTestBaseTest::testVarDump

Same name and namespace in other branches
  1. 9 core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php \Drupal\FunctionalTests\BrowserTestBaseTest::testVarDump()
  2. 10 core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php \Drupal\FunctionalTests\BrowserTestBaseTest::testVarDump()

Tests the dump() function provided by the var-dumper Symfony component.

File

core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php, line 608

Class

BrowserTestBaseTest
Tests BrowserTestBase functionality.

Namespace

Drupal\FunctionalTests

Code

public function testVarDump() : void {
  // Dump some variables.
  $object = (object) [
    'Aldebaran' => 'Betelgeuse',
  ];
  dump($object);
  dump('Alpheratz');
  $dumpString = json_encode(DebugDump::getDumps());
  $this->assertStringContainsString('BrowserTestBaseTest::testVarDump', $dumpString);
  $this->assertStringContainsString('Aldebaran', $dumpString);
  $this->assertStringContainsString('Betelgeuse', $dumpString);
  $this->assertStringContainsString('Alpheratz', $dumpString);
  // Visit a Drupal page with call to the dump() function to check that dump()
  // in site code produces output in the requested web page's HTML.
  $body = $this->drupalGet('test-page-var-dump');
  $this->assertSession()
    ->statusCodeEquals(200);
  // It is too strict to assert all properties of the Role and it is easy to
  // break if one of these properties gets removed or gets a new default
  // value. It should be sufficient to test just a couple of properties.
  $this->assertStringContainsString('<span class=sf-dump-note>', $body);
  $this->assertStringContainsString('  #<span class=sf-dump-protected title="Protected property">id</span>: "<span class=sf-dump-str title="9 characters">test_role</span>"', $body);
  $this->assertStringContainsString('  #<span class=sf-dump-protected title="Protected property">label</span>: "<span class=sf-dump-str title="9 characters">Test role</span>"', $body);
  $this->assertStringContainsString('  #<span class=sf-dump-protected title="Protected property">permissions</span>: []', $body);
  $this->assertStringContainsString('  #<span class=sf-dump-protected title="Protected property">uuid</span>: "', $body);
  $this->assertStringContainsString('</samp>}', $body);
  // Check that dump() in SUT did not leak into the test's dumps.
  $this->assertSame($dumpString, json_encode(DebugDump::getDumps()));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.