Completed
Push — master ( 7fe5eb...9bad48 )
by Beñat
08:54 queued 04:27
created

PublishNotificationSpec   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 14 1
A it_does_not_publish_notification_when_the_notification_already_exists() 0 11 1
A it_does_not_publish_notification_when_the_user_does_not_exist() 0 9 1
A it_publishes_notification() 0 11 1
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\PublishNotificationCommand;
18
use Kreta\Notifier\Domain\Model\Inbox\Notification\Notification;
19
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationAlreadyExists;
20
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationDoesNotExist;
21
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationId;
22
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationRepository;
23
use Kreta\Notifier\Domain\Model\Inbox\User;
24
use Kreta\Notifier\Domain\Model\Inbox\UserDoesNotExist;
25
use Kreta\Notifier\Domain\Model\Inbox\UserId;
26
use Kreta\Notifier\Domain\Model\Inbox\UserRepository;
27
use PhpSpec\ObjectBehavior;
28
use Prophecy\Argument;
29
30
class PublishNotificationSpec extends ObjectBehavior
31
{
32
    private $notificationId;
33
    private $userId;
34
35
    function let(
36
        NotificationRepository $repository,
37
        UserRepository $userRepository,
38
        PublishNotificationCommand $command
39
    ) {
40
        $this->beConstructedWith($repository, $userRepository);
41
42
        $command->notificationId()->shouldBeCalled()->willReturn('notification-id');
43
        $command->userId()->shouldBeCalled()->willReturn('user-id');
44
        $command->body()->shouldBeCalled()->willReturn('The notification body');
45
46
        $this->notificationId = NotificationId::generate('notification-id');
47
        $this->userId = UserId::generate('user-id');
48
    }
49
50
    function it_does_not_publish_notification_when_the_notification_already_exists(
51
        PublishNotificationCommand $command,
52
        NotificationRepository $repository,
53
        Notification $notification,
54
        NotificationId $notificationId
55
    ) {
56
        $repository->get($this->notificationId)->shouldBeCalled()->willReturn($notification);
57
        $notification->id()->shouldBeCalled()->willReturn($notificationId);
58
        $notificationId->id()->willReturn('notification-id');
59
        $this->shouldThrow(NotificationAlreadyExists::class)->during__invoke($command);
60
    }
61
62
    function it_does_not_publish_notification_when_the_user_does_not_exist(
63
        PublishNotificationCommand $command,
64
        NotificationRepository $repository,
65
        UserRepository $userRepository
66
    ) {
67
        $repository->get($this->notificationId)->shouldBeCalled()->willThrow(NotificationDoesNotExist::class);
68
        $userRepository->get($this->userId)->shouldBeCalled()->willThrow(UserDoesNotExist::class);
69
        $this->shouldThrow(UserDoesNotExist::class)->during__invoke($command);
70
    }
71
72
    function it_publishes_notification(
73
        PublishNotificationCommand $command,
74
        NotificationRepository $repository,
75
        UserRepository $userRepository,
76
        User $user
77
    ) {
78
        $repository->get($this->notificationId)->shouldBeCalled()->willThrow(NotificationDoesNotExist::class);
79
        $userRepository->get($this->userId)->shouldBeCalled()->willReturn($user);
80
        $repository->save(Argument::type(Notification::class))->shouldBeCalled();
81
        $this->__invoke($command);
82
    }
83
}
84