Completed
Pull Request — master (#157)
by
unknown
02:50
created

NotifynderParserSpec   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 138
Duplicated Lines 45.65 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 8
c 5
b 1
f 1
lcom 1
cbo 3
dl 63
loc 138
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A it_is_initializable() 0 4 1
A it_should_replace_special_values_with_an_associative_array() 0 13 1
A it_replace_from_values_relations() 14 14 1
A it_replace_both_in_a_string() 16 16 1
A it_replace_property_name_of_current_object() 16 16 1
A it_will_remove_extra_markup_if_extra_value_is_not_provided() 17 17 1
A it_will_throw_exception_when_strict_extra_is_enabled() 0 19 1
A it_will_parse_4_extra_params() 0 20 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace spec\Fenos\Notifynder\Parsers;
4
5
use Fenos\Notifynder\Exceptions\ExtraParamsException;
6
use Fenos\Notifynder\Parsers\NotifynderParser;
7
use PhpSpec\ObjectBehavior;
8
9
class NotifynderParserSpec extends ObjectBehavior
10
{
11
    public $url;
12
13
    public function it_is_initializable()
14
    {
15
        $this->shouldHaveType('Fenos\Notifynder\Parsers\NotifynderParser');
16
    }
17
18
    /** @test */
19
    public function it_should_replace_special_values_with_an_associative_array()
20
    {
21
        $extra = ['hello' => 'world'];
22
23
        $notification = [
24
            'body' => [
25
                'text' => 'Hi jhon hello {extra.hello}',
26
            ],
27
            'extra' => json_encode($extra),
28
        ];
29
30
        $this->parse($notification)->shouldReturn('Hi jhon hello world');
31
    }
32
33
    /** @test */
34 View Code Duplication
    public function it_replace_from_values_relations()
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...
35
    {
36
        $notification = [
37
            'body' => [
38
                'text' => 'Hi {to.username} hello',
39
            ],
40
            'to' => [
41
                'username' => 'jhon',
42
            ],
43
            'extra' => null,
44
        ];
45
46
        $this->parse($notification)->shouldReturn('Hi jhon hello');
47
    }
48
49
    /** @test */
50 View Code Duplication
    public function it_replace_both_in_a_string()
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...
51
    {
52
        $extra = ['hello' => 'world'];
53
54
        $notification = [
55
            'body' => [
56
                'text' => 'Hi {to.username} hello {extra.hello}',
57
            ],
58
            'to' => [
59
                'username' => 'jhon',
60
            ],
61
            'extra' => json_encode($extra),
62
        ];
63
64
        $this->parse($notification)->shouldReturn('Hi jhon hello world');
65
    }
66
67
    /** @test */
68 View Code Duplication
    public function it_replace_property_name_of_current_object()
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...
69
    {
70
        $this->url = 'http://localhost';
71
72
        $notification = [
73
            'body' => [
74
                'text' => '{from.username} replied to <a href="{url}">your post</a>',
75
            ],
76
            'from' => [
77
                'username' => 'jhon',
78
            ],
79
            'extra' => null,
80
        ];
81
82
        $this->parse($notification)->shouldReturn('jhon replied to <a href="http://localhost">your post</a>');
83
    }
84
85
    /** @test */
86 View Code Duplication
    public function it_will_remove_extra_markup_if_extra_value_is_not_provided()
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...
87
    {
88
        $extra = [];
89
90
        $notification = [
91
            'body' => [
92
                'text' => 'Hi {to.username} hello {extra.hello}',
93
            ],
94
            'to' => [
95
                'username' => 'jhon',
96
            ],
97
            'extra' => json_encode($extra),
98
        ];
99
100
        // note the space, TODO: shall i remove it?
101
        $this->parse($notification)->shouldReturn('Hi jhon hello ');
102
    }
103
104
    /** @test */
105
    public function it_will_throw_exception_when_strict_extra_is_enabled()
106
    {
107
        $extra = null;
108
109
        $notification = [
110
            'body' => [
111
                'text' => 'Hi {to.username} hello {extra.hello}',
112
            ],
113
            'to' => [
114
                'username' => 'jhon',
115
            ],
116
            'extra' => json_encode($extra),
117
        ];
118
119
        NotifynderParser::setStrictExtra(true);
120
121
        $this->shouldThrow(ExtraParamsException::class)
122
            ->during('parse', [$notification]);
123
    }
124
125
    /** @test */
126
    public function it_will_parse_4_extra_params()
127
    {
128
        $extra = [
129
            'name' => 'fabri',
130
            'username' => 'fenos',
131
            'status' => 'active',
132
            'prof' => 'dev',
133
        ];
134
135
        $text = 'Hi {extra.name}, your username is: {extra.username} your status: {extra.status} your profession: {extra.prof}';
136
        $notification = [
137
            'body' => [
138
                'text' => $text,
139
            ],
140
            'extra' => json_encode($extra),
141
        ];
142
143
        $parsedText = "Hi {$extra['name']}, your username is: {$extra['username']} your status: {$extra['status']} your profession: {$extra['prof']}";
144
        $this->parse($notification)->shouldReturn($parsedText);
145
    }
146
}
147