1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace spec\Fenos\Notifynder\Senders; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use Fenos\Notifynder\Contracts\DefaultSender; |
7
|
|
|
use Fenos\Notifynder\Contracts\StoreNotification; |
8
|
|
|
use Fenos\Notifynder\Senders\SenderFactory; |
9
|
|
|
use Illuminate\Contracts\Foundation\Application; |
10
|
|
|
use PhpSpec\ObjectBehavior; |
11
|
|
|
|
12
|
|
|
class SenderManagerSpec extends ObjectBehavior |
13
|
|
|
{ |
14
|
|
|
public function let(SenderFactory $senderFactory, StoreNotification $storeNotification, Application $application) |
15
|
|
|
{ |
16
|
|
|
$this->beConstructedWith($senderFactory, $storeNotification, $application); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function it_is_initializable() |
20
|
|
|
{ |
21
|
|
|
$this->shouldHaveType('Fenos\Notifynder\Senders\SenderManager'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** @test */ |
25
|
|
View Code Duplication |
public function it_send_now_a_notification(SenderFactory $senderFactory, |
|
|
|
|
26
|
|
|
DefaultSender $sender, |
27
|
|
|
StoreNotification $storeNotification) |
28
|
|
|
{ |
29
|
|
|
$notifications = []; |
30
|
|
|
$category = 1; |
31
|
|
|
|
32
|
|
|
$senderFactory->getSender($notifications, $category)->shouldBeCalled() |
33
|
|
|
->willReturn($sender); |
34
|
|
|
|
35
|
|
|
$sender->send($storeNotification)->shouldBeCalled() |
36
|
|
|
->willReturn(1); |
37
|
|
|
|
38
|
|
|
$this->sendNow($notifications, $category)->shouldReturn(1); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** @test */ |
42
|
|
View Code Duplication |
public function it_send_one_notification(SenderFactory $senderFactory, |
|
|
|
|
43
|
|
|
DefaultSender $sender, |
44
|
|
|
StoreNotification $storeNotification) |
45
|
|
|
{ |
46
|
|
|
$notifications = []; |
47
|
|
|
$category = 1; |
48
|
|
|
|
49
|
|
|
$senderFactory->sendSingle($notifications, $category)->shouldBeCalled() |
50
|
|
|
->willReturn($sender); |
51
|
|
|
|
52
|
|
|
$sender->send($storeNotification, $category)->shouldBeCalled() |
53
|
|
|
->willReturn(1); |
54
|
|
|
|
55
|
|
|
$this->sendOne($notifications, $category)->shouldReturn(1); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** @test */ |
59
|
|
|
public function it_send_multiple_notification(SenderFactory $senderFactory, |
60
|
|
|
DefaultSender $sender, |
61
|
|
|
StoreNotification $storeNotification) |
62
|
|
|
{ |
63
|
|
|
$notifications = []; |
64
|
|
|
|
65
|
|
|
$senderFactory->sendMultiple($notifications)->shouldBeCalled() |
66
|
|
|
->willReturn($sender); |
67
|
|
|
|
68
|
|
|
$sender->send($storeNotification)->shouldBeCalled() |
69
|
|
|
->willReturn(1); |
70
|
|
|
|
71
|
|
|
$this->sendMultiple($notifications)->shouldReturn(1); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** @test */ |
75
|
|
|
public function it_call_an_extended_method(SenderFactory $senderFactory, |
|
|
|
|
76
|
|
|
DefaultSender $sender, |
77
|
|
|
StoreNotification $storeNotification) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$notifications = []; |
80
|
|
|
|
81
|
|
|
$this->extend('sendExtended', function ($app) use ($sender) { |
|
|
|
|
82
|
|
|
return new TestExtender(); |
83
|
|
|
}); |
84
|
|
|
|
85
|
|
|
$this->sendExtended($notifications)->shouldReturn([]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** @test */ |
89
|
|
|
public function it_try_to_call_an_inexistent_extended_method() |
90
|
|
|
{ |
91
|
|
|
$this->shouldThrow(BadMethodCallException::class)->during('NotExistingExtender', []); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/* |
96
|
|
|
|-------------------------------------------------------------------------- |
97
|
|
|
| Extended class |
98
|
|
|
|-------------------------------------------------------------------------- |
99
|
|
|
| Test Extender sender class |
100
|
|
|
--------------------------------------------------------------------------*/ |
101
|
|
|
|
102
|
|
|
class TestExtender implements DefaultSender |
103
|
|
|
{ |
104
|
|
|
/** |
105
|
|
|
* Send Single notification. |
106
|
|
|
* |
107
|
|
|
* @param StoreNotification $sender |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
|
|
public function send(StoreNotification $sender) |
111
|
|
|
{ |
112
|
|
|
return []; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
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.