| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Test\Unit; |
| 4 |
|
| 5 |
use MediaWiki\Extension\Notifications\Push\NotificationServiceClient; |
| 6 |
use MediaWiki\Extension\Notifications\Push\Subscription; |
| 7 |
use MediaWikiUnitTestCase; |
| 8 |
|
| 9 |
|
| 10 |
class NotificationServiceClientUnitTest extends MediaWikiUnitTestCase { |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
public function testSendCheckEchoRequests( $numOfCalls, $subscriptions, $expected ): void { |
| 16 |
$mock = $this->getMockBuilder( NotificationServiceClient::class ) |
| 17 |
->disableOriginalConstructor() |
| 18 |
->onlyMethods( [ 'sendRequest' ] ) |
| 19 |
->getMock(); |
| 20 |
|
| 21 |
$mock->expects( $this->exactly( $numOfCalls ) ) |
| 22 |
->method( 'sendRequest' ) |
| 23 |
->willReturnCallback( function ( $provider, $payload ) use ( &$expected ): void { |
| 24 |
$expectedIdx = array_search( [ $provider, $payload ], $expected, true ); |
| 25 |
$this->assertNotFalse( $expectedIdx, "Unexpected arguments (provider: $provider)" ); |
| 26 |
unset( $expected[$expectedIdx] ); |
| 27 |
} ); |
| 28 |
|
| 29 |
$mock->sendCheckEchoRequests( $subscriptions ); |
| 30 |
} |
| 31 |
|
| 32 |
public static function sendCheckEchoRequestsProvider(): array { |
| 33 |
$row = (object)[ |
| 34 |
'eps_token' => 'JKL123', |
| 35 |
'epp_name' => 'fcm', |
| 36 |
'ept_text' => null, |
| 37 |
'eps_updated' => '2020-01-01 10:10:10', |
| 38 |
]; |
| 39 |
$subscriptions[] = Subscription::newFromRow( $row ); |
| 40 |
|
| 41 |
$row = (object)[ |
| 42 |
'eps_token' => 'DEF456', |
| 43 |
'epp_name' => 'fcm', |
| 44 |
'ept_text' => null, |
| 45 |
'eps_updated' => '2020-01-01 10:10:10', |
| 46 |
]; |
| 47 |
$subscriptions[] = Subscription::newFromRow( $row ); |
| 48 |
|
| 49 |
$row = (object)[ |
| 50 |
'eps_token' => 'GHI789', |
| 51 |
'epp_name' => 'apns', |
| 52 |
'ept_text' => 'test', |
| 53 |
'eps_updated' => '2020-01-01 10:10:10', |
| 54 |
]; |
| 55 |
$subscriptions[] = Subscription::newFromRow( $row ); |
| 56 |
|
| 57 |
return [ |
| 58 |
[ |
| 59 |
1, |
| 60 |
[ $subscriptions[0], $subscriptions[1] ], |
| 61 |
[ |
| 62 |
[ |
| 63 |
'fcm', |
| 64 |
[ |
| 65 |
'deviceTokens' => [ "JKL123", 'DEF456' ], |
| 66 |
'messageType' => 'checkEchoV1' |
| 67 |
] |
| 68 |
] |
| 69 |
] |
| 70 |
], |
| 71 |
[ |
| 72 |
2, |
| 73 |
$subscriptions, |
| 74 |
[ |
| 75 |
[ |
| 76 |
'fcm', |
| 77 |
[ |
| 78 |
'deviceTokens' => [ "JKL123", 'DEF456' ], |
| 79 |
'messageType' => 'checkEchoV1' |
| 80 |
] |
| 81 |
], |
| 82 |
[ |
| 83 |
'apns', |
| 84 |
[ |
| 85 |
'deviceTokens' => [ 'GHI789' ], |
| 86 |
'messageType' => 'checkEchoV1', |
| 87 |
'topic' => 'test' |
| 88 |
] |
| 89 |
] |
| 90 |
] |
| 91 |
] |
| 92 |
]; |
| 93 |
} |
| 94 |
|
| 95 |
} |