Skip to content

Commit ffb3a3e

Browse files
committed
Improve ResetTokenBackgroundJob and unit test
* Automatic DI is implemented since 11 * Correctly type hint parameters * Optimise the tests Signed-off-by: Joas Schilling <[email protected]>
1 parent 0f729e2 commit ffb3a3e

File tree

2 files changed

+15
-30
lines changed

2 files changed

+15
-30
lines changed

apps/updatenotification/lib/ResetTokenBackgroundJob.php

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
namespace OCA\UpdateNotification;
2424

25-
use OC\AppFramework\Utility\TimeFactory;
2625
use OC\BackgroundJob\TimedJob;
2726
use OCP\AppFramework\Utility\ITimeFactory;
2827
use OCP\IConfig;
@@ -40,28 +39,15 @@ class ResetTokenBackgroundJob extends TimedJob {
4039
private $timeFactory;
4140

4241
/**
43-
* @param IConfig|null $config
44-
* @param ITimeFactory|null $timeFactory
42+
* @param IConfig $config
43+
* @param ITimeFactory $timeFactory
4544
*/
46-
public function __construct(IConfig $config = null,
47-
ITimeFactory $timeFactory = null) {
45+
public function __construct(IConfig $config,
46+
ITimeFactory $timeFactory) {
4847
// Run all 10 minutes
4948
$this->setInterval(60 * 10);
50-
51-
if ($config instanceof IConfig && $timeFactory instanceof ITimeFactory) {
52-
$this->config = $config;
53-
$this->timeFactory = $timeFactory;
54-
} else {
55-
$this->fixDIForJobs();
56-
}
57-
}
58-
59-
/**
60-
* DI for jobs
61-
*/
62-
private function fixDIForJobs() {
63-
$this->config = \OC::$server->getConfig();
64-
$this->timeFactory = new TimeFactory();
49+
$this->config = $config;
50+
$this->timeFactory = $timeFactory;
6551
}
6652

6753
/**

apps/updatenotification/tests/ResetTokenBackgroundJobTest.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,23 @@
2929
use Test\TestCase;
3030

3131
class ResetTokenBackgroundJobTest extends TestCase {
32-
/** @var IConfig */
32+
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
3333
private $config;
34+
/** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
35+
private $timeFactory;
3436
/** @var ResetTokenBackgroundJob */
3537
private $resetTokenBackgroundJob;
36-
/** @var ITimeFactory */
37-
private $timeFactory;
3838

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

4646
public function testRunWithNotExpiredToken() {
4747
$this->timeFactory
48-
->expects($this->any())
48+
->expects($this->atLeastOnce())
4949
->method('getTime')
5050
->willReturn(123);
5151
$this->config
@@ -54,10 +54,9 @@ public function testRunWithNotExpiredToken() {
5454
->with('core', 'updater.secret.created', 123);
5555
$this->config
5656
->expects($this->never())
57-
->method('deleteSystemValue')
58-
->with('updater.secret');
57+
->method('deleteSystemValue');
5958

60-
$this->invokePrivate($this->resetTokenBackgroundJob, 'run', ['']);
59+
static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]);
6160
}
6261

6362
public function testRunWithExpiredToken() {
@@ -78,6 +77,6 @@ public function testRunWithExpiredToken() {
7877
->method('deleteSystemValue')
7978
->with('updater.secret');
8079

81-
$this->invokePrivate($this->resetTokenBackgroundJob, 'run', ['']);
80+
static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]);
8281
}
8382
}

0 commit comments

Comments
 (0)