Completed
Pull Request — master (#366)
by Beñat
14:40
created

UserSpec   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 129
Duplicated Lines 32.56 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 7
dl 42
loc 129
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 5 1
A it_can_be_signed_up() 0 9 1
A it_can_be_reconstituted_user() 0 16 1
A it_receives_notification() 0 17 1
A it_reads_notification() 20 20 1
A it_unreads_notification() 22 22 1
A it_does_not_get_notification_when_it_does_not_exist() 0 12 1
A it_gets_notification() 0 18 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\Domain\Model\Inbox;
16
17
use Kreta\Notifier\Domain\Model\Inbox\Notification;
18
use Kreta\Notifier\Domain\Model\Inbox\NotificationBody;
19
use Kreta\Notifier\Domain\Model\Inbox\NotificationDoesNotExist;
20
use Kreta\Notifier\Domain\Model\Inbox\NotificationId;
21
use Kreta\Notifier\Domain\Model\Inbox\User;
22
use Kreta\Notifier\Domain\Model\Inbox\UserId;
23
use Kreta\Notifier\Domain\Model\Inbox\UserReadNotification;
24
use Kreta\Notifier\Domain\Model\Inbox\UserReceivedNotification;
25
use Kreta\Notifier\Domain\Model\Inbox\UserSignedUp;
26
use Kreta\Notifier\Domain\Model\Inbox\UserUnreadNotification;
27
use Kreta\SharedKernel\Domain\Model\AggregateRoot;
28
use Kreta\SharedKernel\Domain\Model\DomainEventCollection;
29
use Kreta\SharedKernel\Domain\Model\EventSourcedAggregateRoot;
30
use Kreta\SharedKernel\Event\EventStream;
31
use PhpSpec\ObjectBehavior;
32
33
class UserSpec extends ObjectBehavior
34
{
35
    function let(UserId $id)
36
    {
37
        $id->id()->willReturn('user-id');
38
        $this->beConstructedSignUp($id);
39
    }
40
41
    function it_can_be_signed_up()
42
    {
43
        $this->shouldHaveType(User::class);
44
        $this->shouldHaveType(AggregateRoot::class);
45
        $this->shouldImplement(EventSourcedAggregateRoot::class);
46
        $this->shouldHavePublished(UserSignedUp::class);
47
        $this->id()->shouldReturnAnInstanceOf(UserId::class);
48
        $this->__toString()->shouldReturn('user-id');
49
    }
50
51
    function it_can_be_reconstituted_user(
52
        EventStream $stream,
53
        UserId $id,
54
        UserSignedUp $userSignedUp,
55
        DomainEventCollection $collection
56
    ) {
57
        $collection->toArray()->willReturn([
58
            $userSignedUp,
59
        ]);
60
        $stream->events()->willReturn($collection);
61
        $stream->aggregateRootId()->shouldBeCalled()->willReturn($id);
62
        $this->beConstructedReconstitute($stream);
63
        $this->id()->shouldReturn($id);
64
        $id->id()->shouldBeCalled()->willReturn('user-id');
65
        $this->__toString()->shouldReturn('user-id');
66
    }
67
68
    function it_receives_notification(
69
        EventStream $stream,
70
        UserId $id,
71
        UserSignedUp $userSignedUp,
72
        DomainEventCollection $collection,
73
        NotificationId $notificationId,
74
        NotificationBody $body
75
    ) {
76
        $collection->toArray()->willReturn([
77
            $userSignedUp,
78
        ]);
79
        $stream->events()->willReturn($collection);
80
        $stream->aggregateRootId()->shouldBeCalled()->willReturn($id);
81
        $this->beConstructedReconstitute($stream);
82
        $this->receiveNotification($notificationId, $body);
83
        $this->shouldHavePublished(UserReceivedNotification::class);
84
    }
85
86 View Code Duplication
    function it_reads_notification(
0 ignored issues
show
Duplication introduced by
This method 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...
87
        EventStream $stream,
88
        UserId $id,
89
        UserSignedUp $userSignedUp,
90
        DomainEventCollection $collection,
91
        Notification $notification,
92
        NotificationId $notificationId,
93
        UserReceivedNotification $userReceivedNotification
94
    ) {
95
        $collection->toArray()->willReturn([
96
            $userSignedUp,
97
            $userReceivedNotification,
98
        ]);
99
        $stream->events()->willReturn($collection);
100
        $stream->aggregateRootId()->shouldBeCalled()->willReturn($id);
101
        $this->beConstructedReconstitute($stream);
102
        $notification->id()->shouldBeCalled()->willReturn($notificationId);
103
        $this->readNotification($notification);
104
        $this->shouldHavePublished(UserReadNotification::class);
105
    }
106
107 View Code Duplication
    function it_unreads_notification(
0 ignored issues
show
Duplication introduced by
This method 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...
108
        EventStream $stream,
109
        UserId $id,
110
        UserSignedUp $userSignedUp,
111
        DomainEventCollection $collection,
112
        Notification $notification,
113
        NotificationId $notificationId,
114
        UserReceivedNotification $userReceivedNotification,
115
        UserReadNotification $userReadNotification
116
    ) {
117
        $collection->toArray()->willReturn([
118
            $userSignedUp,
119
            $userReceivedNotification,
120
            $userReadNotification,
121
        ]);
122
        $stream->events()->willReturn($collection);
123
        $stream->aggregateRootId()->shouldBeCalled()->willReturn($id);
124
        $this->beConstructedReconstitute($stream);
125
        $notification->id()->shouldBeCalled()->willReturn($notificationId);
126
        $this->unreadNotification($notification);
127
        $this->shouldHavePublished(UserUnreadNotification::class);
128
    }
129
130
    function it_does_not_get_notification_when_it_does_not_exist(
131
        EventStream $stream,
132
        UserId $id,
133
        DomainEventCollection $collection,
134
        NotificationId $notificationId
135
    ) {
136
        $collection->toArray()->willReturn([]);
137
        $stream->events()->willReturn($collection);
138
        $stream->aggregateRootId()->shouldBeCalled()->willReturn($id);
139
        $this->beConstructedReconstitute($stream);
140
        $this->shouldThrow(NotificationDoesNotExist::class)->duringNotification($notificationId);
141
    }
142
143
    function it_gets_notification(
144
        EventStream $stream,
145
        UserId $id,
146
        DomainEventCollection $collection,
147
        NotificationId $notificationId,
148
        NotificationId $notificationId2,
149
        NotificationBody $body,
150
        UserReadNotification $userReadNotification
151
    ) {
152
        $collection->toArray()->willReturn([]);
153
        $stream->events()->willReturn($collection);
154
        $stream->aggregateRootId()->shouldBeCalled()->willReturn($id);
155
        $this->beConstructedReconstitute($stream);
156
        $this->receiveNotification($notificationId, $body);
157
        $userReadNotification->notificationId()->willReturn($notificationId);
158
        $notificationId2->equals($notificationId)->shouldBeCalled()->willReturn(true);
159
        $this->notification($notificationId2)->shouldReturnAnInstanceOf(Notification::class);
160
    }
161
}
162