From eea5803ae58c314ab396bc064f12cab69a65ac13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Thu, 6 Sep 2018 09:25:50 +0200 Subject: [PATCH] Fix existing service tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- tests/unit/Service/AttachmentServiceTest.php | 6 ++-- tests/unit/Service/BoardServiceTest.php | 20 +++++++++--- tests/unit/Service/CardServiceTest.php | 31 +++++++++++++++---- tests/unit/Service/StackServiceTest.php | 32 ++++++++++++-------- 4 files changed, 62 insertions(+), 27 deletions(-) diff --git a/tests/unit/Service/AttachmentServiceTest.php b/tests/unit/Service/AttachmentServiceTest.php index 56bb9cb8a..b1f205c66 100644 --- a/tests/unit/Service/AttachmentServiceTest.php +++ b/tests/unit/Service/AttachmentServiceTest.php @@ -112,7 +112,7 @@ class AttachmentServiceTest extends TestCase { $application->expects($this->any()) ->method('getContainer') ->willReturn($appContainer); - $attachmentService = new AttachmentService($this->attachmentMapper, $this->cardMapper, $this->permissionService, $application, $this->cacheFactory, $this->userId, $this->l10n); + $attachmentService = new AttachmentService($this->attachmentMapper, $this->cardMapper, $this->permissionService, $application, $this->cacheFactory, $this->userId, $this->l10n, $this->activityManager); $attachmentService->registerAttachmentService('custom', MyAttachmentService::class); $this->assertEquals($fileServiceMock, $attachmentService->getService('deck_file')); $this->assertEquals(MyAttachmentService::class, get_class($attachmentService->getService('custom'))); @@ -130,7 +130,7 @@ class AttachmentServiceTest extends TestCase { $application->expects($this->any()) ->method('getContainer') ->willReturn($appContainer); - $attachmentService = new AttachmentService($this->attachmentMapper, $this->cardMapper, $this->permissionService, $application, $this->cacheFactory, $this->userId, $this->l10n); + $attachmentService = new AttachmentService($this->attachmentMapper, $this->cardMapper, $this->permissionService, $application, $this->cacheFactory, $this->userId, $this->l10n, $this->activityManager); $attachmentService->registerAttachmentService('custom', MyAttachmentService::class); $attachmentService->getService('deck_file_invalid'); } @@ -371,4 +371,4 @@ class AttachmentServiceTest extends TestCase { $actual = $this->attachmentService->restore(123, 1); } -} \ No newline at end of file +} diff --git a/tests/unit/Service/BoardServiceTest.php b/tests/unit/Service/BoardServiceTest.php index 5f9d5082d..a5c33530d 100644 --- a/tests/unit/Service/BoardServiceTest.php +++ b/tests/unit/Service/BoardServiceTest.php @@ -24,6 +24,7 @@ namespace OCA\Deck\Service; use OC\L10N\L10N; +use OCA\Deck\Activity\ActivityManager; use OCA\Deck\Db\Acl; use OCA\Deck\Db\AclMapper; use OCA\Deck\Db\AssignedUsers; @@ -59,8 +60,10 @@ class BoardServiceTest extends TestCase { private $userManager; /** @var IUserManager */ private $groupManager; + /** @var ActivityManager */ + private $activityManager; - private $userId = 'admin'; + private $userId = 'admin'; public function setUp() { parent::setUp(); @@ -72,7 +75,8 @@ class BoardServiceTest extends TestCase { $this->notificationHelper = $this->createMock(NotificationHelper::class); $this->assignedUsersMapper = $this->createMock(AssignedUsersMapper::class); $this->userManager = $this->createMock(IUserManager::class); - $this->groupManager = $this->createMock(IGroupManager::class); + $this->groupManager = $this->createMock(IGroupManager::class); + $this->activityManager = $this->createMock(ActivityManager::class); $this->service = new BoardService( $this->boardMapper, @@ -84,11 +88,12 @@ class BoardServiceTest extends TestCase { $this->assignedUsersMapper, $this->userManager, $this->groupManager, + $this->activityManager, $this->userId ); $user = $this->createMock(IUser::class); - $user->method('getUID')->willReturn('admin'); + $user->method('getUID')->willReturn('admin'); } public function testFindAll() { @@ -186,7 +191,12 @@ class BoardServiceTest extends TestCase { ->willReturn([ 'admin' => 'admin', ]); - $this->assertEquals($board, $this->service->delete(123)); + $boardDeleted = clone $board; + $board->setDeletedAt(1); + $this->boardMapper->expects($this->once()) + ->method('update') + ->willReturn($boardDeleted); + $this->assertEquals($boardDeleted, $this->service->delete(123)); } public function testAddAcl() { @@ -268,4 +278,4 @@ class BoardServiceTest extends TestCase { ->willReturn(true); $this->assertTrue($this->service->deleteAcl(123)); } -} \ No newline at end of file +} diff --git a/tests/unit/Service/CardServiceTest.php b/tests/unit/Service/CardServiceTest.php index b00c5889c..bb72bcfc6 100644 --- a/tests/unit/Service/CardServiceTest.php +++ b/tests/unit/Service/CardServiceTest.php @@ -24,6 +24,7 @@ namespace OCA\Deck\Service; +use OCA\Deck\Activity\ActivityManager; use OCA\Deck\Db\AssignedUsers; use OCA\Deck\Db\AssignedUsersMapper; use OCA\Deck\Db\Card; @@ -34,6 +35,7 @@ use OCA\Deck\Db\LabelMapper; use OCA\Deck\NotFoundException; use OCA\Deck\Notification\NotificationHelper; use OCA\Deck\StatusException; +use OCP\Activity\IEvent; use Test\TestCase; class CardServiceTest extends TestCase { @@ -55,7 +57,10 @@ class CardServiceTest extends TestCase { /** @var LabelMapper|\PHPUnit\Framework\MockObject\MockObject */ private $labelMapper; private $boardMapper; + /** @var AttachmentService|\PHPUnit\Framework\MockObject\MockObject */ private $attachmentService; + /** @var ActivityManager|\PHPUnit\Framework\MockObject\MockObject */ + private $activityManager; public function setUp() { parent::setUp(); @@ -68,7 +73,8 @@ class CardServiceTest extends TestCase { $this->notificationHelper = $this->createMock(NotificationHelper::class); $this->assignedUsersMapper = $this->createMock(AssignedUsersMapper::class); $this->attachmentService = $this->createMock(AttachmentService::class); - $this->cardService = new CardService( + $this->activityManager = $this->createMock(ActivityManager::class); + $this->cardService = new CardService( $this->cardMapper, $this->stackMapper, $this->boardMapper, @@ -78,10 +84,23 @@ class CardServiceTest extends TestCase { $this->notificationHelper, $this->assignedUsersMapper, $this->attachmentService, + $this->activityManager, 'user1' ); } + public function mockActivity($type, $object, $subject) { + // ActivityManager::DECK_OBJECT_BOARD, $newAcl, ActivityManager::SUBJECT_BOARD_SHARE + $event = $this->createMock(IEvent::class); + $this->activityManager->expects($this->once()) + ->method('createEvent') + ->with($type, $object, $subject) + ->willReturn($event); + $this->activityManager->expects($this->once()) + ->method('sendToUsers') + ->with($event); + } + public function testFind() { $card = new Card(); $card->setId(1337); @@ -263,7 +282,7 @@ class CardServiceTest extends TestCase { $card = new Card(); $card->setArchived(true); $this->cardMapper->expects($this->once())->method('find')->willReturn($card); - $this->cardMapper->expects($this->never())->method('removeLabel'); + $this->cardMapper->expects($this->never())->method('removeLabel'); $this->expectException(StatusException::class); $this->cardService->removeLabel(123, 999); } @@ -321,9 +340,9 @@ class CardServiceTest extends TestCase { /** * @expectException \OCA\Deck\NotFoundException - * - * - * + * + * + * */ public function testUnassignUserNotExisting() { $assignment = new AssignedUsers(); @@ -337,7 +356,7 @@ class CardServiceTest extends TestCase { ->with(123) ->willReturn($assignments); $this->expectException(NotFoundException::class); - $actual = $this->cardService->unassignUser(123, 'user'); + $actual = $this->cardService->unassignUser(123, 'user'); } diff --git a/tests/unit/Service/StackServiceTest.php b/tests/unit/Service/StackServiceTest.php index e013de63d..1d436afa2 100644 --- a/tests/unit/Service/StackServiceTest.php +++ b/tests/unit/Service/StackServiceTest.php @@ -25,6 +25,7 @@ namespace OCA\Deck\Service; +use OCA\Deck\Activity\ActivityManager; use OCA\Deck\Db\AssignedUsersMapper; use OCA\Deck\Db\Card; use OCA\Deck\Db\CardMapper; @@ -63,6 +64,8 @@ class StackServiceTest extends TestCase { private $boardService; /** @var CardService|\PHPUnit\Framework\MockObject\MockObject */ private $cardService; + /** @var ActivityManager|\PHPUnit\Framework\MockObject\MockObject */ + private $activityManager; public function setUp() { parent::setUp(); @@ -75,18 +78,19 @@ class StackServiceTest extends TestCase { $this->assignedUsersMapper = $this->createMock(AssignedUsersMapper::class); $this->attachmentService = $this->createMock(AttachmentService::class); $this->labelMapper = $this->createMock(LabelMapper::class); + $this->activityManager = $this->createMock(ActivityManager::class); $this->stackService = new StackService( $this->stackMapper, - $this->boardMapper, - $this->cardMapper, - $this->labelMapper, - + $this->boardMapper, + $this->cardMapper, + $this->labelMapper, $this->permissionService, $this->boardService, $this->cardService, $this->assignedUsersMapper, - $this->attachmentService + $this->attachmentService, + $this->activityManager ); } @@ -176,14 +180,16 @@ class StackServiceTest extends TestCase { } public function testDelete() { - $this->permissionService->expects($this->once())->method('checkPermission'); - $stackToBeDeleted = new Stack(); - $stackToBeDeleted->setId(1); - $this->stackMapper->expects($this->once())->method('find')->willReturn($stackToBeDeleted); - $this->stackMapper->expects($this->once())->method('update'); - $this->stackService->delete(123); - $this->assertTrue($stackToBeDeleted->getDeletedAt() <= time(), "deletedAt is in the past"); - } + $this->permissionService->expects($this->once())->method('checkPermission'); + $stackToBeDeleted = new Stack(); + $stackToBeDeleted->setId(1); + $this->stackMapper->expects($this->once())->method('find')->willReturn($stackToBeDeleted); + $this->stackMapper->expects($this->once())->method('update')->willReturn($stackToBeDeleted); + $this->stackService->delete(123); + $this->assertTrue($stackToBeDeleted->getDeletedAt() <= time(), "deletedAt is in the past"); + $this->assertTrue($stackToBeDeleted->getDeletedAt() > 0, "deletedAt is set"); + + } public function testUpdate() { $this->permissionService->expects($this->once())->method('checkPermission');