From 7c42835cc1dc3df5068e296446fc986c034b3271 Mon Sep 17 00:00:00 2001
From: Alejandro Celaya <alejandro@alejandrocelaya.com>
Date: Thu, 18 Aug 2016 18:02:24 +0200
Subject: [PATCH] Improved error management on install command

---
 .../src/Command/Install/InstallCommand.php    | 31 +++++++++++++++----
 .../Command/Install/InstallCommandTest.php    |  5 ++-
 phpcs.xml                                     |  1 +
 3 files changed, 30 insertions(+), 7 deletions(-)

diff --git a/module/CLI/src/Command/Install/InstallCommand.php b/module/CLI/src/Command/Install/InstallCommand.php
index bb19514f..6df92f26 100644
--- a/module/CLI/src/Command/Install/InstallCommand.php
+++ b/module/CLI/src/Command/Install/InstallCommand.php
@@ -94,14 +94,33 @@ class InstallCommand extends Command
         $output->writeln(['<info>Custom configuration properly generated!</info>', '']);
 
         // Generate database
-        $output->write('Initializing database...');
-        $this->processHelper->run($output, 'php vendor/bin/doctrine.php orm:schema-tool:create');
-        $output->writeln(' <info>Success!</info>');
+        $output->writeln('Initializing database...');
+        $process = $this->processHelper->run($output, 'php vendor/bin/doctrine.php orm:schema-tool:create');
+        if ($process->isSuccessful()) {
+            $output->writeln('    <info>Success!</info>');
+        } else {
+            if ($output->isVerbose()) {
+                return;
+            }
+            $output->writeln(
+                '    <error>Error generating database.</error>  Run this command with -vvv to see specific error info.'
+            );
+            return;
+        }
 
         // Generate proxies
-        $output->write('Generating proxies...');
-        $this->processHelper->run($output, 'php vendor/bin/doctrine.php orm:generate-proxies');
-        $output->writeln(' <info>Success!</info>');
+        $output->writeln('Generating proxies...');
+        $process = $this->processHelper->run($output, 'php vendor/bin/doctrine.php orm:generate-proxies');
+        if ($process->isSuccessful()) {
+            $output->writeln('    <info>Success!</info>');
+        } else {
+            if ($output->isVerbose()) {
+                return;
+            }
+            $output->writeln(
+                '    <error>Error generating proxies.</error>  Run this command with -vvv to see specific error info.'
+            );
+        }
     }
 
     protected function askDatabase()
diff --git a/module/CLI/test/Command/Install/InstallCommandTest.php b/module/CLI/test/Command/Install/InstallCommandTest.php
index 6fb2ee79..846a083b 100644
--- a/module/CLI/test/Command/Install/InstallCommandTest.php
+++ b/module/CLI/test/Command/Install/InstallCommandTest.php
@@ -8,6 +8,7 @@ use Shlinkio\Shlink\CLI\Command\Install\InstallCommand;
 use Symfony\Component\Console\Application;
 use Symfony\Component\Console\Helper\ProcessHelper;
 use Symfony\Component\Console\Tester\CommandTester;
+use Symfony\Component\Process\Process;
 use Zend\Config\Writer\WriterInterface;
 
 class InstallCommandTest extends TestCase
@@ -23,10 +24,12 @@ class InstallCommandTest extends TestCase
 
     public function setUp()
     {
+        $processMock = $this->prophesize(Process::class);
+        $processMock->isSuccessful()->willReturn(true);
         $processHelper = $this->prophesize(ProcessHelper::class);
         $processHelper->getName()->willReturn('process');
         $processHelper->setHelperSet(Argument::any())->willReturn(null);
-        $processHelper->run(Argument::cetera())->willReturn(null);
+        $processHelper->run(Argument::cetera())->willReturn($processMock->reveal());
 
         $app = new Application();
         $helperSet = $app->getHelperSet();
diff --git a/phpcs.xml b/phpcs.xml
index 469dd989..5b686079 100644
--- a/phpcs.xml
+++ b/phpcs.xml
@@ -20,4 +20,5 @@
     <file>module</file>
     <file>config</file>
     <file>public/index.php</file>
+    <exclude-pattern>config/params/*</exclude-pattern>
 </ruleset>