6 * Tests sql-connect command
8 * Installs Drupal and checks that the given URL by sql-connect is correct.
13 class SqlConnectCase extends CommandUnishTestCase
16 public function testSqlConnect()
18 $this->setUpDrupal(1, true);
19 // Get the connection details with sql-connect and check its structure.
20 $this->drush('sql-connect');
21 $connectionString = $this->getOutput();
23 // Not all drivers need -e option like sqlite
24 $shell_options = "-e";
25 $db_driver = $this->dbDriver();
26 if ($db_driver == 'mysql') {
27 $this->assertRegExp('/^mysql --user=[^\s]+ --password=.* --database=[^\s]+ --host=[^\s]+/', $connectionString);
28 } elseif ($db_driver == 'sqlite') {
29 $this->assertContains('sqlite3', $connectionString);
31 } elseif ($db_driver == 'pgsql') {
32 $this->assertRegExp('/^psql -q --dbname=[^\s]+ --host=[^\s]+ --port=[^\s]+ --username=[^\s]+/', $connectionString);
34 $this->markTestSkipped('sql-connect test does not recognize database type in ' . self::getDbUrl());
37 // Issue a query and check the result to verify the connection.
38 $this->execute($connectionString . ' ' . $shell_options . ' "SELECT uid FROM users where uid = 1;"');
39 $output = $this->getOutput();
40 $this->assertContains('1', $output);
42 // Run 'core-status' and insure that we can bootstrap Drupal.
43 $this->drush('core-status', [], ['fields' => 'bootstrap']);
44 $output = $this->getOutput();
45 $this->assertContains('Successful', $output);
47 // Test to see if 'sql-create' can erase the database.
48 // The only output is a confirmation string, so we'll run
49 // other commands to confirm that this worked.
50 $this->drush('sql-create');
52 // Try to execute a query. This should give a "table not found" error.
53 $this->execute($connectionString . ' ' . $shell_options . ' "SELECT uid FROM users where uid = 1;"', self::EXIT_ERROR);
55 // We should still be able to run 'core-status' without getting an
56 // error, although Drupal should not bootstrap any longer.
57 $this->drush('core-status', [], ['fields' => 'bootstrap']);
58 $output = $this->getOutput();
59 $this->assertNotContains('Successful', $output);