Completed
Push — master ( 9abbc7...a687e5 )
by Fabrizio
03:03
created

it_create_multi_notification_in_a_loop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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)
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...
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) {
0 ignored issues
show
Unused Code introduced by
$closure is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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()
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...
175
    {
176
        $closure = function (NotifynderBuilder $builder, $data, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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()
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...
183
    {
184
        $closure = function (NotifynderBuilder $builder, $data, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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()
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...
191
    {
192
        $closure = function (NotifynderBuilder $builder, $data, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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