Completed
Pull Request — master (#366)
by Beñat
04:18
created

MarkAsUnreadNotificationSpec   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 7
dl 52
loc 52
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 13 13 1
A it_does_not_mark_as_unread_notification_when_the_user_does_not_exist() 7 7 1
A it_does_not_mark_as_unread_notification_when_the_notification_does_not_exist() 10 10 1
A it_marks_as_unread_notification() 13 13 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace Spec\Kreta\Notifier\Application\Inbox\Notification;
16
17
use Kreta\Notifier\Application\Inbox\Notification\MarkAsUnreadNotificationCommand;
18
use Kreta\Notifier\Domain\Model\Inbox\Notification\Notification;
19
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationDoesNotExist;
20
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationId;
21
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationRepository;
22
use Kreta\Notifier\Domain\Model\Inbox\User;
23
use Kreta\Notifier\Domain\Model\Inbox\UserDoesNotExist;
24
use Kreta\Notifier\Domain\Model\Inbox\UserId;
25
use Kreta\Notifier\Domain\Model\Inbox\UserRepository;
26
use PhpSpec\ObjectBehavior;
27
28 View Code Duplication
class MarkAsUnreadNotificationSpec extends ObjectBehavior
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
{
30
    private $notificationId;
31
    private $userId;
32
33
    function let(
34
        NotificationRepository $repository,
35
        UserRepository $userRepository,
36
        MarkAsUnreadNotificationCommand $command
37
    ) {
38
        $this->beConstructedWith($repository, $userRepository);
39
40
        $command->notificationId()->shouldBeCalled()->willReturn('notification-id');
41
        $command->userId()->shouldBeCalled()->willReturn('user-id');
42
43
        $this->notificationId = NotificationId::generate('notification-id');
44
        $this->userId = UserId::generate('user-id');
45
    }
46
47
    function it_does_not_mark_as_unread_notification_when_the_user_does_not_exist(
48
        MarkAsUnreadNotificationCommand $command,
49
        UserRepository $userRepository
50
    ) {
51
        $userRepository->get($this->userId)->shouldBeCalled()->willThrow(UserDoesNotExist::class);
52
        $this->shouldThrow(UserDoesNotExist::class)->during__invoke($command);
53
    }
54
55
    function it_does_not_mark_as_unread_notification_when_the_notification_does_not_exist(
56
        MarkAsUnreadNotificationCommand $command,
57
        NotificationRepository $repository,
58
        UserRepository $userRepository,
59
        User $user
60
    ) {
61
        $userRepository->get($this->userId)->shouldBeCalled()->willReturn($user);
62
        $repository->get($this->notificationId)->shouldBeCalled()->willThrow(NotificationDoesNotExist::class);
63
        $this->shouldThrow(NotificationDoesNotExist::class)->during__invoke($command);
64
    }
65
66
    function it_marks_as_unread_notification(
67
        MarkAsUnreadNotificationCommand $command,
68
        Notification $notification,
69
        NotificationRepository $repository,
70
        UserRepository $userRepository,
71
        User $user
72
    ) {
73
        $userRepository->get($this->userId)->shouldBeCalled()->willReturn($user);
74
        $repository->get($this->notificationId)->shouldBeCalled()->willReturn($notification);
75
        $notification->markAsUnread()->shouldBeCalled();
76
        $repository->save($notification)->shouldBeCalled();
77
        $this->__invoke($command);
78
    }
79
}
80