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