|
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() |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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
|
|
|
|
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.