-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNtfyTransportTest.php
More file actions
116 lines (92 loc) · 4.51 KB
/
NtfyTransportTest.php
File metadata and controls
116 lines (92 loc) · 4.51 KB
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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Notifier\Bridge\Ntfy\Tests;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\Notifier\Bridge\Ntfy\NtfyTransport;
use Symfony\Component\Notifier\Message\PushMessage;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Test\TransportTestCase;
use Symfony\Component\Notifier\Tests\Transport\DummyMessage;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
/**
* @author Mickael Perraud <[email protected]>
*/
final class NtfyTransportTest extends TransportTestCase
{
public static function createTransport(?HttpClientInterface $client = null): NtfyTransport
{
return new NtfyTransport('test', true, $client ?? new MockHttpClient());
}
public static function toStringProvider(): iterable
{
yield ['ntfy://ntfy.sh/test', self::createTransport()];
}
public static function supportedMessagesProvider(): iterable
{
yield [new PushMessage('Hello!', 'Symfony Notifier')];
}
public static function unsupportedMessagesProvider(): iterable
{
yield [new SmsMessage('0123456789', 'Hello!')];
yield [new DummyMessage()];
}
public function testCanSetCustomHost()
{
$transport = $this->createTransport();
$transport->setHost($customHost = self::CUSTOM_HOST);
$this->assertSame(\sprintf('ntfy://%s/test', $customHost), (string) $transport);
}
public function testCanSetCustomHostAndPort()
{
$transport = $this->createTransport();
$transport->setHost($customHost = self::CUSTOM_HOST);
$transport->setPort($customPort = self::CUSTOM_PORT);
$this->assertSame(\sprintf('ntfy://%s:%s/test', $customHost, $customPort), (string) $transport);
}
public function testSend()
{
$client = new MockHttpClient(function (string $method, string $url, array $options = []): ResponseInterface {
$expectedBody = json_encode(['topic' => 'test', 'title' => 'Hello', 'message' => 'World']);
$this->assertJsonStringEqualsJsonString($expectedBody, $options['body']);
return new MockResponse(json_encode(['id' => '2BYIwRmvBKcv', 'event' => 'message']));
});
$transport = $this->createTransport($client);
$sentMessage = $transport->send(new PushMessage('Hello', 'World'));
$this->assertSame('2BYIwRmvBKcv', $sentMessage->getMessageId());
}
public function testSendWithPassword()
{
$client = new MockHttpClient(function (string $method, string $url, array $options = []): ResponseInterface {
$expectedBody = json_encode(['topic' => 'test', 'title' => 'Hello', 'message' => 'World']);
$expectedAuthorization = 'Authorization: Bearer testtokentesttoken';
$this->assertJsonStringEqualsJsonString($expectedBody, $options['body']);
$this->assertTrue(\in_array($expectedAuthorization, $options['headers'], true));
return new MockResponse(json_encode(['id' => '2BYIwRmvBKcv', 'event' => 'message']));
});
$transport = $this->createTransport($client)->setPassword('testtokentesttoken');
$sentMessage = $transport->send(new PushMessage('Hello', 'World'));
$this->assertSame('2BYIwRmvBKcv', $sentMessage->getMessageId());
}
public function testSendWithUserAndPassword()
{
$client = new MockHttpClient(function (string $method, string $url, array $options = []): ResponseInterface {
$expectedBody = json_encode(['topic' => 'test', 'title' => 'Hello', 'message' => 'World']);
$expectedAuthorization = 'Authorization: Basic dGVzdF91c2VyOnRlc3RfcGFzc3dvcmQ=';
$this->assertJsonStringEqualsJsonString($expectedBody, $options['body']);
$this->assertTrue(\in_array($expectedAuthorization, $options['headers'], true));
return new MockResponse(json_encode(['id' => '2BYIwRmvBKcv', 'event' => 'message']));
});
$transport = $this->createTransport($client)->setUser('test_user')->setPassword('test_password');
$sentMessage = $transport->send(new PushMessage('Hello', 'World'));
$this->assertSame('2BYIwRmvBKcv', $sentMessage->getMessageId());
}
}