1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IrishDan\NotificationBundle\Test; |
4
|
|
|
|
5
|
|
|
use IrishDan\NotificationBundle\Adapter\MessageAdapterInterface; |
6
|
|
|
use IrishDan\NotificationBundle\Test\Notification\TestNotification; |
7
|
|
|
use IrishDan\NotificationBundle\Dispatcher\MessageDispatcherInterface; |
8
|
|
|
use IrishDan\NotificationBundle\Formatter\MessageFormatterInterface; |
9
|
|
|
use IrishDan\NotificationBundle\Message\Message; |
10
|
|
|
use IrishDan\NotificationBundle\Test\Entity\User; |
11
|
|
|
use Symfony\Component\Yaml\Yaml; |
12
|
|
|
|
13
|
|
|
class NotificationTestCase extends \PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
protected $testKernel; |
16
|
|
|
protected $parameters = []; |
17
|
|
|
|
18
|
|
|
protected function bootSymfony() |
19
|
|
|
{ |
20
|
|
|
require_once __DIR__ . '/AppKernel.php'; |
21
|
|
|
|
22
|
|
|
$this->testKernel = new \AppKernel('test', true); |
23
|
|
|
$this->testKernel->boot(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function getTestUser() |
27
|
|
|
{ |
28
|
|
|
$user = new User(); |
29
|
|
|
|
30
|
|
|
return $user; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function getTestNotification() |
34
|
|
|
{ |
35
|
|
|
$notification = new TestNotification(); |
36
|
|
|
|
37
|
|
|
return $notification; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function getNotificationWithUser() |
41
|
|
|
{ |
42
|
|
|
$user = $this->getTestUser(); |
43
|
|
|
$notification = $this->getTestNotification(); |
44
|
|
|
|
45
|
|
|
$notification->setNotifiable($user); |
46
|
|
|
|
47
|
|
|
return $notification; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function getContainer() |
51
|
|
|
{ |
52
|
|
|
if (empty($this->testKernel)) { |
53
|
|
|
$this->bootSymfony(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this->testKernel->getContainer(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function getMockAdapter($withFormat = false, $withDispatch = false) |
60
|
|
|
{ |
61
|
|
|
$adapter = $this->getMockBuilder(MessageAdapterInterface::class) |
62
|
|
|
->disableOriginalConstructor() |
63
|
|
|
->getMock(); |
64
|
|
|
|
65
|
|
|
// Adapter needs to have the configuration set |
66
|
|
|
// $adapter->expects($this->once()) |
|
|
|
|
67
|
|
|
// ->method('setConfiguration'); |
|
|
|
|
68
|
|
|
|
69
|
|
|
// Adapter needs to have the channelName set |
70
|
|
|
// $adapter->expects($this->once()) |
|
|
|
|
71
|
|
|
// ->method('setChannelName'); |
|
|
|
|
72
|
|
|
|
73
|
|
|
if ($withFormat) { |
74
|
|
|
$adapter->expects($this->once()) |
75
|
|
|
->method('format') |
76
|
|
|
->will($this->returnValue($this->getTestMessage())); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($withDispatch) { |
80
|
|
|
$adapter->expects($this->once()) |
81
|
|
|
->method('dispatch') |
82
|
|
|
->will($this->returnValue(true)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $adapter; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function getTestMessage() |
89
|
|
|
{ |
90
|
|
|
$message = new Message(); |
91
|
|
|
$message->setDispatchData(['mail' => '[email protected]']); |
92
|
|
|
$message->setMessageData( |
93
|
|
|
[ |
94
|
|
|
'title' => 'Hi!', |
95
|
|
|
'body' => 'Hi Jim, this is a notification', |
96
|
|
|
] |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
return $message; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function getService($serviceName) |
103
|
|
|
{ |
104
|
|
|
$container = $this->getContainer(); |
105
|
|
|
|
106
|
|
|
return $container->get($serviceName); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function getParametersFromContainer($parameter) |
110
|
|
|
{ |
111
|
|
|
$container = $this->getContainer(); |
112
|
|
|
|
113
|
|
|
return $container->getParameter($parameter); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function getParameters($key = '') |
117
|
|
|
{ |
118
|
|
|
if (empty($this->parameters)) { |
119
|
|
|
$path = __DIR__ . '/config_test.yml'; |
120
|
|
|
$this->parameters = Yaml::parse(file_get_contents($path)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (empty($key)) { |
124
|
|
|
return $this->parameters; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return empty($this->parameters[$key]) ? [] : $this->parameters[$key]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function getNotificationChannelConfiguration($key = '') |
131
|
|
|
{ |
132
|
|
|
$config = $this->getParameters('notification'); |
133
|
|
|
$config = $config['channels']; |
134
|
|
|
|
135
|
|
|
if (!empty($key)) { |
136
|
|
|
$config = empty($config[$key]) ? [] : $config[$key]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $config; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
protected function getToken() |
143
|
|
|
{ |
144
|
|
|
return $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); |
145
|
|
|
} |
146
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.