|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace IrishDan\NotificationBundle\Test\Channel; |
|
4
|
|
|
|
|
5
|
|
|
use IrishDan\NotificationBundle\Channel\DefaultChannel; |
|
6
|
|
|
use IrishDan\NotificationBundle\Channel\EventChannel; |
|
7
|
|
|
use IrishDan\NotificationBundle\Exception\MessageDispatchException; |
|
8
|
|
|
use IrishDan\NotificationBundle\Test\NotificationTestCase; |
|
9
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
10
|
|
|
|
|
11
|
|
|
class EventChannelTest extends NotificationTestCase |
|
12
|
|
|
{ |
|
13
|
|
|
protected $eventChannel; |
|
14
|
|
|
protected $eventDispatcher; |
|
15
|
|
|
protected $notification; |
|
16
|
|
|
|
|
17
|
|
|
public function setUp() |
|
18
|
|
|
{ |
|
19
|
|
|
parent::setUp(); |
|
20
|
|
|
|
|
21
|
|
|
$this->notification = $this->getNotificationWithUser(); |
|
22
|
|
|
|
|
23
|
|
|
$this->eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class) |
|
24
|
|
|
->disableOriginalConstructor() |
|
25
|
|
|
->getMock(); |
|
26
|
|
|
|
|
27
|
|
|
$this->eventChannel = new EventChannel($this->eventDispatcher); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testFormat() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->eventDispatcher->expects($this->once())->method('dispatch'); |
|
33
|
|
|
|
|
34
|
|
|
$formatter = $this->getMockFormatter(true); |
|
35
|
|
|
$this->eventChannel->setDataFormatter($formatter); |
|
36
|
|
|
|
|
37
|
|
|
$message = $this->eventChannel->format($this->notification); |
|
38
|
|
|
|
|
39
|
|
|
$this->assertInstanceOf('IrishDan\NotificationBundle\Message\MessageInterface', $message); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
View Code Duplication |
public function testDispatch() |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
$this->eventDispatcher->expects($this->once())->method('dispatch'); |
|
45
|
|
|
|
|
46
|
|
|
$dispatcher = $this->getMockDispatcher(); |
|
47
|
|
|
$this->eventChannel->setDispatchers('default', $dispatcher); |
|
48
|
|
|
|
|
49
|
|
|
$message = $this->getTestMessage(); |
|
50
|
|
|
$message->setChannel('default'); |
|
51
|
|
|
|
|
52
|
|
|
$this->eventChannel->dispatch($message); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
View Code Duplication |
public function testDispatchWithWrongChannelKey() |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
$dispatcher = $this->getMockDispatcher(); |
|
58
|
|
|
$this->eventChannel->setDispatchers('default', $dispatcher); |
|
59
|
|
|
|
|
60
|
|
|
$message = $this->getTestMessage(); |
|
61
|
|
|
$message->setChannel('not-default'); |
|
62
|
|
|
|
|
63
|
|
|
$this->setExpectedException(MessageDispatchException::class); |
|
64
|
|
|
|
|
65
|
|
|
$this->eventChannel->dispatch($message); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
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.