5 * Contains \Drupal\Tests\system\Kernel\Scripts\DbCommandBaseTest.
8 namespace Drupal\Tests\system\Kernel\Scripts;
10 use Drupal\Core\Command\DbCommandBase;
11 use Drupal\Core\Database\ConnectionNotDefinedException;
12 use Drupal\Core\Database\Database;
13 use Drupal\KernelTests\KernelTestBase;
14 use Symfony\Component\Console\Input\InputInterface;
15 use Symfony\Component\Console\Output\OutputInterface;
16 use Symfony\Component\Console\Tester\CommandTester;
19 * Test that the DbToolsApplication works correctly.
21 * The way console application's run it is impossible to test. For now we only
22 * test that we are registering the correct commands.
26 class DbCommandBaseTest extends KernelTestBase {
29 * Test specifying a database key.
31 public function testSpecifyDatabaseKey() {
32 $command = new DbCommandBaseTester();
33 $command_tester = new CommandTester($command);
35 Database::addConnectionInfo('magic_db', 'default', Database::getConnectionInfo('default')['default']);
37 $command_tester->execute([
38 '--database' => 'magic_db'
40 $this->assertEquals('magic_db', $command->getDatabaseConnection($command_tester->getInput())->getKey(),
41 'Special db key is returned');
45 * Invalid database names will throw a useful exception.
47 public function testSpecifyDatabaseDoesNotExist() {
48 $command = new DbCommandBaseTester();
49 $command_tester = new CommandTester($command);
50 $command_tester->execute([
53 $this->setExpectedException(ConnectionNotDefinedException::class);
54 $command->getDatabaseConnection($command_tester->getInput());
58 * Test supplying database connection as a url.
60 public function testSpecifyDbUrl() {
61 $connection_info = Database::getConnectionInfo('default')['default'];
63 $command = new DbCommandBaseTester();
64 $command_tester = new CommandTester($command);
65 $command_tester->execute([
66 '-db-url' => $connection_info['driver'] . '://' . $connection_info['username'] . ':' . $connection_info['password'] . '@' . $connection_info['host'] . '/' . $connection_info['database']
68 $this->assertEquals('db-tools', $command->getDatabaseConnection($command_tester->getInput())->getKey());
70 Database::removeConnection('db-tools');
71 $command_tester->execute([
72 '--database-url' => $connection_info['driver'] . '://' . $connection_info['username'] . ':' . $connection_info['password'] . '@' . $connection_info['host'] . '/' . $connection_info['database']
74 $this->assertEquals('db-tools', $command->getDatabaseConnection($command_tester->getInput())->getKey());
78 * Test specifying a prefix for different connections.
80 public function testPrefix() {
81 if (Database::getConnection()->driver() == 'sqlite') {
82 $this->markTestSkipped('SQLITE modifies the prefixes so we cannot effectively test it');
85 Database::addConnectionInfo('magic_db', 'default', Database::getConnectionInfo('default')['default']);
86 $command = new DbCommandBaseTester();
87 $command_tester = new CommandTester($command);
88 $command_tester->execute([
89 '--database' => 'magic_db',
90 '--prefix' => 'extra',
92 $this->assertEquals('extra', $command->getDatabaseConnection($command_tester->getInput())->tablePrefix());
94 $connection_info = Database::getConnectionInfo('default')['default'];
95 $command_tester->execute([
96 '-db-url' => $connection_info['driver'] . '://' . $connection_info['username'] . ':' . $connection_info['password'] . '@' . $connection_info['host'] . '/' . $connection_info['database'],
97 '--prefix' => 'extra2',
99 $this->assertEquals('extra2', $command->getDatabaseConnection($command_tester->getInput())->tablePrefix());
101 // This breaks simpletest cleanup.
102 // $command_tester->execute([
103 // '--prefix' => 'notsimpletest',
105 // $this->assertEquals('notsimpletest', $command->getDatabaseConnection($command_tester->getInput())->tablePrefix());
111 * Concrete command implementation for testing base features.
113 class DbCommandBaseTester extends DbCommandBase {
118 public function configure() {
120 $this->setName('test');
126 public function getDatabaseConnection(InputInterface $input) {
127 return parent::getDatabaseConnection($input);
133 protected function execute(InputInterface $input, OutputInterface $output) {
134 // Empty implementation for testing.