X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fsystem%2Ftests%2Fsrc%2FKernel%2FScripts%2FDbCommandBaseTest.php;fp=web%2Fcore%2Fmodules%2Fsystem%2Ftests%2Fsrc%2FKernel%2FScripts%2FDbCommandBaseTest.php;h=cb7f5eb7c4e1320523def9e5d867160d3af8c6b7;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/system/tests/src/Kernel/Scripts/DbCommandBaseTest.php b/web/core/modules/system/tests/src/Kernel/Scripts/DbCommandBaseTest.php new file mode 100644 index 000000000..cb7f5eb7c --- /dev/null +++ b/web/core/modules/system/tests/src/Kernel/Scripts/DbCommandBaseTest.php @@ -0,0 +1,137 @@ +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' + ]); + $this->setExpectedException(ConnectionNotDefinedException::class); + $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()); + } + +} + +/** + * 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. + } + +}