Skip to content

Commit 50e1cee

Browse files
committed
Automatic DI for Controllers also works
Signed-off-by: Joas Schilling <[email protected]>
1 parent ffb3a3e commit 50e1cee

File tree

4 files changed

+48
-78
lines changed

4 files changed

+48
-78
lines changed

apps/updatenotification/appinfo/app.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
}
4545

4646
$manager = \OC::$server->getNotificationManager();
47-
$manager->registerNotifier(function() use ($manager) {
47+
$manager->registerNotifier(function() {
4848
return \OC::$server->query(\OCA\UpdateNotification\Notification\Notifier::class);
4949
}, function() {
5050
$l = \OC::$server->getL10N('updatenotification');
Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,32 @@
11
<?php
22
/**
3-
* @copyright Copyright (c) 2016, ownCloud, Inc.
3+
* @copyright Copyright (c) 2018, Joas Schilling <[email protected]>
44
*
5-
* @author Lukas Reschke <[email protected]>
6-
* @author Thomas Müller <[email protected]>
5+
* @author Joas Schilling <[email protected]>
76
*
8-
* @license AGPL-3.0
7+
* @license GNU AGPL version 3 or any later version
98
*
10-
* This code is free software: you can redistribute it and/or modify
11-
* it under the terms of the GNU Affero General Public License, version 3,
12-
* as published by the Free Software Foundation.
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
1313
*
1414
* This program is distributed in the hope that it will be useful,
1515
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1717
* GNU Affero General Public License for more details.
1818
*
19-
* You should have received a copy of the GNU Affero General Public License, version 3,
20-
* along with this program. If not, see <http://www.gnu.org/licenses/>
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2121
*
2222
*/
2323

2424
namespace OCA\UpdateNotification\AppInfo;
2525

26-
use OC\AppFramework\Utility\TimeFactory;
27-
use OCA\UpdateNotification\Controller\AdminController;
28-
use OCA\UpdateNotification\UpdateChecker;
2926
use OCP\AppFramework\App;
30-
use OCP\AppFramework\IAppContainer;
3127

3228
class Application extends App {
33-
public function __construct (array $urlParams = array()) {
34-
parent::__construct('updatenotification', $urlParams);
35-
$container = $this->getContainer();
36-
37-
$container->registerService('AdminController', function(IAppContainer $c) {
38-
$updater = new \OC\Updater\VersionCheck(
39-
\OC::$server->getHTTPClientService(),
40-
\OC::$server->getConfig()
41-
);
42-
return new AdminController(
43-
$c->query('AppName'),
44-
$c->query('Request'),
45-
$c->getServer()->getJobList(),
46-
$c->getServer()->getSecureRandom(),
47-
$c->getServer()->getConfig(),
48-
new TimeFactory(),
49-
$c->getServer()->getL10N($c->query('AppName')),
50-
new UpdateChecker($updater),
51-
$c->getServer()->getDateTimeFormatter()
52-
);
53-
});
29+
public function __construct() {
30+
parent::__construct('updatenotification', []);
5431
}
55-
5632
}

apps/updatenotification/lib/Controller/AdminController.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
namespace OCA\UpdateNotification\Controller;
2727

28+
use OCA\UpdateNotification\ResetTokenBackgroundJob;
2829
use OCA\UpdateNotification\UpdateChecker;
2930
use OCP\AppFramework\Controller;
3031
use OCP\AppFramework\Http\DataResponse;
@@ -37,6 +38,7 @@
3738
use OCP\IRequest;
3839
use OCP\Security\ISecureRandom;
3940
use OCP\Settings\ISettings;
41+
use OCP\Util;
4042

4143
class AdminController extends Controller implements ISettings {
4244
/** @var IJobList */
@@ -87,7 +89,7 @@ public function __construct($appName,
8789
/**
8890
* @return TemplateResponse
8991
*/
90-
public function displayPanel() {
92+
public function getForm() {
9193
$lastUpdateCheckTimestamp = $this->config->getAppValue('core', 'lastupdatedat');
9294
$lastUpdateCheck = $this->dateTimeFormatter->formatDateTime($lastUpdateCheckTimestamp);
9395

@@ -97,7 +99,7 @@ public function displayPanel() {
9799
'stable',
98100
'production',
99101
];
100-
$currentChannel = \OCP\Util::getChannel();
102+
$currentChannel = Util::getChannel();
101103

102104
// Remove the currently used channel from the channels list
103105
if(($key = array_search($currentChannel, $channels)) !== false) {
@@ -128,13 +130,11 @@ public function displayPanel() {
128130
}
129131

130132
/**
131-
* @UseSession
132-
*
133133
* @param string $channel
134134
* @return DataResponse
135135
*/
136136
public function setChannel($channel) {
137-
\OCP\Util::setChannel($channel);
137+
Util::setChannel($channel);
138138
$this->config->setAppValue('core', 'lastupdatedat', 0);
139139
return new DataResponse(['status' => 'success', 'data' => ['message' => $this->l10n->t('Channel updated')]]);
140140
}
@@ -144,7 +144,7 @@ public function setChannel($channel) {
144144
*/
145145
public function createCredentials() {
146146
// Create a new job and store the creation date
147-
$this->jobList->add('OCA\UpdateNotification\ResetTokenBackgroundJob');
147+
$this->jobList->add(ResetTokenBackgroundJob::class);
148148
$this->config->setAppValue('core', 'updater.secret.created', $this->timeFactory->getTime());
149149

150150
// Create a new token
@@ -154,13 +154,6 @@ public function createCredentials() {
154154
return new DataResponse($newToken);
155155
}
156156

157-
/**
158-
* @return TemplateResponse returns the instance with all parameters set, ready to be rendered
159-
*/
160-
public function getForm() {
161-
return $this->displayPanel();
162-
}
163-
164157
/**
165158
* @return string the section ID, e.g. 'sharing'
166159
*/

apps/updatenotification/tests/Controller/AdminControllerTest.php

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
namespace OCA\UpdateNotification\Tests\Controller;
2626

2727
use OCA\UpdateNotification\Controller\AdminController;
28+
use OCA\UpdateNotification\ResetTokenBackgroundJob;
2829
use OCA\UpdateNotification\UpdateChecker;
2930
use OCP\AppFramework\Http\DataResponse;
3031
use OCP\AppFramework\Http\TemplateResponse;
@@ -35,40 +36,40 @@
3536
use OCP\IL10N;
3637
use OCP\IRequest;
3738
use OCP\Security\ISecureRandom;
39+
use OCP\Util;
3840
use Test\TestCase;
3941

4042
class AdminControllerTest extends TestCase {
41-
/** @var IRequest */
43+
/** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
4244
private $request;
43-
/** @var IJobList */
45+
/** @var IJobList|\PHPUnit_Framework_MockObject_MockObject */
4446
private $jobList;
45-
/** @var ISecureRandom */
47+
/** @var ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */
4648
private $secureRandom;
47-
/** @var IConfig */
49+
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
4850
private $config;
4951
/** @var AdminController */
5052
private $adminController;
51-
/** @var ITimeFactory */
53+
/** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
5254
private $timeFactory;
53-
/** @var IL10N */
55+
/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
5456
private $l10n;
55-
/** @var UpdateChecker */
57+
/** @var UpdateChecker|\PHPUnit_Framework_MockObject_MockObject */
5658
private $updateChecker;
57-
/** @var IDateTimeFormatter */
59+
/** @var IDateTimeFormatter|\PHPUnit_Framework_MockObject_MockObject */
5860
private $dateTimeFormatter;
5961

6062
public function setUp() {
6163
parent::setUp();
6264

63-
$this->request = $this->getMockBuilder('\\OCP\\IRequest')->getMock();
64-
$this->jobList = $this->getMockBuilder('\\OCP\\BackgroundJob\\IJobList')->getMock();
65-
$this->secureRandom = $this->getMockBuilder('\\OCP\\Security\\ISecureRandom')->getMock();
66-
$this->config = $this->getMockBuilder('\\OCP\\IConfig')->getMock();
67-
$this->timeFactory = $this->getMockBuilder('\\OCP\\AppFramework\\Utility\\ITimeFactory')->getMock();
68-
$this->l10n = $this->getMockBuilder('\\OCP\\IL10N')->getMock();
69-
$this->updateChecker = $this->getMockBuilder('\\OCA\\UpdateNotification\\UpdateChecker')
70-
->disableOriginalConstructor()->getMock();
71-
$this->dateTimeFormatter = $this->getMockBuilder('\\OCP\\IDateTimeFormatter')->getMock();
65+
$this->request = $this->createMock(IRequest::class);
66+
$this->jobList = $this->createMock(IJobList::class);
67+
$this->secureRandom = $this->createMock(ISecureRandom::class);
68+
$this->config = $this->createMock(IConfig::class);
69+
$this->timeFactory = $this->createMock(ITimeFactory::class);
70+
$this->l10n = $this->createMock(IL10N::class);
71+
$this->updateChecker = $this->createMock(UpdateChecker::class);
72+
$this->dateTimeFormatter = $this->createMock(IDateTimeFormatter::class);
7273

7374
$this->adminController = new AdminController(
7475
'updatenotification',
@@ -83,17 +84,17 @@ public function setUp() {
8384
);
8485
}
8586

86-
public function testDisplayPanelWithUpdate() {
87+
public function testGetFormWithUpdate() {
8788
$channels = [
8889
'daily',
8990
'beta',
9091
'stable',
9192
'production',
9293
];
93-
$currentChannel = \OCP\Util::getChannel();
94+
$currentChannel = Util::getChannel();
9495

9596
// Remove the currently used channel from the channels list
96-
if(($key = array_search($currentChannel, $channels)) !== false) {
97+
if(($key = array_search($currentChannel, $channels, true)) !== false) {
9798
unset($channels[$key]);
9899
}
99100

@@ -128,7 +129,7 @@ public function testDisplayPanelWithUpdate() {
128129
'isNewVersionAvailable' => true,
129130
'isUpdateChecked' => true,
130131
'lastChecked' => 'LastCheckedReturnValue',
131-
'currentChannel' => \OCP\Util::getChannel(),
132+
'currentChannel' => Util::getChannel(),
132133
'channels' => $channels,
133134
'newVersionString' => '8.1.2',
134135
'downloadLink' => 'https://downloads.nextcloud.org/server',
@@ -139,20 +140,20 @@ public function testDisplayPanelWithUpdate() {
139140
];
140141

141142
$expected = new TemplateResponse('updatenotification', 'admin', $params, '');
142-
$this->assertEquals($expected, $this->adminController->displayPanel());
143+
$this->assertEquals($expected, $this->adminController->getForm());
143144
}
144145

145-
public function testDisplayPanelWithoutUpdate() {
146+
public function testGetFormWithoutUpdate() {
146147
$channels = [
147148
'daily',
148149
'beta',
149150
'stable',
150151
'production',
151152
];
152-
$currentChannel = \OCP\Util::getChannel();
153+
$currentChannel = Util::getChannel();
153154

154155
// Remove the currently used channel from the channels list
155-
if(($key = array_search($currentChannel, $channels)) !== false) {
156+
if(($key = array_search($currentChannel, $channels, true)) !== false) {
156157
unset($channels[$key]);
157158
}
158159

@@ -182,7 +183,7 @@ public function testDisplayPanelWithoutUpdate() {
182183
'isNewVersionAvailable' => false,
183184
'isUpdateChecked' => true,
184185
'lastChecked' => 'LastCheckedReturnValue',
185-
'currentChannel' => \OCP\Util::getChannel(),
186+
'currentChannel' => Util::getChannel(),
186187
'channels' => $channels,
187188
'newVersionString' => '',
188189
'downloadLink' => '',
@@ -193,15 +194,15 @@ public function testDisplayPanelWithoutUpdate() {
193194
];
194195

195196
$expected = new TemplateResponse('updatenotification', 'admin', $params, '');
196-
$this->assertEquals($expected, $this->adminController->displayPanel());
197+
$this->assertEquals($expected, $this->adminController->getForm());
197198
}
198199

199200

200201
public function testCreateCredentials() {
201202
$this->jobList
202203
->expects($this->once())
203204
->method('add')
204-
->with('OCA\UpdateNotification\ResetTokenBackgroundJob');
205+
->with(ResetTokenBackgroundJob::class);
205206
$this->secureRandom
206207
->expects($this->once())
207208
->method('generate')

0 commit comments

Comments
 (0)