From 4e00c950cc2d1fa13eaf3d663f2e6d8cbb93cb81 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 12 Feb 2021 23:23:34 +0100 Subject: [PATCH] Created ProcessRunnerTest --- CHANGELOG.md | 1 + module/CLI/src/Util/ProcessRunner.php | 10 +- module/CLI/test/Util/ProcessRunnerTest.php | 106 +++++++++++++++++++++ 3 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 module/CLI/test/Util/ProcessRunnerTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f00b680..78fe5104 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this ### Changed * [#977](https://github.com/shlinkio/shlink/issues/977) Migrated from `laminas/laminas-paginator` to `pagerfanta/core` to handle pagination. * [#986](https://github.com/shlinkio/shlink/issues/986) Updated official docker image to use PHP 8. +* [#1010](https://github.com/shlinkio/shlink/issues/1010) Increased timeout for database commands to 10 minutes. ### Deprecated * [#959](https://github.com/shlinkio/shlink/issues/959) Deprecated all command flags using camelCase format (like `--expirationDate`), adding kebab-case replacements for all of them (like `--expiration-date`). diff --git a/module/CLI/src/Util/ProcessRunner.php b/module/CLI/src/Util/ProcessRunner.php index 4da5cb9f..1a6b826e 100644 --- a/module/CLI/src/Util/ProcessRunner.php +++ b/module/CLI/src/Util/ProcessRunner.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Shlinkio\Shlink\CLI\Util; +use Closure; use Shlinkio\Shlink\CLI\Command\Util\LockedCommandConfig; use Symfony\Component\Console\Helper\DebugFormatterHelper; use Symfony\Component\Console\Helper\ProcessHelper; @@ -18,10 +19,14 @@ use function str_replace; class ProcessRunner implements ProcessRunnerInterface { private ProcessHelper $helper; + private Closure $createProcess; - public function __construct(ProcessHelper $helper) + public function __construct(ProcessHelper $helper, ?callable $createProcess = null) { $this->helper = $helper; + $this->createProcess = $createProcess !== null + ? Closure::fromCallable($createProcess) + : static fn (array $cmd) => new Process($cmd, null, null, null, LockedCommandConfig::DEFAULT_TTL); } public function run(OutputInterface $output, array $cmd): void @@ -32,7 +37,8 @@ class ProcessRunner implements ProcessRunnerInterface /** @var DebugFormatterHelper $formatter */ $formatter = $this->helper->getHelperSet()->get('debug_formatter'); - $process = new Process($cmd, null, null, null, LockedCommandConfig::DEFAULT_TTL); + /** @var Process $process */ + $process = ($this->createProcess)($cmd); if ($output->isVeryVerbose()) { $output->write( diff --git a/module/CLI/test/Util/ProcessRunnerTest.php b/module/CLI/test/Util/ProcessRunnerTest.php new file mode 100644 index 00000000..05ac5dd7 --- /dev/null +++ b/module/CLI/test/Util/ProcessRunnerTest.php @@ -0,0 +1,106 @@ +helper = $this->prophesize(ProcessHelper::class); + $this->formatter = $this->prophesize(DebugFormatterHelper::class); + $helperSet = $this->prophesize(HelperSet::class); + $helperSet->get('debug_formatter')->willReturn($this->formatter->reveal()); + $this->helper->getHelperSet()->willReturn($helperSet->reveal()); + $this->process = $this->prophesize(Process::class); + + $this->runner = new ProcessRunner($this->helper->reveal(), fn () => $this->process->reveal()); + $this->output = $this->prophesize(OutputInterface::class); + } + + /** @test */ + public function noMessagesAreWrittenWhenOutputIsNotVerbose(): void + { + $isVeryVerbose = $this->output->isVeryVerbose()->willReturn(false); + $isDebug = $this->output->isDebug()->willReturn(false); + $mustRun = $this->process->mustRun(Argument::cetera())->willReturn($this->process->reveal()); + + $this->runner->run($this->output->reveal(), []); + + $isVeryVerbose->shouldHaveBeenCalledTimes(2); + $isDebug->shouldHaveBeenCalledOnce(); + $mustRun->shouldHaveBeenCalledOnce(); + $this->process->isSuccessful()->shouldNotHaveBeenCalled(); + $this->process->getCommandLine()->shouldNotHaveBeenCalled(); + $this->output->write(Argument::cetera())->shouldNotHaveBeenCalled(); + $this->helper->wrapCallback(Argument::cetera())->shouldNotHaveBeenCalled(); + $this->formatter->start(Argument::cetera())->shouldNotHaveBeenCalled(); + $this->formatter->stop(Argument::cetera())->shouldNotHaveBeenCalled(); + } + + /** @test */ + public function someMessagesAreWrittenWhenOutputIsVerbose(): void + { + $isVeryVerbose = $this->output->isVeryVerbose()->willReturn(true); + $isDebug = $this->output->isDebug()->willReturn(false); + $mustRun = $this->process->mustRun(Argument::cetera())->willReturn($this->process->reveal()); + $isSuccessful = $this->process->isSuccessful()->willReturn(true); + $getCommandLine = $this->process->getCommandLine()->willReturn('true'); + $start = $this->formatter->start(Argument::cetera())->willReturn(''); + $stop = $this->formatter->stop(Argument::cetera())->willReturn(''); + + $this->runner->run($this->output->reveal(), []); + + $isVeryVerbose->shouldHaveBeenCalledTimes(2); + $isDebug->shouldHaveBeenCalledOnce(); + $mustRun->shouldHaveBeenCalledOnce(); + $this->output->write(Argument::cetera())->shouldHaveBeenCalledTimes(2); + $this->helper->wrapCallback(Argument::cetera())->shouldNotHaveBeenCalled(); + $isSuccessful->shouldHaveBeenCalledTimes(2); + $getCommandLine->shouldHaveBeenCalledOnce(); + $start->shouldHaveBeenCalledOnce(); + $stop->shouldHaveBeenCalledOnce(); + } + + /** @test */ + public function wrapsCallbackWhenOutputIsDebug(): void + { + $isVeryVerbose = $this->output->isVeryVerbose()->willReturn(false); + $isDebug = $this->output->isDebug()->willReturn(true); + $mustRun = $this->process->mustRun(Argument::cetera())->willReturn($this->process->reveal()); + $wrapCallback = $this->helper->wrapCallback(Argument::cetera())->willReturn(function (): void { + }); + + $this->runner->run($this->output->reveal(), []); + + $isVeryVerbose->shouldHaveBeenCalledTimes(2); + $isDebug->shouldHaveBeenCalledOnce(); + $mustRun->shouldHaveBeenCalledOnce(); + $wrapCallback->shouldHaveBeenCalledOnce(); + $this->process->isSuccessful()->shouldNotHaveBeenCalled(); + $this->process->getCommandLine()->shouldNotHaveBeenCalled(); + $this->output->write(Argument::cetera())->shouldNotHaveBeenCalled(); + $this->formatter->start(Argument::cetera())->shouldNotHaveBeenCalled(); + $this->formatter->stop(Argument::cetera())->shouldNotHaveBeenCalled(); + } +}