1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Notification\Tests; |
4
|
|
|
|
5
|
|
|
use Modules\Notification\Entities\Notification; |
6
|
|
|
use Modules\Notification\Repositories\NotificationRepository; |
7
|
|
|
|
8
|
|
|
class EloquentNotificationRepositoryTest extends BaseTestCase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var NotificationRepository |
12
|
|
|
*/ |
13
|
|
|
private $notification; |
14
|
|
|
|
15
|
|
|
public function setUp() |
16
|
|
|
{ |
17
|
|
|
parent::setUp(); |
18
|
|
|
$this->notification = app(NotificationRepository::class); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** @test */ |
22
|
|
|
public function it_can_create_a_notification() |
23
|
|
|
{ |
24
|
|
|
$notification = $this->notification->create([ |
25
|
|
|
'user_id' => 1, |
26
|
|
|
'icon_class' => 'fa fa-link', |
27
|
|
|
'link' => 'http://localhost/users', |
28
|
|
|
'title' => 'My notification', |
29
|
|
|
'message' => 'Is awesome!', |
30
|
|
|
]); |
31
|
|
|
|
32
|
|
|
$this->assertCount(1, $this->notification->all()); |
33
|
|
|
$this->assertEquals('1' , $notification->user_id); |
34
|
|
|
$this->assertEquals('fa fa-link' , $notification->icon_class); |
35
|
|
|
$this->assertEquals('http://localhost/users' , $notification->link); |
36
|
|
|
$this->assertEquals('My notification' , $notification->title); |
37
|
|
|
$this->assertEquals('Is awesome!' , $notification->message); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** @test */ |
41
|
|
View Code Duplication |
public function it_can_fetch_latest_notification_for_user() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$this->createNotification(); |
44
|
|
|
$this->createNotification(); |
45
|
|
|
$this->createNotification(); |
46
|
|
|
$this->createNotification(); |
47
|
|
|
$this->createNotification(); |
48
|
|
|
$this->createNotification(); |
49
|
|
|
$this->createNotification(); |
50
|
|
|
$this->createNotification(); |
51
|
|
|
$this->createNotification(); |
52
|
|
|
$this->createNotification(); |
53
|
|
|
$this->createNotification(); |
54
|
|
|
$this->createNotification(['user_id' => 2]); |
55
|
|
|
$this->createNotification(['user_id' => 2]); |
56
|
|
|
|
57
|
|
|
$this->assertCount(10, $this->notification->latestForUser(1)); |
58
|
|
|
$this->assertCount(2, $this->notification->latestForUser(2)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** @test */ |
62
|
|
View Code Duplication |
public function it_can_get_all_notifications_for_user() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$this->createNotification(); |
65
|
|
|
$this->createNotification(); |
66
|
|
|
$this->createNotification(); |
67
|
|
|
$this->createNotification(); |
68
|
|
|
$this->createNotification(); |
69
|
|
|
$this->createNotification(); |
70
|
|
|
$this->createNotification(); |
71
|
|
|
$this->createNotification(); |
72
|
|
|
$this->createNotification(); |
73
|
|
|
$this->createNotification(); |
74
|
|
|
$this->createNotification(); |
75
|
|
|
$this->createNotification(['user_id' => 2]); |
76
|
|
|
$this->createNotification(['user_id' => 2]); |
77
|
|
|
|
78
|
|
|
$this->assertCount(11, $this->notification->allForUser(1)); |
79
|
|
|
$this->assertCount(2, $this->notification->allForUser(2)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** @test */ |
83
|
|
|
public function it_can_mark_a_notification_as_read() |
84
|
|
|
{ |
85
|
|
|
$notification = $this->createNotification(); |
86
|
|
|
|
87
|
|
|
$this->assertFalse($notification->isRead()); |
88
|
|
|
$this->notification->markNotificationAsRead($notification->id); |
89
|
|
|
|
90
|
|
|
$notification->refresh(); |
91
|
|
|
$this->assertTrue($notification->isRead()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** @test */ |
95
|
|
View Code Duplication |
public function it_can_get_all_unread_notifications_for_user() |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$this->createNotification(); |
98
|
|
|
$this->createNotification(['is_read' => true]); |
99
|
|
|
$this->createNotification(['is_read' => true]); |
100
|
|
|
$this->createNotification(); |
101
|
|
|
|
102
|
|
|
$this->assertCount(2, $this->notification->allUnreadForUser(1)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** @test */ |
106
|
|
View Code Duplication |
public function it_can_get_all_read_notifications_for_user() |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$this->createNotification(); |
109
|
|
|
$this->createNotification(['is_read' => true]); |
110
|
|
|
$this->createNotification(['is_read' => true]); |
111
|
|
|
$this->createNotification(); |
112
|
|
|
$this->createNotification(); |
113
|
|
|
|
114
|
|
|
$this->assertCount(2, $this->notification->allReadForUser(1)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** @test */ |
118
|
|
|
public function it_can_delete_all_notifications_for_user() |
119
|
|
|
{ |
120
|
|
|
$this->createNotification(); |
121
|
|
|
$this->createNotification(); |
122
|
|
|
$this->createNotification(); |
123
|
|
|
$this->createNotification(['user_id' => 2]); |
124
|
|
|
$this->createNotification(['user_id' => 2]); |
125
|
|
|
|
126
|
|
|
$this->assertCount(3, $this->notification->allForUser(1)); |
127
|
|
|
$this->assertCount(2, $this->notification->allForUser(2)); |
128
|
|
|
|
129
|
|
|
$this->notification->deleteAllForUser(1); |
130
|
|
|
|
131
|
|
|
$this->assertCount(0, $this->notification->allForUser(1)); |
132
|
|
|
$this->assertCount(2, $this->notification->allForUser(2)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** @test */ |
136
|
|
|
public function it_can_mark_all_notifications_read_for_user() |
137
|
|
|
{ |
138
|
|
|
$this->createNotification(); |
139
|
|
|
$this->createNotification(); |
140
|
|
|
$this->createNotification(); |
141
|
|
|
$this->createNotification(['user_id' => 2]); |
142
|
|
|
$this->createNotification(['user_id' => 2]); |
143
|
|
|
|
144
|
|
|
$this->assertCount(0, $this->notification->allReadForUser(1)); |
145
|
|
|
|
146
|
|
|
$this->notification->markAllAsReadForUser(1); |
147
|
|
|
$this->assertCount(3, $this->notification->allReadForUser(1)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
private function createNotification(array $properties = []) : Notification |
151
|
|
|
{ |
152
|
|
|
$data = [ |
153
|
|
|
'user_id' => 1, |
154
|
|
|
'icon_class' => 'fa fa-link', |
155
|
|
|
'link' => 'http://localhost/users', |
156
|
|
|
'title' => 'My notification', |
157
|
|
|
'message' => 'Is awesome!', |
158
|
|
|
]; |
159
|
|
|
|
160
|
|
|
return $this->notification->create(array_merge($data, $properties)); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
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.