Skip to content

Commit

Permalink
fix: use typed api for changing backgroundjobs_mode
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
  • Loading branch information
kesselb committed Jun 27, 2024
1 parent 00aa8f5 commit 270276a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 46 deletions.
7 changes: 4 additions & 3 deletions core/Command/Background/Ajax.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2015 Christian Kampka <christian@kampka.net>
* SPDX-License-Identifier: MIT
*/
namespace OC\Core\Command\Background;

class Ajax extends Base {
protected function getMode(): string {
return 'ajax';
}
// Implementation in OC\Core\Command\Background\Base
}
39 changes: 15 additions & 24 deletions core/Command/Background/Base.php
Original file line number Diff line number Diff line change
@@ -1,50 +1,41 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2015 Christian Kampka <christian@kampka.net>
* 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;
}
}
4 changes: 1 addition & 3 deletions core/Command/Background/Cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 4 additions & 3 deletions core/Command/Background/WebCron.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2015 Christian Kampka <christian@kampka.net>
* SPDX-License-Identifier: MIT
*/
namespace OC\Core\Command\Background;

class WebCron extends Base {
protected function getMode(): string {
return 'webcron';
}
// Implementation in OC\Core\Command\Background\Base
}
47 changes: 34 additions & 13 deletions tests/lib/Command/BackgroundJobsTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2016-2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2015 Christian Kampka <christian@kampka.net>
Expand All @@ -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()));
}
}

0 comments on commit 270276a

Please sign in to comment.