Skip to content

Commit

Permalink
Improve ResetTokenBackgroundJob and unit test
Browse files Browse the repository at this point in the history
* Automatic DI is implemented since 11
* Correctly type hint parameters
* Optimise the tests

Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Jan 15, 2018
1 parent 0f729e2 commit ffb3a3e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 30 deletions.
26 changes: 6 additions & 20 deletions apps/updatenotification/lib/ResetTokenBackgroundJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

namespace OCA\UpdateNotification;

use OC\AppFramework\Utility\TimeFactory;
use OC\BackgroundJob\TimedJob;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig;
Expand All @@ -40,28 +39,15 @@ class ResetTokenBackgroundJob extends TimedJob {
private $timeFactory;

/**
* @param IConfig|null $config
* @param ITimeFactory|null $timeFactory
* @param IConfig $config
* @param ITimeFactory $timeFactory
*/
public function __construct(IConfig $config = null,
ITimeFactory $timeFactory = null) {
public function __construct(IConfig $config,
ITimeFactory $timeFactory) {
// Run all 10 minutes
$this->setInterval(60 * 10);

if ($config instanceof IConfig && $timeFactory instanceof ITimeFactory) {
$this->config = $config;
$this->timeFactory = $timeFactory;
} else {
$this->fixDIForJobs();
}
}

/**
* DI for jobs
*/
private function fixDIForJobs() {
$this->config = \OC::$server->getConfig();
$this->timeFactory = new TimeFactory();
$this->config = $config;
$this->timeFactory = $timeFactory;
}

/**
Expand Down
19 changes: 9 additions & 10 deletions apps/updatenotification/tests/ResetTokenBackgroundJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@
use Test\TestCase;

class ResetTokenBackgroundJobTest extends TestCase {
/** @var IConfig */
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
private $config;
/** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
private $timeFactory;
/** @var ResetTokenBackgroundJob */
private $resetTokenBackgroundJob;
/** @var ITimeFactory */
private $timeFactory;

public function setUp() {
parent::setUp();
$this->config = $this->getMockBuilder('\\OCP\\IConfig')->getMock();
$this->timeFactory = $this->getMockBuilder('\\OCP\\AppFramework\\Utility\\ITimeFactory')->getMock();
$this->config = $this->createMock(IConfig::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->resetTokenBackgroundJob = new ResetTokenBackgroundJob($this->config, $this->timeFactory);
}

public function testRunWithNotExpiredToken() {
$this->timeFactory
->expects($this->any())
->expects($this->atLeastOnce())
->method('getTime')
->willReturn(123);
$this->config
Expand All @@ -54,10 +54,9 @@ public function testRunWithNotExpiredToken() {
->with('core', 'updater.secret.created', 123);
$this->config
->expects($this->never())
->method('deleteSystemValue')
->with('updater.secret');
->method('deleteSystemValue');

$this->invokePrivate($this->resetTokenBackgroundJob, 'run', ['']);
static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]);
}

public function testRunWithExpiredToken() {
Expand All @@ -78,6 +77,6 @@ public function testRunWithExpiredToken() {
->method('deleteSystemValue')
->with('updater.secret');

$this->invokePrivate($this->resetTokenBackgroundJob, 'run', ['']);
static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]);
}
}

0 comments on commit ffb3a3e

Please sign in to comment.