shlink/module/Installer/test/Command/InstallCommandTest.php

148 lines
4.9 KiB
PHP
Raw Normal View History

2016-08-15 12:34:35 +03:00
<?php
2017-10-12 11:13:20 +03:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Installer\Command;
2016-08-15 12:34:35 +03:00
2017-03-24 22:34:18 +03:00
use PHPUnit\Framework\TestCase;
2016-08-15 12:34:35 +03:00
use Prophecy\Argument;
2017-07-06 14:38:15 +03:00
use Prophecy\Prophecy\MethodProphecy;
2016-08-15 12:34:35 +03:00
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Installer\Command\InstallCommand;
use Shlinkio\Shlink\Installer\Config\ConfigCustomizerManagerInterface;
use Shlinkio\Shlink\Installer\Config\Plugin\ConfigCustomizerInterface;
2016-08-15 12:34:35 +03:00
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\ProcessHelper;
use Symfony\Component\Console\Tester\CommandTester;
2017-07-06 14:38:15 +03:00
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;
2016-08-15 12:34:35 +03:00
use Zend\Config\Writer\WriterInterface;
class InstallCommandTest extends TestCase
{
2017-07-06 14:38:15 +03:00
/**
* @var InstallCommand
*/
protected $command;
2016-08-15 12:34:35 +03:00
/**
* @var CommandTester
*/
protected $commandTester;
/**
* @var ObjectProphecy
*/
protected $configWriter;
/**
* @var ObjectProphecy
*/
protected $filesystem;
2016-08-15 12:34:35 +03:00
public function setUp()
{
$processMock = $this->prophesize(Process::class);
$processMock->isSuccessful()->willReturn(true);
2016-08-15 12:34:35 +03:00
$processHelper = $this->prophesize(ProcessHelper::class);
$processHelper->getName()->willReturn('process');
$processHelper->setHelperSet(Argument::any())->willReturn(null);
$processHelper->run(Argument::cetera())->willReturn($processMock->reveal());
2016-08-15 12:34:35 +03:00
$this->filesystem = $this->prophesize(Filesystem::class);
$this->filesystem->exists(Argument::cetera())->willReturn(false);
2017-07-06 11:04:35 +03:00
$this->configWriter = $this->prophesize(WriterInterface::class);
$configCustomizer = $this->prophesize(ConfigCustomizerInterface::class);
$configCustomizers = $this->prophesize(ConfigCustomizerManagerInterface::class);
2017-07-06 11:04:35 +03:00
$configCustomizers->get(Argument::cetera())->willReturn($configCustomizer->reveal());
$finder = $this->prophesize(PhpExecutableFinder::class);
$finder->find(false)->willReturn('php');
2016-08-15 12:34:35 +03:00
$app = new Application();
$helperSet = $app->getHelperSet();
$helperSet->set($processHelper->reveal());
$app->setHelperSet($helperSet);
2017-07-06 14:38:15 +03:00
$this->command = new InstallCommand(
2017-07-06 11:04:35 +03:00
$this->configWriter->reveal(),
$this->filesystem->reveal(),
$configCustomizers->reveal(),
false,
$finder->reveal()
2017-07-06 11:04:35 +03:00
);
2017-07-06 14:38:15 +03:00
$app->add($this->command);
2016-08-15 12:34:35 +03:00
2017-07-06 14:38:15 +03:00
$this->commandTester = new CommandTester($this->command);
2016-08-15 12:34:35 +03:00
}
/**
* @test
*/
2017-07-06 14:38:15 +03:00
public function generatedConfigIsProperlyPersisted()
2016-08-15 12:34:35 +03:00
{
2017-07-06 11:04:35 +03:00
$this->configWriter->toFile(Argument::any(), Argument::type('array'), false)->shouldBeCalledTimes(1);
2017-07-06 14:38:15 +03:00
$this->commandTester->execute([]);
}
/**
* @test
*/
public function cachedConfigIsDeletedIfExists()
{
/** @var MethodProphecy $appConfigExists */
$appConfigExists = $this->filesystem->exists('data/cache/app_config.php')->willReturn(true);
/** @var MethodProphecy $appConfigRemove */
$appConfigRemove = $this->filesystem->remove('data/cache/app_config.php')->willReturn(null);
2017-07-06 14:38:15 +03:00
$this->commandTester->execute([]);
$appConfigExists->shouldHaveBeenCalledTimes(1);
$appConfigRemove->shouldHaveBeenCalledTimes(1);
}
/**
* @test
*/
public function exceptionWhileDeletingCachedConfigCancelsProcess()
{
/** @var MethodProphecy $appConfigExists */
$appConfigExists = $this->filesystem->exists('data/cache/app_config.php')->willReturn(true);
/** @var MethodProphecy $appConfigRemove */
$appConfigRemove = $this->filesystem->remove('data/cache/app_config.php')->willThrow(IOException::class);
/** @var MethodProphecy $configToFile */
$configToFile = $this->configWriter->toFile(Argument::cetera())->willReturn(true);
$this->commandTester->execute([]);
$appConfigExists->shouldHaveBeenCalledTimes(1);
$appConfigRemove->shouldHaveBeenCalledTimes(1);
$configToFile->shouldNotHaveBeenCalled();
}
/**
* @test
*/
public function whenCommandIsUpdatePreviousConfigCanBeImported()
{
$ref = new \ReflectionObject($this->command);
$prop = $ref->getProperty('isUpdate');
$prop->setAccessible(true);
$prop->setValue($this->command, true);
/** @var MethodProphecy $importedConfigExists */
$importedConfigExists = $this->filesystem->exists(
__DIR__ . '/../../test-resources/' . InstallCommand::GENERATED_CONFIG_PATH
2017-07-06 14:38:15 +03:00
)->willReturn(true);
$this->commandTester->setInputs([
'',
'/foo/bar/wrong_previous_shlink',
'',
__DIR__ . '/../../test-resources',
2016-08-15 12:34:35 +03:00
]);
2017-07-06 14:38:15 +03:00
$this->commandTester->execute([]);
$importedConfigExists->shouldHaveBeenCalled();
2016-08-15 12:34:35 +03:00
}
}