class BootableCommandTraitTest
Tests the trait that helps console commands boot Drupal.
Attributes
#[Group('Command')]
#[CoversTrait(BootableCommandTrait::class)]
#[RequiresPhpExtension('pdo_sqlite')]
Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\Core\Command\BootableCommandTraitTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of BootableCommandTraitTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Command/ BootableCommandTraitTest.php, line 23
Namespace
Drupal\Tests\Core\CommandView source
class BootableCommandTraitTest extends UnitTestCase {
/**
* The class loader, which is needed to boot Drupal.
*/
private readonly object $classLoader;
/**
* A console application to manage the commands under test.
*/
private readonly Application $application;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
// To boot Drupal, we need a database. For the purposes of this test, an
// in-memory SQLite database is sufficient.
Database::addConnectionInfo('default', 'default', [
'driver' => 'sqlite',
'namespace' => 'Drupal\\sqlite\\Driver\\Database\\sqlite',
'autoload' => 'core/modules/sqlite/src/Driver/Database/sqlite/',
'database' => ':memory:',
]);
$class_loaders = ClassLoader::getRegisteredLoaders();
$this->classLoader = reset($class_loaders);
$this->application = new Application('drupal', \Drupal::VERSION);
}
/**
* Tests that commands are initialized with a reasonable base URL.
*/
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());
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
|---|---|---|---|---|
| BootableCommandTraitTest::$application | private | property | A console application to manage the commands under test. | |
| BootableCommandTraitTest::$classLoader | private | property | The class loader, which is needed to boot Drupal. | |
| BootableCommandTraitTest::setUp | protected | function | Overrides UnitTestCase::setUp | |
| BootableCommandTraitTest::testRequestUrlIsValid | public | function | Tests that commands are initialized with a reasonable base URL. | |
| ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | |
| ExpectDeprecationTrait::setUpErrorHandler | public | function | Sets up the test error handler. | |
| ExpectDeprecationTrait::tearDownErrorHandler | public | function | Tears down the test error handler. | |
| RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |
| RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |
| RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |
| RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |
| UnitTestCase::$root | protected | property | The app root. | |
| UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |
| UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | |
| UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |
| UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |
| UnitTestCase::setDebugDumpHandler | public static | function | Registers the dumper CLI handler when the DebugDump extension is enabled. | |
| UnitTestCase::setupMockIterator | protected | function | Set up a traversable class mock to return specific items when iterated. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.