1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
<?php
namespace MediaWiki\Tests\Notification;
use MediaWiki\Notification\MiddlewareChain;
use MediaWiki\Notification\Notification;
use MediaWiki\Notification\NotificationHandler;
use MediaWiki\Notification\NotificationService;
use MediaWiki\Notification\RecipientSet;
use MediaWikiUnitTestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use RuntimeException;
/**
* @covers \MediaWiki\Notification\NotificationService
*/
class NotificationServiceTest extends MediaWikiUnitTestCase {
protected function getEmptyMiddleware() {
$middleware = $this->createMock( MiddlewareChain::class );
$middleware->expects( $this->any() )
->method( 'process' )
->willReturnArgument( 0 );
return $middleware;
}
public function testTriggersMiddleware() {
$notification = new Notification( 'test.middleware' );
$recipients = new RecipientSet( [] );
$middleware = $this->createMock( MiddlewareChain::class );
$middleware->expects( $this->once() )
->method( 'process' )
->willReturnCallback( function ( $batch ) {
$envelopes = iterator_to_array( $batch );
$this->assertCount( 1, $envelopes );
$this->assertSame( 'test.middleware', $envelopes[0]->getNotification()->getType() );
return $batch;
} );
$svc = new NotificationService(
new NullLogger(),
$this->createSimpleObjectFactory(),
$middleware,
[]
);
$svc->notify( $notification, $recipients );
}
public function testBasic() {
$recipients = new RecipientSet( [] );
$notifA = new Notification( 'A' );
$handlerA = $this->createMock( NotificationHandler::class );
$handlerA->expects( $this->once() )->method( 'notify' )->with( $notifA, $recipients );
$notifB = new Notification( 'B' );
$handlerB = $this->createMock( NotificationHandler::class );
$handlerB->expects( $this->once() )->method( 'notify' )->with( $notifB, $recipients );
$notifC = new Notification( 'C' );
$handlerC = $this->createMock( NotificationHandler::class );
$handlerC->expects( $this->once() )->method( 'notify' )->with( $notifC, $recipients );
$svc = new NotificationService(
new NullLogger(),
$this->createSimpleObjectFactory(),
$this->getEmptyMiddleware(),
[
[ 'types' => [ 'A' ], 'factory' => static fn () => $handlerA ],
[ 'types' => [ '*' ], 'factory' => static fn () => $handlerB ],
[ 'types' => [ 'C' ], 'factory' => static fn () => $handlerC ],
]
);
$svc->notify( $notifA, $recipients );
$svc->notify( $notifB, $recipients );
$svc->notify( $notifC, $recipients );
}
public function testBadWildcard() {
$this->expectException( RuntimeException::class );
$recipients = new RecipientSet( [] );
$notif = new Notification( 'A' );
$handler = $this->createNoOpMock( NotificationHandler::class );
$svc = new NotificationService(
new NullLogger(),
$this->createSimpleObjectFactory(),
$this->getEmptyMiddleware(),
[
[ 'types' => [ 'A/*' ], 'factory' => static fn () => $handler ],
]
);
$svc->notify( $notif, $recipients );
}
public function testDoubleDefinition() {
$this->expectException( RuntimeException::class );
$recipients = new RecipientSet( [] );
$notif = new Notification( 'A' );
$handler = $this->createNoOpMock( NotificationHandler::class );
$svc = new NotificationService(
new NullLogger(),
$this->createSimpleObjectFactory(),
$this->getEmptyMiddleware(),
[
[ 'types' => [ '*' ], 'factory' => static fn () => $handler ],
[ 'types' => [ '*' ], 'factory' => static fn () => $handler ],
]
);
$svc->notify( $notif, $recipients );
}
public function testMissingHandler() {
$mockLogger = $this->createMock( LoggerInterface::class );
$mockLogger->expects( $this->once() )->method( 'warning' );
$recipients = new RecipientSet( [] );
$notif = new Notification( 'A' );
$handler = $this->createNoOpMock( NotificationHandler::class );
$svc = new NotificationService(
$mockLogger,
$this->createSimpleObjectFactory(),
$this->getEmptyMiddleware(),
[
[ 'types' => [ 'B' ], 'factory' => static fn () => $handler ],
]
);
$svc->notify( $notif, $recipients );
}
}
|