|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace spec\Fenos\Notifynder\Builder; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Fenos\Notifynder\Builder\NotifynderBuilder; |
|
7
|
|
|
use Fenos\Notifynder\Categories\CategoryManager; |
|
8
|
|
|
use Fenos\Notifynder\Exceptions\EntityNotIterableException; |
|
9
|
|
|
use Fenos\Notifynder\Exceptions\IterableIsEmptyException; |
|
10
|
|
|
use Fenos\Notifynder\Models\NotificationCategory; |
|
11
|
|
|
use Illuminate\Support\Collection; |
|
12
|
|
|
use PhpSpec\ObjectBehavior; |
|
13
|
|
|
use Prophecy\Argument; |
|
14
|
|
|
|
|
15
|
|
|
class NotifynderBuilderSpec extends ObjectBehavior |
|
16
|
|
|
{ |
|
17
|
|
|
public function let(CategoryManager $category) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->beConstructedWith($category); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
function it_is_initializable() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->shouldHaveType('Fenos\Notifynder\Builder\NotifynderBuilder'); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** @test */ |
|
28
|
|
|
function it_set_the_FROM_value_with_a_single_entity() |
|
29
|
|
|
{ |
|
30
|
|
|
$user_id = 1; |
|
31
|
|
|
|
|
32
|
|
|
$this->from($user_id)->shouldReturnAnInstanceOf(NotifynderBuilder::class); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** @test */ |
|
36
|
|
|
function it_set_the_FROM_value_giving_a_polymorphic_entity() |
|
37
|
|
|
{ |
|
38
|
|
|
$user_id = 1; |
|
39
|
|
|
$user_class = 'User'; |
|
40
|
|
|
|
|
41
|
|
|
$this->from($user_class,$user_id)->shouldReturnAnInstanceOf(NotifynderBuilder::class); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** @test */ |
|
45
|
|
|
function it_set_the_FROM_value_giving_a_polymorphic_entity_the_first_value_must_be_the_class_entity() |
|
46
|
|
|
{ |
|
47
|
|
|
$user_id = 1; |
|
48
|
|
|
$user_class = 'User'; |
|
49
|
|
|
|
|
50
|
|
|
$this->shouldThrow('InvalidArgumentException')->during('from',[$user_id,$user_class]); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** @test */ |
|
54
|
|
|
function it_set_the_TO_value_with_a_single_entity() |
|
55
|
|
|
{ |
|
56
|
|
|
$user_id = 1; |
|
57
|
|
|
|
|
58
|
|
|
$this->to($user_id)->shouldReturnAnInstanceOf(NotifynderBuilder::class); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** @test */ |
|
62
|
|
|
function it_set_the_TO_value_giving_a_polymorphic_entity() |
|
63
|
|
|
{ |
|
64
|
|
|
$user_id = 1; |
|
65
|
|
|
$user_class = 'User'; |
|
66
|
|
|
|
|
67
|
|
|
$this->to($user_class,$user_id)->shouldReturnAnInstanceOf(NotifynderBuilder::class); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** @test */ |
|
71
|
|
|
function it_set_the_TO_value_giving_a_polymorphic_entity_the_first_value_must_be_the_class_entity() |
|
72
|
|
|
{ |
|
73
|
|
|
$user_id = 1; |
|
74
|
|
|
$user_class = 'User'; |
|
75
|
|
|
|
|
76
|
|
|
$this->shouldThrow('InvalidArgumentException')->during('to',[$user_id,$user_class]); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** @test */ |
|
80
|
|
|
function it_add_the_url_parameter_to_the_builder() |
|
81
|
|
|
{ |
|
82
|
|
|
$url = 'www.notifynder.io'; |
|
83
|
|
|
|
|
84
|
|
|
$this->url($url)->shouldReturnAnInstanceOf(NotifynderBuilder::class); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** @test */ |
|
88
|
|
|
function it_allow_only_string_as_url() |
|
89
|
|
|
{ |
|
90
|
|
|
$url = 1; |
|
91
|
|
|
|
|
92
|
|
|
$this->shouldThrow('InvalidArgumentException')->during('url',[$url]); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** @test */ |
|
96
|
|
|
function it_add_the_expire_parameter_to_the_builder() |
|
97
|
|
|
{ |
|
98
|
|
|
date_default_timezone_set('UTC'); |
|
99
|
|
|
|
|
100
|
|
|
$datetime = new Carbon; |
|
101
|
|
|
|
|
102
|
|
|
$this->expire($datetime)->shouldReturnAnInstanceOf(NotifynderBuilder::class); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** @test */ |
|
106
|
|
|
function it_allow_only_carbon_instance_as_expire_time() |
|
107
|
|
|
{ |
|
108
|
|
|
$datetime = 1; |
|
109
|
|
|
|
|
110
|
|
|
$this->shouldThrow('InvalidArgumentException')->during('expire',[$datetime]); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
/** @test */ |
|
115
|
|
|
function it_add_a_category_id_to_the_builder() |
|
116
|
|
|
{ |
|
117
|
|
|
$category_id = 1; |
|
118
|
|
|
|
|
119
|
|
|
$this->category($category_id)->shouldReturnAnInstanceOf(NotifynderBuilder::class); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** @test */ |
|
123
|
|
View Code Duplication |
function it_add_a_category_id_to_the_builder_givin_the_name_of_it(CategoryManager $category, NotificationCategory $categoryModel) |
|
|
|
|
|
|
124
|
|
|
{ |
|
125
|
|
|
$name = 'category.name'; |
|
126
|
|
|
$category_id = 1; |
|
127
|
|
|
|
|
128
|
|
|
$category->findByName($name)->shouldBeCalled() |
|
129
|
|
|
->willReturn($categoryModel); |
|
130
|
|
|
|
|
131
|
|
|
$categoryModel->getAttribute('id')->willReturn($category_id); |
|
132
|
|
|
|
|
133
|
|
|
$this->category($name)->shouldReturnAnInstanceOf(NotifynderBuilder::class); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** @test */ |
|
137
|
|
|
function it_add_the_extra_parameter_to_the_builder() |
|
138
|
|
|
{ |
|
139
|
|
|
$extra = ['my' => 'extra']; |
|
140
|
|
|
|
|
141
|
|
|
$this->extra($extra)->shouldReturnAnInstanceOf(NotifynderBuilder::class); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** @test */ |
|
145
|
|
|
function it_allow_only_associative_array_as_extra_parameter_they_llbe_converted_in_jon() |
|
146
|
|
|
{ |
|
147
|
|
|
$extra = ['my']; |
|
148
|
|
|
|
|
149
|
|
|
$this->shouldThrow('InvalidArgumentException')->during('extra',[$extra]); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** @test */ |
|
153
|
|
|
function it_create_a_builder_array_using_a_raw_closure() |
|
154
|
|
|
{ |
|
155
|
|
|
date_default_timezone_set('UTC'); |
|
156
|
|
|
|
|
157
|
|
|
$closure = function(NotifynderBuilder $builder) |
|
158
|
|
|
{ |
|
159
|
|
|
return $builder->to(1)->from(2)->url('notifynder.io')->category(1); |
|
160
|
|
|
}; |
|
161
|
|
|
|
|
162
|
|
|
$this->raw($closure)->shouldHaveKey('to_id'); |
|
163
|
|
|
$this->raw($closure)->shouldHaveKey('from_id'); |
|
164
|
|
|
$this->raw($closure)->shouldHaveKey('url'); |
|
165
|
|
|
$this->raw($closure)->shouldHaveKey('category_id'); |
|
166
|
|
|
$this->raw($closure)->shouldHaveKey('created_at'); |
|
167
|
|
|
$this->raw($closure)->shouldHaveKey('updated_at'); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function it_create_multi_notification_in_a_loop() |
|
171
|
|
|
{ |
|
172
|
|
|
$closure = function(NotifynderBuilder $builder,$data,$key) |
|
|
|
|
|
|
173
|
|
|
{ |
|
174
|
|
|
return $builder->to(1)->from(2)->url('notifynder.io')->category(1); |
|
175
|
|
|
}; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
View Code Duplication |
public function it_create_empty_array_loop_builder() |
|
|
|
|
|
|
179
|
|
|
{ |
|
180
|
|
|
$closure = function(NotifynderBuilder $builder,$data,$key) |
|
|
|
|
|
|
181
|
|
|
{ |
|
182
|
|
|
return $builder->to(1)->from(2)->url('notifynder.io')->category(1); |
|
183
|
|
|
}; |
|
184
|
|
|
$this->shouldThrow(IterableIsEmptyException::class)->during('loop', [[], $closure]); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
View Code Duplication |
public function it_create_empty_collection_loop_builder() |
|
|
|
|
|
|
188
|
|
|
{ |
|
189
|
|
|
$closure = function(NotifynderBuilder $builder,$data,$key) |
|
|
|
|
|
|
190
|
|
|
{ |
|
191
|
|
|
return $builder->to(1)->from(2)->url('notifynder.io')->category(1); |
|
192
|
|
|
}; |
|
193
|
|
|
$this->shouldThrow(IterableIsEmptyException::class)->during('loop', [new Collection([]), $closure]); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
View Code Duplication |
public function it_create_not_iterable_loop_builder() |
|
|
|
|
|
|
197
|
|
|
{ |
|
198
|
|
|
$closure = function(NotifynderBuilder $builder,$data,$key) |
|
|
|
|
|
|
199
|
|
|
{ |
|
200
|
|
|
return $builder->to(1)->from(2)->url('notifynder.io')->category(1); |
|
201
|
|
|
}; |
|
202
|
|
|
$this->shouldThrow(EntityNotIterableException::class)->during('loop', ['hello world', $closure]); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
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.