Skip to content

Commit 0053a07

Browse files
authored
Merge pull request #9 from ker0x/feature/tests
Improve tests
2 parents 2e6b144 + 20c2357 commit 0053a07

File tree

10 files changed

+75
-18
lines changed

10 files changed

+75
-18
lines changed

src/Helper/ValidatorTrait.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace Kerox\Fcm\Helper;
66

7-
use InvalidArgumentException;
8-
97
trait ValidatorTrait
108
{
119
/**
@@ -15,7 +13,7 @@ public function isValidData(array $data): void
1513
{
1614
foreach ($data as $key => $value) {
1715
if (!\is_string($key) || !\is_string($value)) {
18-
throw new InvalidArgumentException('Array must only contain string for key and value.');
16+
throw new \InvalidArgumentException('Array must only contain string for key and value.');
1917
}
2018
}
2119
}
@@ -26,7 +24,7 @@ public function isValidData(array $data): void
2624
public function isValidTtl(string $ttl): void
2725
{
2826
if (!preg_match('/^\d+(\.\d{1,9})?s$/', $ttl)) {
29-
throw new InvalidArgumentException('Invalid TTL format.');
27+
throw new \InvalidArgumentException('Invalid TTL format.');
3028
}
3129
}
3230

@@ -36,7 +34,7 @@ public function isValidTtl(string $ttl): void
3634
public function isValidLang(string $lang): void
3735
{
3836
if (!preg_match('/^[a-z]{2}-[A-Z]{2}$/', $lang)) {
39-
throw new InvalidArgumentException('Invalid lang format.');
37+
throw new \InvalidArgumentException('Invalid lang format.');
4038
}
4139
}
4240

@@ -47,7 +45,7 @@ public function isValidVibratePattern(array $vibratePattern): void
4745
{
4846
foreach ($vibratePattern as $pattern) {
4947
if (!\is_int($pattern)) {
50-
throw new InvalidArgumentException('Vibrate pattern must only contain integer.');
48+
throw new \InvalidArgumentException('Vibrate pattern must only contain integer.');
5149
}
5250
}
5351
}
@@ -61,7 +59,7 @@ protected function isValidUrl(string $url): void
6159
'/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/=]*)$/',
6260
$url
6361
)) {
64-
throw new InvalidArgumentException(sprintf('%s is not a valid url.', $url));
62+
throw new \InvalidArgumentException(sprintf('"%s" is not a valid url.', $url));
6563
}
6664
}
6765

@@ -71,7 +69,7 @@ protected function isValidUrl(string $url): void
7169
public function isValidTopicName(string $topic): void
7270
{
7371
if (!preg_match('/^[a-zA-Z0-9-_.~%]+$/', $topic)) {
74-
throw new InvalidArgumentException(sprintf('%s is an invalid topic name.', $topic));
72+
throw new \InvalidArgumentException(sprintf('"%s" is an invalid topic name.', $topic));
7573
}
7674
}
7775
}

src/Model/Message.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function __construct($message)
8484
}
8585

8686
if (!$message instanceof Notification) {
87-
throw new \InvalidArgumentException(sprintf('$message must be a string or an instance of %s.', Notification::class));
87+
throw new \InvalidArgumentException(sprintf('$message must be a string or an instance of "%s".', Notification::class));
8888
}
8989

9090
$this->notification = $message;

src/Model/Message/Notification/ApnsNotification.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function setAlert($alert): self
6868
}
6969

7070
if (!$alert instanceof Alert) {
71-
throw new InvalidArgumentException(sprintf('alert must be a string or an instance of %s.', Alert::class));
71+
throw new InvalidArgumentException(sprintf('alert must be a string or an instance of "%s".', Alert::class));
7272
}
7373

7474
$this->alert = $alert;

src/Model/Message/Webpush.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function setOptions($options): self
7575
{
7676
if (\is_array($options)) {
7777
trigger_error(sprintf(
78-
'Using array to set options is deprecated since version 2.1 and will be remove in version 3.0, use class %s instead.',
78+
'Using array to set options is deprecated since version 2.1 and will be remove in version 3.0, use class "%s" instead.',
7979
WebpushOptions::class
8080
), \E_USER_WARNING);
8181

tests/Mocks/Model/message.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"name": "fcm",
23
"data": {
34
"story_id": "story_12345"
45
},
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "fcm",
3+
"data": {
4+
"story_id": "story_12345"
5+
},
6+
"notification": {
7+
"title": "Breaking News"
8+
},
9+
"topic": "TopicA"
10+
}

tests/TestCase/Model/Message/ConditionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function testConditionWithMultipleClosure(): void
7171
public function testConditionAndWithInvalidTopics(): void
7272
{
7373
$this->expectException(\InvalidArgumentException::class);
74-
$this->expectExceptionMessage('Topic A is an invalid topic name.');
74+
$this->expectExceptionMessage('"Topic A" is an invalid topic name.');
7575

7676
(new Condition)->and('Topic A', 'TopicB', 'TopicC');
7777
}

tests/TestCase/Model/Message/Notification/ApnsNotificationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ApnsNotificationTest extends AbstractTestCase
1010
public function testApnsNotificationWithInvalidAlert(): void
1111
{
1212
$this->expectException(\InvalidArgumentException::class);
13-
$this->expectExceptionMessage('alert must be a string or an instance of Kerox\Fcm\Model\Message\Notification\ApnsNotification\Alert.');
13+
$this->expectExceptionMessage('alert must be a string or an instance of "Kerox\Fcm\Model\Message\Notification\ApnsNotification\Alert".');
1414

1515
(new ApnsNotification())->setAlert(true);
1616
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kerox\Fcm\Test\TestCase\Model\Message;
6+
7+
use Kerox\Fcm\Model\Message\Webpush;
8+
use Kerox\Fcm\Test\TestCase\AbstractTestCase;
9+
use PHPUnit\Framework\Error\Warning;
10+
11+
class WebpushTest extends AbstractTestCase
12+
{
13+
public function testDeprecationWarning(): void
14+
{
15+
$this->expectException(Warning::class);
16+
$this->expectExceptionMessage('Using array to set options is deprecated since version 2.1 and will be remove in version 3.0, use class "Kerox\Fcm\Model\Message\Options\WebpushOptions" instead.');
17+
18+
(new Webpush())
19+
->setOptions([
20+
'analytics_label' => 'webpush',
21+
'link' => 'https://example.com',
22+
]);
23+
}
24+
}

tests/TestCase/Model/MessageTest.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ class MessageTest extends AbstractTestCase
2525
{
2626
public function testMessage(): void
2727
{
28-
$expectedJson = file_get_contents(__DIR__ . '/../../Mocks/Model/message.json');
29-
3028
$message = (new Message((new Notification('Breaking News'))->setBody('New news story available.')))
31-
->setData(['story_id' => 'story_12345'])
29+
->setName('fcm')
30+
->setData([
31+
'story_id' => 'story_12345'
32+
])
3233
->setAndroid(
3334
(new Android())
3435
->setCollapseKey('collapse_key')
@@ -165,6 +166,7 @@ public function testMessage(): void
165166
->setSound(
166167
(new Sound())
167168
->isCritical()
169+
->setName(Sound::DEFAULT_NAME)
168170
->setVolume(0.5)
169171
)
170172
->setContentAvailable(true)
@@ -185,8 +187,30 @@ public function testMessage(): void
185187
->setOptions(
186188
(new Options())
187189
->setAnalyticsLabel('fcm')
188-
);
190+
)
191+
;
192+
193+
$this->assertJsonStringEqualsJsonFile(__DIR__ . '/../../Mocks/Model/message.json', json_encode($message));
194+
}
195+
196+
public function testMessageWithTopic(): void
197+
{
198+
$message = (new Message('Breaking News'))
199+
->setName('fcm')
200+
->setData([
201+
'story_id' => 'story_12345'
202+
])
203+
->setTopic('TopicA')
204+
;
205+
206+
$this->assertJsonStringEqualsJsonFile(__DIR__ . '/../../Mocks/Model/message_with_topic.json', json_encode($message));
207+
}
208+
209+
public function testInvalidMessage(): void
210+
{
211+
$this->expectException(\InvalidArgumentException::class);
212+
$this->expectExceptionMessage('$message must be a string or an instance of "Kerox\Fcm\Model\Message\Notification".');
189213

190-
$this->assertJsonStringEqualsJsonString($expectedJson, json_encode($message));
214+
(new Message(1234));
191215
}
192216
}

0 commit comments

Comments
 (0)