function BootableCommandTraitTest::testRequestUrlIsValid

Tests that commands are initialized with a reasonable base URL.

File

core/tests/Drupal/Tests/Core/Command/BootableCommandTraitTest.php, line 60

Class

BootableCommandTraitTest
Tests the trait that helps console commands boot Drupal.

Namespace

Drupal\Tests\Core\Command

Code

public function testRequestUrlIsValid() : void {
  // Create a fake command that boots Drupal and outputs the base URL.
  $this->application
    ->add(new class ($this->classLoader) extends Command {
    use BootableCommandTrait;
    public function __construct(object $classLoader) {
      parent::__construct('test');
      $this->classLoader = $classLoader;
    }
    
    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output) : int {
      $this->boot();
      $output->write($GLOBALS['base_url']);
      // Symfony Console apparently changes the error and exception handlers,
      // which will anger PHPUnit.
      restore_error_handler();
      restore_exception_handler();
      return 0;
    }

});
  $tester = new CommandTester($this->application
    ->find('test'));
  $tester->execute([]);
  $this->assertSame('http://default', $tester->getDisplay());
}

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