Skip to content

Commit ab46655

Browse files
committed
more unit tests
Signed-off-by: Artur Neumann <artur@jankaritech.com>
1 parent ce6f922 commit ab46655

File tree

1 file changed

+221
-22
lines changed

1 file changed

+221
-22
lines changed

tests/lib/Service/OpenProjectAPIServiceCheckNotificationsTest.php

Lines changed: 221 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace OCA\OpenProject\Service;
1111

12+
use OCA\Notifications\Handler;
13+
use OCP\Http\Client\IClientService;
1214
use OCP\Http\Client\IResponse;
1315
use OCP\IConfig;
1416
use OCP\IUserManager;
@@ -17,9 +19,16 @@
1719
use PHPUnit\Framework\TestCase;
1820

1921
class OpenProjectAPIServiceCheckNotificationsTest extends TestCase {
20-
public function testCheckNotifications(): void {
21-
$configMock = $this->getMockBuilder(IConfig::class)->getMock();
22-
$configMock
22+
/** @var IConfig $configMock */
23+
private $configMock;
24+
25+
/**
26+
* @return void
27+
* @before
28+
*/
29+
public function setUpMocks(): void {
30+
$this->configMock = $this->getMockBuilder(IConfig::class)->getMock();
31+
$this->configMock
2332
->method('getUserValue')
2433
->withConsecutive(
2534
[$this->anything(), 'integration_openproject', 'token'],
@@ -34,7 +43,7 @@ public function testCheckNotifications(): void {
3443
'refresh-token',
3544
);
3645

37-
$configMock
46+
$this->configMock
3847
->method('getAppValue')
3948
->withConsecutive(
4049
['integration_openproject', 'default_enable_notifications','0'],
@@ -47,26 +56,211 @@ public function testCheckNotifications(): void {
4756
'SECRET',
4857
'https://openproject',
4958
);
59+
}
5060

61+
/**
62+
* @param string|null $oPNotificationAPIResponse
63+
* @return IClientService
64+
*/
65+
private function getClientServiceMock($oPNotificationAPIResponse = null): IClientService {
5166
$response = $this->getMockBuilder(IResponse::class)->getMock();
52-
$oPNotificationAPIResponse = '{
67+
if ($oPNotificationAPIResponse === null) {
68+
$oPNotificationAPIResponse = '{
5369
"_type": "Collection",
5470
"_embedded": {
5571
"elements":
5672
';
57-
$oPNotificationAPIResponse .= file_get_contents(
58-
__DIR__ . '/../../jest/fixtures/notificationsResponse.json'
59-
);
60-
$oPNotificationAPIResponse .= '}}';
73+
$oPNotificationAPIResponse .= file_get_contents(
74+
__DIR__ . '/../../jest/fixtures/notificationsResponse.json'
75+
);
76+
$oPNotificationAPIResponse .= '}}';
77+
}
6178
$response->method('getBody')->willReturn($oPNotificationAPIResponse);
6279
$ocClient = $this->getMockBuilder('\OCP\Http\Client\IClient')->getMock();
6380
$ocClient->method('get')->willReturn($response);
6481
$clientService = $this->getMockBuilder('\OCP\Http\Client\IClientService')->getMock();
6582
$clientService->method('newClient')->willReturn($ocClient);
83+
return $clientService;
84+
}
85+
86+
/**
87+
* @param IManager $notificationManagerMock
88+
* @param IClientService $clientServiceMock
89+
* @param Handler $handlerMock
90+
* @return OpenProjectAPIService
91+
*/
92+
private function getService($notificationManagerMock, $clientServiceMock, $handlerMock): OpenProjectAPIService {
93+
return new OpenProjectAPIService(
94+
'integration_openproject',
95+
\OC::$server->get(IUserManager::class),
96+
$this->createMock(\OCP\IAvatarManager::class),
97+
$this->createMock(\Psr\Log\LoggerInterface::class),
98+
$this->createMock(\OCP\IL10N::class),
99+
$this->configMock,
100+
$notificationManagerMock,
101+
$clientServiceMock,
102+
$this->createMock(\OCP\Files\IRootFolder::class),
103+
$this->createMock(\OCP\IURLGenerator::class),
104+
$this->createMock(\OCP\ICacheFactory::class),
105+
$handlerMock,
106+
);
107+
}
66108

109+
public function testCheckNotifications(): void {
67110
$notificationManagerMock = $this->getMockBuilder(IManager::class)->getMock();
111+
$notificationMock = $this->getMockBuilder(INotification::class)
112+
->getMock();
68113

114+
$notificationMock
115+
->expects($this->exactly(3))
116+
->method('setSubject')
117+
->withConsecutive(
118+
[
119+
'op_notification',
120+
[
121+
'wpId' => '36',
122+
'resourceTitle' => 'write a software',
123+
'projectTitle' => 'Dev-large',
124+
'count' => 2,
125+
'reasons' => ['assigned'],
126+
'actors' => ['Admin de DEV user'],
127+
'updatedAt' => '2022-08-17T10:28:12Z'
128+
]
129+
],
130+
[
131+
'op_notification',
132+
[
133+
'wpId' => '17',
134+
'resourceTitle' => 'Create wireframes for new landing page',
135+
'projectTitle' => 'Scrum project',
136+
'count' => 5,
137+
'reasons' => [0 => 'assigned', 3 => 'mentioned'],
138+
'actors' => [0 => 'Admin de DEV user', 2 => 'Artur Neumann'],
139+
'updatedAt' => '2022-08-17T10:27:41Z'
140+
]
141+
],
142+
[
143+
'op_notification',
144+
[
145+
'wpId' => '18',
146+
'resourceTitle' => 'Contact form',
147+
'projectTitle' => 'Scrum project',
148+
'count' => 1,
149+
'reasons' => ['mentioned'],
150+
'actors' => ['Artur Neumann'],
151+
'updatedAt' => '2022-08-09T08:00:08Z'
152+
]
153+
]
154+
);
69155

156+
$notificationManagerMock
157+
->expects($this->exactly(4)) //once for marking as read and once for every notification
158+
->method('createNotification')
159+
->willReturn($notificationMock);
160+
161+
$notificationManagerMock
162+
->expects($this->exactly(3))
163+
->method('notify');
164+
165+
$service = $this->getService(
166+
$notificationManagerMock,
167+
$this->getClientServiceMock(),
168+
$this->createMock(Handler::class)
169+
);
170+
$service->checkNotifications();
171+
}
172+
public function testCheckNotificationsAfterAllNotificationsAreMarkedAsRead(): void {
173+
$oPNotificationAPIResponse = '{
174+
"_type": "Collection",
175+
"_embedded": {
176+
"elements": []
177+
}
178+
}
179+
';
180+
$notificationManagerMock = $this->getMockBuilder(IManager::class)->getMock();
181+
$notificationMock = $this->getMockBuilder(INotification::class)
182+
->getMock();
183+
184+
$notificationMock
185+
->expects($this->never())
186+
->method('setSubject');
187+
188+
$notificationManagerMock
189+
->expects($this->exactly(1)) //for marking as read
190+
->method('createNotification')
191+
->willReturn($notificationMock);
192+
193+
$notificationManagerMock
194+
->expects($this->exactly(2))
195+
->method('markProcessed');
196+
197+
$currentNotificationMock0 = $this->getMockBuilder(INotification::class)->getMock();
198+
$currentNotificationMock0
199+
->method('getSubjectParameters')
200+
->willReturn(['wpId' => 34, 'updatedAt' => '2022-11-08T06:34:40Z']);
201+
$currentNotificationMock1 = $this->getMockBuilder(INotification::class)->getMock();
202+
$currentNotificationMock1
203+
->method('getSubjectParameters')
204+
->willReturn(['wpId' => 16, 'updatedAt' => '2022-11-07T06:34:40Z']);
205+
206+
$handlerMock = $this->getMockBuilder(Handler::class)->disableOriginalConstructor()->getMock();
207+
$handlerMock->method('get')
208+
->willReturn(
209+
[12 => $currentNotificationMock0, 13 => $currentNotificationMock1]
210+
);
211+
$service = $this->getService(
212+
$notificationManagerMock,
213+
$this->getClientServiceMock($oPNotificationAPIResponse),
214+
$handlerMock
215+
);
216+
$service->checkNotifications();
217+
}
218+
public function testCheckNotificationsAfterAllNotificationsOfOneWPAreMarkedAsRead(): void {
219+
$notificationManagerMock = $this->getMockBuilder(IManager::class)->getMock();
220+
$notificationMock = $this->getMockBuilder(INotification::class)
221+
->getMock();
222+
223+
$notificationMock
224+
->expects($this->exactly(3)) // new notifications should be set
225+
->method('setSubject');
226+
227+
$notificationManagerMock
228+
->expects($this->exactly(4)) //once for marking as read and once for every notification
229+
->method('createNotification')
230+
->willReturn($notificationMock);
231+
232+
$notificationManagerMock
233+
->expects($this->exactly(3)) // for new notifications
234+
->method('notify');
235+
236+
$notificationManagerMock
237+
->expects($this->exactly(2))
238+
->method('markProcessed'); // for current ones that do not exist in the new response
239+
240+
// current notifications of WP that do not exist in the new response
241+
$currentNotificationMock0 = $this->getMockBuilder(INotification::class)->getMock();
242+
$currentNotificationMock0
243+
->method('getSubjectParameters')
244+
->willReturn(['wpId' => 34, 'updatedAt' => '2022-11-08T06:34:40Z']);
245+
$currentNotificationMock1 = $this->getMockBuilder(INotification::class)->getMock();
246+
$currentNotificationMock1
247+
->method('getSubjectParameters')
248+
->willReturn(['wpId' => 16, 'updatedAt' => '2022-11-07T06:34:40Z']);
249+
250+
$handlerMock = $this->getMockBuilder(Handler::class)->disableOriginalConstructor()->getMock();
251+
$handlerMock->method('get')
252+
->willReturn(
253+
[12 => $currentNotificationMock0, 13 => $currentNotificationMock1]
254+
);
255+
$service = $this->getService(
256+
$notificationManagerMock,
257+
$this->getClientServiceMock(),
258+
$handlerMock
259+
);
260+
$service->checkNotifications();
261+
}
262+
public function testCheckNotificationsAfterOneWPReceivedANewNotification(): void {
263+
$notificationManagerMock = $this->getMockBuilder(IManager::class)->getMock();
70264
$notificationMock = $this->getMockBuilder(INotification::class)
71265
->getMock();
72266

@@ -118,22 +312,27 @@ public function testCheckNotifications(): void {
118312
->willReturn($notificationMock);
119313

120314
$notificationManagerMock
121-
->expects($this->exactly(3))
315+
->expects($this->exactly(3)) // for new notifications
122316
->method('notify');
123317

124-
$service = new OpenProjectAPIService(
125-
'integration_openproject',
126-
\OC::$server->get(IUserManager::class),
127-
$this->createMock(\OCP\IAvatarManager::class),
128-
$this->createMock(\Psr\Log\LoggerInterface::class),
129-
$this->createMock(\OCP\IL10N::class),
130-
$configMock,
318+
$notificationManagerMock
319+
->expects($this->exactly(1))
320+
->method('markProcessed'); // for the notification that needs to be upldated
321+
322+
// this notification is also part of the response, but the response also
323+
// contains an other newer notification of that WP
324+
$currentNotificationMock0 = $this->getMockBuilder(INotification::class)->getMock();
325+
$currentNotificationMock0
326+
->method('getSubjectParameters')
327+
->willReturn(['wpId' => 36, 'updatedAt' => '2022-08-17T10:13:25Z']);
328+
329+
$handlerMock = $this->getMockBuilder(Handler::class)->disableOriginalConstructor()->getMock();
330+
$handlerMock->method('get')
331+
->willReturn([12 => $currentNotificationMock0]);
332+
$service = $this->getService(
131333
$notificationManagerMock,
132-
$clientService,
133-
$this->createMock(\OCP\Files\IRootFolder::class),
134-
$this->createMock(\OCP\IURLGenerator::class),
135-
$this->createMock(\OCP\ICacheFactory::class),
136-
$this->createMock(\OCA\Notifications\Handler::class),
334+
$this->getClientServiceMock(),
335+
$handlerMock
137336
);
138337
$service->checkNotifications();
139338
}

0 commit comments

Comments
 (0)