Unverified Commit f7dd26aa authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2795567 by joachim, jungle, daffie, Sophie.SK, mondrake, ravi.shankar,...

Issue #2795567 by joachim, jungle, daffie, Sophie.SK, mondrake, ravi.shankar, jonathanshaw, dawehner, AaronBauman, alexpott: Use Symfony's VarDumper for easier test debugging with dump()
parent c377d261
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@

namespace Drupal\test_page_test\Controller;

use Drupal\user\Entity\Role;

/**
 * Controller routines for test_page_test routes.
 */
@@ -23,4 +25,16 @@ public function testPage() {
    ];
  }

  /**
   * Returns a test page and with the call to the dump() function.
   */
  public function testPageVarDump() {
    $role = Role::create(['id' => 'test_role']);
    dump($role);
    return [
      '#title' => t('Test page with var dump'),
      '#markup' => t('Test page text.'),
    ];
  }

}
+8 −0
Original line number Diff line number Diff line
@@ -144,3 +144,11 @@ test_page_test.deprecations:
    _controller: '\Drupal\test_page_test\Controller\Test::deprecations'
  requirements:
    _access: 'TRUE'

test_page_test.test_page_var_dump:
  path: '/test-page-var-dump'
  defaults:
    _title: 'Test front page with var dump'
    _controller: '\Drupal\test_page_test\Controller\TestPageTestController::testPageVarDump'
  requirements:
    _access: 'TRUE'
+37 −0
Original line number Diff line number Diff line
@@ -8,7 +8,9 @@
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\StreamCapturer;
use Drupal\Tests\Traits\Core\CronRunTrait;
use Drupal\user\Entity\Role;
use PHPUnit\Framework\ExpectationFailedException;

/**
@@ -949,4 +951,39 @@ public function testDrupalGetHeader() {
    $this->drupalGetHeader('Content-Type');
  }

  /**
   * Tests the dump() function provided by the var-dumper Symfony component.
   */
  public function testVarDump() {
    // Append the stream capturer to the STDOUT stream, so that we can test the
    // dump() output and also prevent it from actually outputting in this
    // particular test.
    stream_filter_register("capture", StreamCapturer::class);
    stream_filter_append(STDOUT, "capture");

    // Dump some variables to check that dump() in test code produces output
    // on the command line that is running the test.
    $role = Role::load('authenticated');
    dump($role);
    dump($role->id());

    $this->assertStringContainsString('Drupal\user\Entity\Role', StreamCapturer::$cache);
    $this->assertStringContainsString('authenticated', StreamCapturer::$cache);

    // 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-const>null</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);
  }

}
+6 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@
use Drupal\Tests\TestRequirementsTrait;
use Drupal\Tests\Traits\PhpUnitWarnings;
use Drupal\TestTools\Comparator\MarkupInterfaceComparator;
use Drupal\TestTools\TestVarDumper;
use PHPUnit\Framework\Exception;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Reference;
@@ -30,6 +31,7 @@
use org\bovigo\vfs\visitor\vfsStreamPrintVisitor;
use Drupal\Core\Routing\RouteObjectInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\VarDumper\VarDumper;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;

/**
@@ -64,6 +66,9 @@
 * KernelTestBase::installEntitySchema(). Alternately, tests which need modules
 * to be fully installed could inherit from \Drupal\Tests\BrowserTestBase.
 *
 * Using Symfony's dump() function in Kernel tests will produce output on the
 * command line, whether the call to dump() is in test code or site code.
 *
 * @see \Drupal\Tests\KernelTestBase::$modules
 * @see \Drupal\Tests\KernelTestBase::enableModules()
 * @see \Drupal\Tests\KernelTestBase::installConfig()
@@ -223,6 +228,7 @@ abstract class KernelTestBase extends TestCase implements ServiceProviderInterfa
   */
  public static function setUpBeforeClass() {
    parent::setUpBeforeClass();
    VarDumper::setHandler(TestVarDumper::class . '::cliHandler');

    // Change the current dir to DRUPAL_ROOT.
    chdir(static::getDrupalRoot());
+22 −0
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@
use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\Core\Database\Database;
use GuzzleHttp\Exception\GuzzleException;
use Drupal\Tests\StreamCapturer;
use Drupal\user\Entity\Role;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\visitor\vfsStreamStructureVisitor;
use PHPUnit\Framework\SkippedTestError;
@@ -396,4 +398,24 @@ public function testKernelTestBaseInstallSchema() {
    $this->assertFalse(Database::getConnection()->schema()->tableExists('key_value'));
  }

  /**
   * Tests the dump() function provided by the var-dumper Symfony component.
   */
  public function testVarDump() {
    // Append the stream capturer to the STDOUT stream, so that we can test the
    // dump() output and also prevent it from actually outputting in this
    // particular test.
    stream_filter_register("capture", StreamCapturer::class);
    stream_filter_append(STDOUT, "capture");

    // Dump some variables.
    $this->enableModules(['system', 'user']);
    $role = Role::create(['id' => 'test_role']);
    dump($role);
    dump($role->id());

    $this->assertStringContainsString('Drupal\user\Entity\Role', StreamCapturer::$cache);
    $this->assertStringContainsString('test_role', StreamCapturer::$cache);
  }

}
Loading