Version 1
[yaffs-website] / vendor / drush / drush / tests / sqlConnectCreateTest.php
1 <?php
2
3 namespace Unish;
4
5 /**
6  * Tests sql-connect command
7  *
8  *   Installs Drupal and checks that the given URL by sql-connect is correct.
9  *
10  * @group commands
11  * @group sql
12  */
13 class SqlConnectCase extends CommandUnishTestCase {
14
15   function testSqlConnect() {
16     $sites = $this->setUpDrupal(1, TRUE);
17     $options = array(
18       'yes' => NULL,
19       'root' => $this->webroot(),
20       'uri' => key($sites),
21     );
22
23     // Get the connection details with sql-connect and check its structure.
24     $this->drush('sql-connect', array(), $options);
25     $connectionString = $this->getOutput();
26
27     // Not all drivers need -e option like sqlite
28     $shell_options = "-e";
29     $db_driver = $this->db_driver();
30     if ($db_driver == 'mysql') {
31       $this->assertRegExp('/^mysql --user=[^\s]+ --password=.* --database=[^\s]+ --host=[^\s]+/', $connectionString);
32     }
33     elseif ($db_driver == 'sqlite') {
34       $this->assertContains('sqlite3', $connectionString);
35       $shell_options = '';
36     }
37     elseif ($db_driver == 'pgsql') {
38       $this->assertRegExp('/^psql -q --dbname=[^\s]+ --host=[^\s]+ --port=[^\s]+ --username=[^\s]+/', $connectionString);
39     }
40     else {
41       $this->markTestSkipped('sql-connect test does not recognize database type in ' . UNISH_DB_URL);
42     }
43
44     // Issue a query and check the result to verify the connection.
45     $this->execute($connectionString . ' ' . $shell_options . ' "SELECT uid FROM users where uid = 1;"');
46     $output = $this->getOutput();
47     $this->assertContains('1', $output);
48
49     // Run 'core-status' and insure that we can bootstrap Drupal.
50     $this->drush('core-status', array("Drupal bootstrap"), $options);
51     $output = $this->getOutput();
52     $this->assertContains('Successful', $output);
53
54     // Test to see if 'sql-create' can erase the database.
55     // The only output is a confirmation string, so we'll run
56     // other commands to confirm that this worked.
57     $this->drush('sql-create', array(), $options);
58
59     // Try to execute a query.  This should give a "table not found" error.
60     $this->execute($connectionString . ' ' . $shell_options . ' "SELECT uid FROM users where uid = 1;"', self::EXIT_ERROR);
61
62     // We should still be able to run 'core-status' without getting an
63     // error, although Drupal should not bootstrap any longer.
64     $this->drush('core-status', array("Drupal bootstrap"), $options);
65     $output = $this->getOutput();
66     $this->assertNotContains('Successful', $output);
67   }
68 }