Abstracted filesystem manipulation in InstallCommand

This commit is contained in:
Alejandro Celaya 2017-07-04 20:14:22 +02:00
parent 102f5c4e12
commit dcc09975a9
4 changed files with 34 additions and 10 deletions

View File

@ -2,6 +2,7 @@
<?php
use Shlinkio\Shlink\CLI\Command\Install\InstallCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Filesystem\Filesystem;
use Zend\Config\Writer\PhpArray;
chdir(dirname(__DIR__));
@ -9,7 +10,7 @@ chdir(dirname(__DIR__));
require __DIR__ . '/../vendor/autoload.php';
$app = new Application();
$command = new InstallCommand(new PhpArray());
$command = new InstallCommand(new PhpArray(), new Filesystem());
$app->add($command);
$app->setDefaultCommand($command->getName());
$app->run();

View File

@ -2,6 +2,7 @@
<?php
use Shlinkio\Shlink\CLI\Command\Install\InstallCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Filesystem\Filesystem;
use Zend\Config\Writer\PhpArray;
chdir(dirname(__DIR__));
@ -9,7 +10,7 @@ chdir(dirname(__DIR__));
require __DIR__ . '/../vendor/autoload.php';
$app = new Application();
$command = new InstallCommand(new PhpArray(), true);
$command = new InstallCommand(new PhpArray(), new Filesystem(), true);
$app->add($command);
$app->setDefaultCommand($command->getName());
$app->run();

View File

@ -14,6 +14,8 @@ use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Zend\Config\Writer\WriterInterface;
class InstallCommand extends Command
@ -56,18 +58,24 @@ class InstallCommand extends Command
* @var bool
*/
private $isUpdate;
/**
* @var Filesystem
*/
private $filesystem;
/**
* InstallCommand constructor.
* @param WriterInterface $configWriter
* @param Filesystem $filesystem
* @param bool $isUpdate
* @throws LogicException
*/
public function __construct(WriterInterface $configWriter, $isUpdate = false)
public function __construct(WriterInterface $configWriter, Filesystem $filesystem, $isUpdate = false)
{
parent::__construct();
$this->configWriter = $configWriter;
$this->isUpdate = $isUpdate;
$this->filesystem = $filesystem;
}
public function configure()
@ -89,15 +97,19 @@ class InstallCommand extends Command
]);
// Check if a cached config file exists and drop it if so
if (file_exists('data/cache/app_config.php')) {
if ($this->filesystem->exists('data/cache/app_config.php')) {
$output->write('Deleting old cached config...');
if (unlink('data/cache/app_config.php')) {
try {
$this->filesystem->remove('data/cache/app_config.php');
$output->writeln(' <info>Success</info>');
} else {
} catch (IOException $e) {
$output->writeln(
' <error>Failed!</error> You will have to manually delete the data/cache/app_config.php file to get'
. ' new config applied.'
);
if ($output->isVerbose()) {
$this->getApplication()->renderException($e, $output);
}
return;
}
}
@ -158,9 +170,11 @@ class InstallCommand extends Command
// Ask the user for the older shlink path
$keepAsking = true;
do {
$this->importedInstallationPath = $this->ask('Previous shlink installation path from which to import config');
$this->importedInstallationPath = $this->ask(
'Previous shlink installation path from which to import config'
);
$configFile = $this->importedInstallationPath . '/' . self::GENERATED_CONFIG_PATH;
$configExists = file_exists($configFile);
$configExists = $this->filesystem->exists($configFile);
if (! $configExists) {
$keepAsking = $this->questionHelper->ask($this->input, $this->output, new ConfirmationQuestion(
@ -191,7 +205,7 @@ class InstallCommand extends Command
if ($keepConfig) {
// If the user selected to keep DB config and is configured to use sqlite, copy DB file
if ($config->getDatabase()['DRIVER'] === self::DATABASE_DRIVERS['SQLite']) {
copy(
$this->filesystem->copy(
$this->importedInstallationPath . '/' . CustomizableAppConfig::SQLITE_DB_PATH,
CustomizableAppConfig::SQLITE_DB_PATH
);

View File

@ -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\Filesystem\Filesystem;
use Symfony\Component\Process\Process;
use Zend\Config\Writer\WriterInterface;
@ -21,6 +22,10 @@ class InstallCommandTest extends TestCase
* @var ObjectProphecy
*/
protected $configWriter;
/**
* @var ObjectProphecy
*/
protected $filesystem;
public function setUp()
{
@ -31,13 +36,16 @@ class InstallCommandTest extends TestCase
$processHelper->setHelperSet(Argument::any())->willReturn(null);
$processHelper->run(Argument::cetera())->willReturn($processMock->reveal());
$this->filesystem = $this->prophesize(Filesystem::class);
$this->filesystem->exists(Argument::cetera())->willReturn(false);
$app = new Application();
$helperSet = $app->getHelperSet();
$helperSet->set($processHelper->reveal());
$app->setHelperSet($helperSet);
$this->configWriter = $this->prophesize(WriterInterface::class);
$command = new InstallCommand($this->configWriter->reveal());
$command = new InstallCommand($this->configWriter->reveal(), $this->filesystem->reveal());
$app->add($command);
$questionHelper = $command->getHelper('question');