From 0b2ea0f8391f32ba99d7566a8e7f9c589a7ea243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Mon, 27 Feb 2023 23:45:33 +0100 Subject: [PATCH] fix: Centralize usage of CardDetails MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/Service/CardService.php | 8 +++++++- lib/Service/StackService.php | 10 +--------- tests/unit/Service/CardServiceTest.php | 6 +++++- .../Service/Importer/Systems/TrelloJsonServiceTest.php | 6 +++--- tests/unit/Service/StackServiceTest.php | 4 ++++ 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/Service/CardService.php b/lib/Service/CardService.php index 374111cb9..8e96dce09 100644 --- a/lib/Service/CardService.php +++ b/lib/Service/CardService.php @@ -39,6 +39,7 @@ use OCA\Deck\Db\StackMapper; use OCA\Deck\Event\CardCreatedEvent; use OCA\Deck\Event\CardDeletedEvent; use OCA\Deck\Event\CardUpdatedEvent; +use OCA\Deck\Model\CardDetails; use OCA\Deck\NoPermissionException; use OCA\Deck\Notification\NotificationHelper; use OCA\Deck\Db\BoardMapper; @@ -155,7 +156,12 @@ class CardService { $card->setAssignedUsers($cardAssignedUsers); } - return $cards; + return array_map( + function (Card $card): CardDetails { + return new CardDetails($card); + }, + $cards + ); } public function fetchDeleted($boardId) { $this->cardServiceValidator->check(compact('boardId')); diff --git a/lib/Service/StackService.php b/lib/Service/StackService.php index 4a21635c5..ce3e1195c 100644 --- a/lib/Service/StackService.php +++ b/lib/Service/StackService.php @@ -99,15 +99,7 @@ class StackService { return; } - $this->cardService->enrichCards($cards); - $cards = array_map( - function (Card $card): CardDetails { - return new CardDetails($card); - }, - $cards - ); - - $stack->setCards($cards); + $stack->setCards($this->cardService->enrichCards($cards)); } private function enrichStacksWithCards($stacks, $since = -1) { diff --git a/tests/unit/Service/CardServiceTest.php b/tests/unit/Service/CardServiceTest.php index 1952747b3..f0a9eb58e 100644 --- a/tests/unit/Service/CardServiceTest.php +++ b/tests/unit/Service/CardServiceTest.php @@ -34,6 +34,7 @@ use OCA\Deck\Db\Stack; use OCA\Deck\Db\StackMapper; use OCA\Deck\Db\BoardMapper; use OCA\Deck\Db\LabelMapper; +use OCA\Deck\Model\CardDetails; use OCA\Deck\Notification\NotificationHelper; use OCA\Deck\StatusException; use OCA\Deck\Validators\CardServiceValidator; @@ -188,7 +189,10 @@ class CardServiceTest extends TestCase { $cardExpected->setRelatedBoard($boardMock); $cardExpected->setRelatedStack($stackMock); $cardExpected->setLabels([]); - $this->assertEquals($cardExpected, $this->cardService->find(123)); + $expected = new CardDetails($cardExpected); + + $actual = $this->cardService->find(123); + $this->assertEquals($expected->jsonSerialize(), $actual->jsonSerialize()); } public function testCreate() { diff --git a/tests/unit/Service/Importer/Systems/TrelloJsonServiceTest.php b/tests/unit/Service/Importer/Systems/TrelloJsonServiceTest.php index a1e18b11c..fd312e4a7 100644 --- a/tests/unit/Service/Importer/Systems/TrelloJsonServiceTest.php +++ b/tests/unit/Service/Importer/Systems/TrelloJsonServiceTest.php @@ -76,7 +76,7 @@ class TrelloJsonServiceTest extends \Test\TestCase { } public function testValidateUsersWithNotStringNextcloud() { - $this->expectErrorMessage('User on setting uidRelation is invalid'); + $this->expectExceptionMessage('User on setting uidRelation is invalid'); $importService = $this->createMock(BoardImportService::class); $importService ->method('getConfig') @@ -92,7 +92,7 @@ class TrelloJsonServiceTest extends \Test\TestCase { } public function testValidateUsersWithNotFoundUser() { - $this->expectErrorMessage('User on setting uidRelation not found: nextcloud_user'); + $this->expectExceptionMessage('User on setting uidRelation not found: nextcloud_user'); $importService = $this->createMock(BoardImportService::class); $importService ->method('getConfig') @@ -124,7 +124,7 @@ class TrelloJsonServiceTest extends \Test\TestCase { } public function testGetBoardWithNoName() { - $this->expectErrorMessage('Invalid name of board'); + $this->expectExceptionMessage('Invalid name of board'); $importService = $this->createMock(BoardImportService::class); $this->service->setImportService($importService); $this->service->getBoard(); diff --git a/tests/unit/Service/StackServiceTest.php b/tests/unit/Service/StackServiceTest.php index 84e38173d..aa573a3c5 100644 --- a/tests/unit/Service/StackServiceTest.php +++ b/tests/unit/Service/StackServiceTest.php @@ -33,6 +33,7 @@ use OCA\Deck\Db\Label; use OCA\Deck\Db\LabelMapper; use OCA\Deck\Db\Stack; use OCA\Deck\Db\StackMapper; +use OCA\Deck\Model\CardDetails; use OCA\Deck\Validators\StackServiceValidator; use OCP\EventDispatcher\IEventDispatcher; use Psr\Log\LoggerInterface; @@ -121,6 +122,9 @@ class StackServiceTest extends TestCase { foreach ($cards as $card) { $card->setLabels($this->getLabels()[$card->getId()]); } + return array_map(function ($card) { + return new CardDetails($card); + }, $cards); } ) );