Skip to content

Commit a2dce36

Browse files
committed
fix(tests): align phpunit mocks with dav and activity changes
Signed-off-by: Jaggob <37583151+Jaggob@users.noreply.github.com>
1 parent 84e26f1 commit a2dce36

File tree

5 files changed

+64
-34
lines changed

5 files changed

+64
-34
lines changed

lib/Activity/ActivityManager.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,11 +452,12 @@ private function createEvent($objectType, $entity, $subject, $additionalParams =
452452
*/
453453
private function sendToUsers(IEvent $event) {
454454
$subjectParams = $event->getSubjectParameters();
455-
if ($event->getObjectType() === self::DECK_OBJECT_BOARD) {
455+
$objectType = $event->getObjectType();
456+
if ($objectType === self::DECK_OBJECT_BOARD) {
456457
$boardId = (int)$event->getObjectId();
457458
} elseif (isset($subjectParams['board']['id'])) {
458459
$boardId = (int)$subjectParams['board']['id'];
459-
} elseif ($event->getObjectType() === self::DECK_OBJECT_CARD) {
460+
} elseif ($objectType === self::DECK_OBJECT_CARD) {
460461
$boardId = $this->cardMapper->findBoardId((int)$event->getObjectId());
461462
} else {
462463
return;

lib/DAV/DeckCalendarBackend.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public function createCalendarObject(int $boardId, string $owner, string $data,
311311
$categories = $this->extractCategories($todo);
312312
if ($categories !== null) {
313313
$categories = $this->normalizeCategoriesForLabelSync($boardId, $categories, $mode);
314-
$this->syncCardCategories($card->getId(), $categories);
314+
$this->syncCardCategories($card->getId(), $categories, $boardId);
315315
}
316316

317317
return $card;
@@ -418,7 +418,7 @@ private function updateCardFromCalendar(Card $sourceItem, string $data, bool $re
418418
return $card;
419419
}
420420
if ($isNoopUpdate && $categories !== null) {
421-
$this->syncCardCategories($card->getId(), $categories);
421+
$this->syncCardCategories($card->getId(), $categories, $boardId);
422422
return $card;
423423
}
424424

@@ -437,7 +437,7 @@ private function updateCardFromCalendar(Card $sourceItem, string $data, bool $re
437437
$boardId
438438
);
439439
if ($categories !== null) {
440-
$this->syncCardCategories($updatedCard->getId(), $categories);
440+
$this->syncCardCategories($updatedCard->getId(), $categories, $boardId);
441441
}
442442

443443
return $updatedCard;
@@ -722,9 +722,9 @@ private function extractCategories($todo): ?array {
722722
/**
723723
* @param list<string> $categories
724724
*/
725-
private function syncCardCategories(int $cardId, array $categories): void {
725+
private function syncCardCategories(int $cardId, array $categories, ?int $boardId = null): void {
726726
$card = $this->cardService->find($cardId);
727-
$boardId = $this->getBoardIdForCard($card);
727+
$boardId ??= $this->getBoardIdForCard($card);
728728
$board = $this->boardMapper->find($boardId, true, false);
729729
$canCreateLabels = $this->checkBoardPermission($boardId, Acl::PERMISSION_MANAGE);
730730
$boardLabels = $board->getLabels() ?? [];

tests/unit/Activity/ActivityManagerTest.php

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,6 @@ public function testCreateEvent() {
134134
$board = new Board();
135135
$board->setId(123);
136136
$board->setTitle('');
137-
$this->boardMapper->expects(self::once())
138-
->method('find')
139-
->willReturn($board);
140137
$event = $this->expectEventCreation(ActivityManager::SUBJECT_BOARD_CREATE, [
141138
'author' => 'admin'
142139
]);
@@ -331,18 +328,11 @@ public function testSendToUser($objectType) {
331328
)
332329
->willReturnSelf();
333330

334-
$mapper = null;
335-
switch ($objectType) {
336-
case ActivityManager::DECK_OBJECT_BOARD:
337-
$mapper = $this->boardMapper;
338-
break;
339-
case ActivityManager::DECK_OBJECT_CARD:
340-
$mapper = $this->cardMapper;
341-
break;
331+
if ($objectType === ActivityManager::DECK_OBJECT_CARD) {
332+
$this->cardMapper->expects(self::once())
333+
->method('findBoardId')
334+
->willReturn(123);
342335
}
343-
$mapper->expects(self::once())
344-
->method('findBoardId')
345-
->willReturn(123);
346336
$this->permissionService->expects(self::once())
347337
->method('findUsers')
348338
->willReturn($users);
@@ -391,18 +381,26 @@ public function testFindObjectForEntity($objectType, $entity) {
391381
$card->setId(3);
392382
$expected = null;
393383
if ($objectType === ActivityManager::DECK_OBJECT_BOARD) {
394-
$this->boardMapper->expects(self::once())
395-
->method('find')
396-
->with(1)
397-
->willReturn($board);
398-
$expected = $board;
384+
if ($entity instanceof Board) {
385+
$expected = $entity;
386+
} else {
387+
$this->boardMapper->expects(self::once())
388+
->method('find')
389+
->with(1)
390+
->willReturn($board);
391+
$expected = $board;
392+
}
399393
}
400394
if ($objectType === ActivityManager::DECK_OBJECT_CARD) {
401-
$this->cardMapper->expects(self::once())
402-
->method('find')
403-
->with(3)
404-
->willReturn($card);
405-
$expected = $card;
395+
if ($entity instanceof Card) {
396+
$expected = $entity;
397+
} else {
398+
$this->cardMapper->expects(self::once())
399+
->method('find')
400+
->with(3)
401+
->willReturn($card);
402+
$expected = $card;
403+
}
406404
}
407405
$actual = $this->invokePrivate($this->activityManager, 'findObjectForEntity', [$objectType, $entity]);
408406
$this->assertEquals($expected, $actual);

tests/unit/DAV/DeckCalendarBackendTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,9 +661,10 @@ public function testCreateCardFromCalendarWithExistingDeckUidUpdatesInsteadOfCre
661661
[123]
662662
)
663663
->willReturnOnConsecutiveCalls($sourceCard, $sourceCard);
664-
$this->stackService->expects($this->exactly(2))
664+
$this->stackService->expects($this->exactly(3))
665665
->method('find')
666666
->willReturnMap([
667+
[42, $sourceStack],
667668
[42, $sourceStack],
668669
[88, $targetStack],
669670
]);
@@ -727,7 +728,7 @@ public function testUpdateCardWithInProgressStatusClearsDone(): void {
727728
$currentStack = new Stack();
728729
$currentStack->setId(42);
729730
$currentStack->setBoardId(12);
730-
$this->stackService->expects($this->once())
731+
$this->stackService->expects($this->exactly(2))
731732
->method('find')
732733
->with(42)
733734
->willReturn($currentStack);

tests/unit/Service/CardServiceTest.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,40 @@ public function testCreate() {
229229
$this->cardMapper->expects($this->once())
230230
->method('insert')
231231
->willReturn($card);
232-
$this->stackMapper->expects($this->once())
232+
$board = $this->createMock(Board::class);
233+
$user = $this->createMock(IUser::class);
234+
$this->userManager->expects($this->once())
235+
->method('get')
236+
->with('user1')
237+
->willReturn($user);
238+
$this->commentsManager->expects($this->once())
239+
->method('getNumberOfCommentsForObject')
240+
->with('deckCard', '0')
241+
->willReturn(0);
242+
$this->attachmentService->expects($this->once())
243+
->method('count')
244+
->with(0)
245+
->willReturn(0);
246+
$this->stackMapper->expects($this->exactly(2))
233247
->method('find')
234248
->with(123)
235249
->willReturn($stack);
250+
$this->boardMapper->expects($this->once())
251+
->method('find')
252+
->with(1337)
253+
->willReturn($board);
254+
$this->labelMapper->expects($this->once())
255+
->method('findAssignedLabelsForCards')
256+
->with([0])
257+
->willReturn([]);
258+
$this->assignedUsersMapper->expects($this->once())
259+
->method('findIn')
260+
->with([0])
261+
->willReturn([]);
262+
$this->referenceManager->expects($this->once())
263+
->method('extractReferences')
264+
->with('Card title')
265+
->willReturn([]);
236266
$b = $this->cardService->create('Card title', 123, 'text', 999, 'admin');
237267

238268
$this->assertEquals($b->getTitle(), 'Card title');

0 commit comments

Comments
 (0)