]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Merge pull request #2262 from wallabag/install-check-db-connection
authorNicolas LÅ“uillet <nicolas@loeuillet.org>
Sat, 3 Sep 2016 11:34:13 +0000 (13:34 +0200)
committerGitHub <noreply@github.com>
Sat, 3 Sep 2016 11:34:13 +0000 (13:34 +0200)
Add a check for the database connection

src/Wallabag/CoreBundle/Command/InstallCommand.php
tests/Wallabag/CoreBundle/Command/InstallCommandTest.php

index 813ac720de0c9d53c4a42c35d7bc49a76f2d2d48..035eb865575a038bd8c338c5635178c79bc702ac 100644 (file)
@@ -72,8 +72,10 @@ class InstallCommand extends ContainerAwareCommand
     {
         $this->defaultOutput->writeln('<info><comment>Step 1 of 5.</comment> Checking system requirements.</info>');
 
-        $fulfilled = true;
+        $rows = [];
 
+        // testing if database driver exists
+        $fulfilled = true;
         $label = '<comment>PDO Driver</comment>';
         $status = '<info>OK!</info>';
         $help = '';
@@ -84,7 +86,23 @@ class InstallCommand extends ContainerAwareCommand
             $help = 'Database driver "'.$this->getContainer()->getParameter('database_driver').'" is not installed.';
         }
 
-        $rows = [];
+        $rows[] = [$label, $status, $help];
+
+        // testing if connection to the database can be etablished
+        $label = '<comment>Database connection</comment>';
+        $status = '<info>OK!</info>';
+        $help = '';
+
+        try {
+            $this->getContainer()->get('doctrine')->getManager()->getConnection()->connect();
+        } catch (\Exception $e) {
+            if (false === strpos($e->getMessage(), 'Unknown database')) {
+                $fulfilled = false;
+                $status = '<error>ERROR!</error>';
+                $help = 'Can\'t connect to the database: '.$e->getMessage();
+            }
+        }
+
         $rows[] = [$label, $status, $help];
 
         foreach ($this->functionExists as $functionRequired) {
@@ -456,7 +474,7 @@ class InstallCommand extends ContainerAwareCommand
         }
 
         // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
-        if ('sqlite' == $schemaManager->getDatabasePlatform()->getName()) {
+        if ('sqlite' === $schemaManager->getDatabasePlatform()->getName()) {
             $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
 
             if (isset($params['path']) && file_exists($params['path'])) {
index 089a1c5fee9a36a2ec6d093d66373581648859d8..83f5bf2461cbb16d69f3cc90426602801f7782bb 100644 (file)
@@ -127,6 +127,12 @@ class InstallCommandTest extends WallabagCoreTestCase
 
     public function testRunInstallCommandWithDatabaseRemoved()
     {
+        // skipped SQLite check when database is removed because while testing for the connection,
+        // the driver will create the file (so the database) before testing if database exist
+        if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver) {
+            $this->markTestSkipped('SQLite spotted: can\'t test with database removed.');
+        }
+
         $application = new Application($this->getClient()->getKernel());
         $application->add(new DropDatabaseDoctrineCommand());