cb7f5eb7c4e1320523def9e5d867160d3af8c6b7
[yaffs-website] / web / core / modules / system / tests / src / Kernel / Scripts / DbCommandBaseTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\system\Kernel\Scripts\DbCommandBaseTest.
6  */
7
8 namespace Drupal\Tests\system\Kernel\Scripts;
9
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;
17
18 /**
19  * Test that the DbToolsApplication works correctly.
20  *
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.
23  *
24  * @group console
25  */
26 class DbCommandBaseTest extends KernelTestBase {
27
28   /**
29    * Test specifying a database key.
30    */
31   public function testSpecifyDatabaseKey() {
32     $command = new DbCommandBaseTester();
33     $command_tester = new CommandTester($command);
34
35     Database::addConnectionInfo('magic_db', 'default', Database::getConnectionInfo('default')['default']);
36
37     $command_tester->execute([
38       '--database' => 'magic_db'
39     ]);
40     $this->assertEquals('magic_db', $command->getDatabaseConnection($command_tester->getInput())->getKey(),
41        'Special db key is returned');
42   }
43
44   /**
45    * Invalid database names will throw a useful exception.
46    */
47   public function testSpecifyDatabaseDoesNotExist() {
48     $command = new DbCommandBaseTester();
49     $command_tester = new CommandTester($command);
50     $command_tester->execute([
51       '--database' => 'dne'
52     ]);
53     $this->setExpectedException(ConnectionNotDefinedException::class);
54     $command->getDatabaseConnection($command_tester->getInput());
55   }
56
57   /**
58    * Test supplying database connection as a url.
59    */
60   public function testSpecifyDbUrl() {
61     $connection_info = Database::getConnectionInfo('default')['default'];
62
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']
67     ]);
68     $this->assertEquals('db-tools', $command->getDatabaseConnection($command_tester->getInput())->getKey());
69
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']
73     ]);
74     $this->assertEquals('db-tools', $command->getDatabaseConnection($command_tester->getInput())->getKey());
75   }
76
77   /**
78    * Test specifying a prefix for different connections.
79    */
80   public function testPrefix() {
81     if (Database::getConnection()->driver() == 'sqlite') {
82       $this->markTestSkipped('SQLITE modifies the prefixes so we cannot effectively test it');
83     }
84
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',
91     ]);
92     $this->assertEquals('extra', $command->getDatabaseConnection($command_tester->getInput())->tablePrefix());
93
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',
98     ]);
99     $this->assertEquals('extra2', $command->getDatabaseConnection($command_tester->getInput())->tablePrefix());
100
101     // This breaks simpletest cleanup.
102     //    $command_tester->execute([
103     //      '--prefix' => 'notsimpletest',
104     //    ]);
105     //    $this->assertEquals('notsimpletest', $command->getDatabaseConnection($command_tester->getInput())->tablePrefix());
106   }
107
108 }
109
110 /**
111  * Concrete command implementation for testing base features.
112  */
113 class DbCommandBaseTester extends DbCommandBase {
114
115   /**
116    * {@inheritdoc}
117    */
118   public function configure() {
119     parent::configure();
120     $this->setName('test');
121   }
122
123   /**
124    * {@inheritdoc}
125    */
126   public function getDatabaseConnection(InputInterface $input) {
127     return parent::getDatabaseConnection($input);
128   }
129
130   /**
131    * {@inheritdoc}
132    */
133   protected function execute(InputInterface $input, OutputInterface $output) {
134     // Empty implementation for testing.
135   }
136
137 }