From 270276a750c8648e2bbfe2c860833d8ec47bb667 Mon Sep 17 00:00:00 2001 From: Daniel Kesselberg Date: Thu, 27 Jun 2024 22:09:09 +0200 Subject: [PATCH] fix: use typed api for changing backgroundjobs_mode Signed-off-by: Daniel Kesselberg --- core/Command/Background/Ajax.php | 7 ++-- core/Command/Background/Base.php | 39 ++++++++------------ core/Command/Background/Cron.php | 4 +- core/Command/Background/WebCron.php | 7 ++-- tests/lib/Command/BackgroundJobsTest.php | 47 +++++++++++++++++------- 5 files changed, 58 insertions(+), 46 deletions(-) diff --git a/core/Command/Background/Ajax.php b/core/Command/Background/Ajax.php index 16486649bf039..5d4d0b1d957d1 100644 --- a/core/Command/Background/Ajax.php +++ b/core/Command/Background/Ajax.php @@ -1,4 +1,7 @@ * SPDX-License-Identifier: MIT @@ -6,7 +9,5 @@ namespace OC\Core\Command\Background; class Ajax extends Base { - protected function getMode(): string { - return 'ajax'; - } + // Implementation in OC\Core\Command\Background\Base } diff --git a/core/Command/Background/Base.php b/core/Command/Background/Base.php index e9946ad4cc701..eb4404625fc7c 100644 --- a/core/Command/Background/Base.php +++ b/core/Command/Background/Base.php @@ -1,50 +1,41 @@ * SPDX-License-Identifier: MIT */ namespace OC\Core\Command\Background; -use OCP\IConfig; +use OCP\IAppConfig; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -/** - * An abstract base class for configuring the background job mode - * from the command line interface. - * Subclasses will override the getMode() function to specify the mode to configure. - */ abstract class Base extends Command { - abstract protected function getMode(); + private string $mode; - public function __construct( - protected IConfig $config, + public function __construct(private IAppConfig $appConfig, ) { + $this->mode = match ($this::class) { + Ajax::class => 'ajax', + Cron::class => 'cron', + WebCron::class => 'webcron', + }; parent::__construct(); } protected function configure(): void { - $mode = $this->getMode(); $this - ->setName("background:$mode") - ->setDescription("Use $mode to run background jobs"); + ->setName('background:' . $this->mode) + ->setDescription('Use ' . $this->mode . ' to run background jobs'); } - /** - * Executing this command will set the background job mode for owncloud. - * The mode to set is specified by the concrete sub class by implementing the - * getMode() function. - * - * @param InputInterface $input - * @param OutputInterface $output - * @return int - */ protected function execute(InputInterface $input, OutputInterface $output): int { - $mode = $this->getMode(); - $this->config->setAppValue('core', 'backgroundjobs_mode', $mode); - $output->writeln("Set mode for background jobs to '$mode'"); + $this->appConfig->setValueString('core', 'backgroundjobs_mode', $this->mode); + $output->writeln("Set mode for background jobs to '" . $this->mode . "'"); return 0; } } diff --git a/core/Command/Background/Cron.php b/core/Command/Background/Cron.php index 9f010b456fa12..561e60e5e2e34 100644 --- a/core/Command/Background/Cron.php +++ b/core/Command/Background/Cron.php @@ -6,7 +6,5 @@ namespace OC\Core\Command\Background; class Cron extends Base { - protected function getMode(): string { - return 'cron'; - } + // Implementation in OC\Core\Command\Background\Base } diff --git a/core/Command/Background/WebCron.php b/core/Command/Background/WebCron.php index 1e3e5ca5f1731..e840aa60aacc4 100644 --- a/core/Command/Background/WebCron.php +++ b/core/Command/Background/WebCron.php @@ -1,4 +1,7 @@ * SPDX-License-Identifier: MIT @@ -6,7 +9,5 @@ namespace OC\Core\Command\Background; class WebCron extends Base { - protected function getMode(): string { - return 'webcron'; - } + // Implementation in OC\Core\Command\Background\Base } diff --git a/tests/lib/Command/BackgroundJobsTest.php b/tests/lib/Command/BackgroundJobsTest.php index 310f1057bb4d5..13006f68e37b6 100644 --- a/tests/lib/Command/BackgroundJobsTest.php +++ b/tests/lib/Command/BackgroundJobsTest.php @@ -1,4 +1,7 @@ @@ -9,30 +12,48 @@ use OC\Core\Command\Background\Ajax; use OC\Core\Command\Background\Cron; use OC\Core\Command\Background\WebCron; - +use OCP\IAppConfig; use Symfony\Component\Console\Input\StringInput; use Symfony\Component\Console\Output\NullOutput; use Test\TestCase; class BackgroundJobsTest extends TestCase { + private IAppConfig $appConfig; + + public function setUp(): void { + $this->appConfig = $this->createMock(IAppConfig::class); + } + public function testCronCommand() { - $config = \OC::$server->getConfig(); - $job = new Cron($config); - $job->run(new StringInput(''), new NullOutput()); - $this->assertEquals('cron', $config->getAppValue('core', 'backgroundjobs_mode')); + $this->appConfig->expects($this->once()) + ->method('setValueString') + ->with('core', 'backgroundjobs_mode', 'cron'); + + $job = new Cron($this->appConfig); + + $this->assertEquals('background:cron', $job->getName()); + $this->assertEquals(0, $job->run(new StringInput(''), new NullOutput())); } public function testAjaxCommand() { - $config = \OC::$server->getConfig(); - $job = new Ajax($config); - $job->run(new StringInput(''), new NullOutput()); - $this->assertEquals('ajax', $config->getAppValue('core', 'backgroundjobs_mode')); + $this->appConfig->expects($this->once()) + ->method('setValueString') + ->with('core', 'backgroundjobs_mode', 'ajax'); + + $job = new Ajax($this->appConfig); + + $this->assertEquals('background:ajax', $job->getName()); + $this->assertEquals(0, $job->run(new StringInput(''), new NullOutput())); } public function testWebCronCommand() { - $config = \OC::$server->getConfig(); - $job = new WebCron($config); - $job->run(new StringInput(''), new NullOutput()); - $this->assertEquals('webcron', $config->getAppValue('core', 'backgroundjobs_mode')); + $this->appConfig->expects($this->once()) + ->method('setValueString') + ->with('core', 'backgroundjobs_mode', 'webcron'); + + $job = new WebCron($this->appConfig); + + $this->assertEquals('background:webcron', $job->getName()); + $this->assertEquals(0, $job->run(new StringInput(''), new NullOutput())); } }