Completed
Push — issue-118 ( 68e7b7...8e2bf8 )
by
unknown
02:25
created

it_create_not_iterable_loop_builder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 8
loc 8
rs 9.4285
cc 1
eloc 4
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
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)
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...
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)
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...
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()
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...
179
    {
180
        $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...
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()
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...
188
    {
189
        $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...
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()
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...
197
    {
198
        $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...
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