Newer
Older
Angie Byron
committed
<?php
/**
* @file
* Contains \Drupal\Tests\system\Kernel\Scripts\DbCommandBaseTest.
*/
namespace Drupal\Tests\system\Kernel\Scripts;
use Drupal\Core\Command\DbCommandBase;
Alex Pott
committed
use Drupal\Core\Database\ConnectionNotDefinedException;
Angie Byron
committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use Drupal\Core\Database\Database;
use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
/**
* Test that the DbToolsApplication works correctly.
*
* The way console application's run it is impossible to test. For now we only
* test that we are registering the correct commands.
*
* @group console
*/
class DbCommandBaseTest extends KernelTestBase {
/**
* Test specifying a database key.
*/
public function testSpecifyDatabaseKey() {
$command = new DbCommandBaseTester();
$command_tester = new CommandTester($command);
Database::addConnectionInfo('magic_db', 'default', Database::getConnectionInfo('default')['default']);
$command_tester->execute([
'--database' => 'magic_db'
]);
$this->assertEquals('magic_db', $command->getDatabaseConnection($command_tester->getInput())->getKey(),
'Special db key is returned');
}
/**
* Invalid database names will throw a useful exception.
*/
public function testSpecifyDatabaseDoesNotExist() {
$command = new DbCommandBaseTester();
$command_tester = new CommandTester($command);
$command_tester->execute([
'--database' => 'dne'
]);
Alex Pott
committed
$this->setExpectedException(ConnectionNotDefinedException::class);
Angie Byron
committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
$command->getDatabaseConnection($command_tester->getInput());
}
/**
* Test supplying database connection as a url.
*/
public function testSpecifyDbUrl() {
$connection_info = Database::getConnectionInfo('default')['default'];
$command = new DbCommandBaseTester();
$command_tester = new CommandTester($command);
$command_tester->execute([
'-db-url' => $connection_info['driver'] . '://' . $connection_info['username'] . ':' . $connection_info['password'] . '@' . $connection_info['host'] . '/' . $connection_info['database']
]);
$this->assertEquals('db-tools', $command->getDatabaseConnection($command_tester->getInput())->getKey());
Database::removeConnection('db-tools');
$command_tester->execute([
'--database-url' => $connection_info['driver'] . '://' . $connection_info['username'] . ':' . $connection_info['password'] . '@' . $connection_info['host'] . '/' . $connection_info['database']
]);
$this->assertEquals('db-tools', $command->getDatabaseConnection($command_tester->getInput())->getKey());
}
/**
* Test specifying a prefix for different connections.
*/
public function testPrefix() {
if (Database::getConnection()->driver() == 'sqlite') {
$this->markTestSkipped('SQLITE modifies the prefixes so we cannot effectively test it');
}
Database::addConnectionInfo('magic_db', 'default', Database::getConnectionInfo('default')['default']);
$command = new DbCommandBaseTester();
$command_tester = new CommandTester($command);
$command_tester->execute([
'--database' => 'magic_db',
'--prefix' => 'extra',
]);
$this->assertEquals('extra', $command->getDatabaseConnection($command_tester->getInput())->tablePrefix());
$connection_info = Database::getConnectionInfo('default')['default'];
$command_tester->execute([
'-db-url' => $connection_info['driver'] . '://' . $connection_info['username'] . ':' . $connection_info['password'] . '@' . $connection_info['host'] . '/' . $connection_info['database'],
'--prefix' => 'extra2',
]);
$this->assertEquals('extra2', $command->getDatabaseConnection($command_tester->getInput())->tablePrefix());
// This breaks simpletest cleanup.
// $command_tester->execute([
// '--prefix' => 'notsimpletest',
// ]);
// $this->assertEquals('notsimpletest', $command->getDatabaseConnection($command_tester->getInput())->tablePrefix());
Angie Byron
committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
}
}
/**
* Concrete command implementation for testing base features.
*/
class DbCommandBaseTester extends DbCommandBase {
/**
* {@inheritdoc}
*/
public function configure() {
parent::configure();
$this->setName('test');
}
/**
* {@inheritdoc}
*/
public function getDatabaseConnection(InputInterface $input) {
return parent::getDatabaseConnection($input);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output) {
// Empty implementation for testing.
}
}