seeder-migration-issues

This commit is contained in:
RafficMohammed
2023-01-30 14:23:34 +05:30
parent 4d918c722f
commit 2ec836b447
3628 changed files with 116006 additions and 187 deletions

View File

@@ -0,0 +1,460 @@
<?php
use GuzzleHttp\Psr7\Response;
use LaravelFCM\Response\DownstreamResponse;
class DownstreamResponseTest extends FCMTestCase
{
/**
* @test
*/
public function it_construct_a_response_with_a_success()
{
$token = 'new_token';
$response = new Response(200, [], '{
"multicast_id": 108,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{ "message_id": "1:08" }
]
}');
$downstreamResponse = new DownstreamResponse($response, $token);
$this->assertEquals(1, $downstreamResponse->numberSuccess());
$this->assertEquals(0, $downstreamResponse->numberFailure());
$this->assertEquals(0, $downstreamResponse->numberModification());
$this->assertCount(0, $downstreamResponse->tokensToDelete());
$this->assertCount(0, $downstreamResponse->tokensToModify());
}
/**
* @test
*/
public function it_construct_a_response_with_multiple_successes()
{
$tokens = [
'first_token',
'second_token',
'third_token',
];
$response = new Response(200, [], '{
"multicast_id": 108,
"success": 3,
"failure": 0,
"canonical_ids": 0,
"results": [
{ "message_id": "1:01" },
{ "message_id": "1:02" },
{ "message_id": "1:03" }
]
}');
$downstreamResponse = new DownstreamResponse($response, $tokens);
$this->assertEquals(3, $downstreamResponse->numberSuccess());
$this->assertEquals(0, $downstreamResponse->numberFailure());
$this->assertEquals(0, $downstreamResponse->numberModification());
$this->assertCount(0, $downstreamResponse->tokensToDelete());
$this->assertCount(0, $downstreamResponse->tokensToModify());
}
/**
* @test
*/
public function it_construct_a_response_with_a_failure()
{
$token = 'new_token';
$response = new Response(200, [], '{
"multicast_id": 108,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{ "error": "NotRegistered" }
]
}');
$downstreamResponse = new DownstreamResponse($response, $token);
$this->assertEquals(0, $downstreamResponse->numberSuccess());
$this->assertEquals(1, $downstreamResponse->numberFailure());
$this->assertEquals(0, $downstreamResponse->numberModification());
$this->assertFalse($downstreamResponse->hasMissingToken());
$this->assertCount(1, $downstreamResponse->tokensToDelete());
$this->assertEquals($token, $downstreamResponse->tokensToDelete()[ 0 ]);
$this->assertCount(0, $downstreamResponse->tokensToModify());
}
/**
* @test
*/
public function it_construct_a_response_with_multiple_failures()
{
$tokens = [
'first_token',
'second_token',
'third_token',
'fourth_token',
];
$response = new Response(200, [], '{
"multicast_id": 108,
"success": 0,
"failure": 3,
"canonical_ids": 0,
"results": [
{ "error": "NotRegistered" },
{ "error": "InvalidRegistration" },
{ "error": "NotRegistered" },
{ "error": "MissingRegistration"}
]
}');
$downstreamResponse = new DownstreamResponse($response, $tokens);
$this->assertEquals(0, $downstreamResponse->numberSuccess());
$this->assertEquals(3, $downstreamResponse->numberFailure());
$this->assertEquals(0, $downstreamResponse->numberModification());
$this->assertTrue($downstreamResponse->hasMissingToken());
$this->assertCount(3, $downstreamResponse->tokensToDelete());
$this->assertEquals($tokens[ 0 ], $downstreamResponse->tokensToDelete()[ 0 ]);
$this->assertEquals($tokens[ 1 ], $downstreamResponse->tokensToDelete()[ 1 ]);
$this->assertEquals($tokens[ 2 ], $downstreamResponse->tokensToDelete()[ 2 ]);
$this->assertCount(0, $downstreamResponse->tokensToModify());
}
/**
* @test
*/
public function it_construct_a_response_with_a_token_to_change()
{
$token = 'new_token';
$response = new Response(200, [], '{
"multicast_id": 108,
"success": 0,
"failure": 0,
"canonical_ids": 1,
"results": [
{ "message_id": "1:2342", "registration_id": "32" }
]
}');
$downstreamResponse = new DownstreamResponse($response, $token);
$this->assertEquals(0, $downstreamResponse->numberSuccess());
$this->assertEquals(0, $downstreamResponse->numberFailure());
$this->assertEquals(1, $downstreamResponse->numberModification());
$this->assertCount(0, $downstreamResponse->tokensToDelete());
$this->assertCount(1, $downstreamResponse->tokensToModify());
$this->assertTrue(array_key_exists($token, $downstreamResponse->tokensToModify()));
$this->assertEquals('32', $downstreamResponse->tokensToModify()[ $token ]);
}
/**
* @test
*/
public function it_construct_a_response_with_multiple_tokens_to_change()
{
$tokens = [
'first_token',
'second_token',
'third_token',
];
$response = new Response(200, [], '{
"multicast_id": 108,
"success": 0,
"failure": 0,
"canonical_ids": 3,
"results": [
{ "message_id": "1:2342", "registration_id": "32" },
{ "message_id": "1:2342", "registration_id": "33" },
{ "message_id": "1:2342", "registration_id": "34" }
]
}');
$downstreamResponse = new DownstreamResponse($response, $tokens);
$this->assertEquals(0, $downstreamResponse->numberSuccess());
$this->assertEquals(0, $downstreamResponse->numberFailure());
$this->assertEquals(3, $downstreamResponse->numberModification());
$this->assertCount(0, $downstreamResponse->tokensToDelete());
$this->assertCount(3, $downstreamResponse->tokensToModify());
$this->assertTrue(array_key_exists($tokens[ 0 ], $downstreamResponse->tokensToModify()));
$this->assertEquals('32', $downstreamResponse->tokensToModify()[ $tokens[ 0 ] ]);
$this->assertTrue(array_key_exists($tokens[ 1 ], $downstreamResponse->tokensToModify()));
$this->assertEquals('33', $downstreamResponse->tokensToModify()[ $tokens[ 1 ] ]);
$this->assertTrue(array_key_exists($tokens[ 2 ], $downstreamResponse->tokensToModify()));
$this->assertEquals('34', $downstreamResponse->tokensToModify()[ $tokens[ 2 ] ]);
}
/**
* @test
*/
public function it_construct_a_response_with_a_token_unavailable()
{
$token = 'first_token';
$response = new Response(200, [], '{
"multicast_id": 216,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{ "error": "Unavailable" }
]
}');
$downstreamResponse = new DownstreamResponse($response, $token);
$this->assertEquals(0, $downstreamResponse->numberSuccess());
$this->assertEquals(1, $downstreamResponse->numberFailure());
$this->assertEquals(0, $downstreamResponse->numberModification());
// Unavailable is not an error caused by the token validity. it don't need to be deleted$
$this->assertCount(0, $downstreamResponse->tokensToModify());
$this->assertCount(0, $downstreamResponse->tokensToDelete());
$this->assertCount(1, $downstreamResponse->tokensToRetry());
$this->assertEquals($token, $downstreamResponse->tokensToRetry()[0]);
}
/**
* @test
*/
public function it_construct_a_response_with_a_token_server_error()
{
$token = 'first_token';
$response = new Response(200, [], '{
"multicast_id": 216,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{ "error": "InternalServerError" }
]
}');
$downstreamResponse = new DownstreamResponse($response, $token);
$this->assertEquals(0, $downstreamResponse->numberSuccess());
$this->assertEquals(1, $downstreamResponse->numberFailure());
$this->assertEquals(0, $downstreamResponse->numberModification());
// Unavailable is not an error caused by the token validity. it don't need to be deleted$
$this->assertCount(0, $downstreamResponse->tokensToModify());
$this->assertCount(0, $downstreamResponse->tokensToDelete());
$this->assertCount(1, $downstreamResponse->tokensToRetry());
$this->assertEquals($token, $downstreamResponse->tokensToRetry()[0]);
}
/**
* @test
*/
public function it_construct_a_response_with_a_token_exceeded()
{
$token = 'first_token';
$response = new Response(200, [], '{
"multicast_id": 216,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{ "error": "DeviceMessageRateExceeded" }
]
}');
$downstreamResponse = new DownstreamResponse($response, $token);
$this->assertEquals(0, $downstreamResponse->numberSuccess());
$this->assertEquals(1, $downstreamResponse->numberFailure());
$this->assertEquals(0, $downstreamResponse->numberModification());
// Unavailable is not an error caused by the token validity. it don't need to be deleted$
$this->assertCount(0, $downstreamResponse->tokensToModify());
$this->assertCount(0, $downstreamResponse->tokensToDelete());
$this->assertCount(1, $downstreamResponse->tokensToRetry());
$this->assertEquals($token, $downstreamResponse->tokensToRetry()[0]);
}
/**
* @test
*/
public function it_construct_a_response_with_a_mixed_token_to_retry()
{
$tokens = [
'first_token',
'second_token',
'third_token',
'fourth_token',
'fifth_token',
'sixth_token',
];
$response = new Response(200, [], '{
"multicast_id": 216,
"success": 0,
"failure": 6,
"canonical_ids": 0,
"results": [
{ "error": "DeviceMessageRateExceeded" },
{ "error": "InternalServerError" },
{ "error": "Unavailable" },
{ "error": "DeviceMessageRateExceeded" },
{ "error": "InternalServerError" },
{ "error": "Unavailable" }
]
}');
$downstreamResponse = new DownstreamResponse($response, $tokens);
$this->assertEquals(0, $downstreamResponse->numberSuccess());
$this->assertEquals(6, $downstreamResponse->numberFailure());
$this->assertEquals(0, $downstreamResponse->numberModification());
// Unavailable is not an error caused by the token validity. it don't need to be deleted$
$this->assertCount(0, $downstreamResponse->tokensToModify());
$this->assertCount(0, $downstreamResponse->tokensToDelete());
$this->assertCount(6, $downstreamResponse->tokensToRetry());
$this->assertEquals($tokens[ 0 ], $downstreamResponse->tokensToRetry()[ 0 ]);
$this->assertEquals($tokens[ 1 ], $downstreamResponse->tokensToRetry()[ 1 ]);
$this->assertEquals($tokens[ 2 ], $downstreamResponse->tokensToRetry()[ 2 ]);
$this->assertEquals($tokens[ 3 ], $downstreamResponse->tokensToRetry()[ 3 ]);
$this->assertEquals($tokens[ 4 ], $downstreamResponse->tokensToRetry()[ 4 ]);
$this->assertEquals($tokens[ 5 ], $downstreamResponse->tokensToRetry()[ 5 ]);
}
/**
* @test
*/
public function it_construct_a_response_with_mixed_response()
{
$tokens = [
'first_token',
'second_token',
'third_token',
'fourth_token',
'fifth_token',
'sixth_token',
];
$response = new Response(200, [], '{
"multicast_id": 216,
"success": 3,
"failure": 3,
"canonical_ids": 1,
"results": [
{ "message_id": "1:0408" },
{ "error": "Unavailable" },
{ "error": "InvalidRegistration" },
{ "message_id": "1:1516" },
{ "message_id": "1:2342", "registration_id": "32" },
{ "error": "NotRegistered"}
]
}');
$downstreamResponse = new DownstreamResponse($response, $tokens);
$this->assertEquals(3, $downstreamResponse->numberSuccess());
$this->assertEquals(3, $downstreamResponse->numberFailure());
$this->assertEquals(1, $downstreamResponse->numberModification());
// Unavailable is not an error caused by the token validity. it don't need to be deleted
$this->assertCount(2, $downstreamResponse->tokensToDelete());
$this->assertCount(1, $downstreamResponse->tokensToModify());
$this->assertEquals($tokens[ 2 ], $downstreamResponse->tokensToDelete()[ 0 ]);
$this->assertEquals($tokens[ 5 ], $downstreamResponse->tokensToDelete()[ 1 ]);
$this->assertTrue(array_key_exists($tokens[ 4 ], $downstreamResponse->tokensToModify()));
$this->assertEquals('32', $downstreamResponse->tokensToModify()[ $tokens[ 4 ] ]);
}
/**
* @test
*/
public function it_construct_a_response_with_multiples_response()
{
$tokens = [
'first_token',
'second_token',
'third_token',
'fourth_token',
'fifth_token',
'sixth_token',
'seventh_token',
];
$tokens1 = [
'first_1_token',
'second_1_token',
'third_1_token',
'fourth_1_token',
'fifth_1_token',
'sixth_1_token',
'seventh_1_token',
];
$response = new Response(200, [], '{
"multicast_id": 216,
"success": 3,
"failure": 3,
"canonical_ids": 1,
"results": [
{ "message_id": "1:0408" },
{ "error": "Unavailable" },
{ "error": "InvalidRegistration" },
{ "message_id": "1:1516" },
{ "message_id": "1:2342", "registration_id": "32" },
{ "error": "NotRegistered"},
{ "error": "MessageTooBig"}
]
}');
$downstreamResponse = new DownstreamResponse($response, $tokens);
$downstreamResponse1 = new DownstreamResponse($response, $tokens1);
$downstreamResponse->merge($downstreamResponse1);
$this->assertEquals(6, $downstreamResponse->numberSuccess());
$this->assertEquals(6, $downstreamResponse->numberFailure());
$this->assertEquals(2, $downstreamResponse->numberModification());
// Unavailable is not an error caused by the token validity. it don't need to be deleted
$this->assertCount(4, $downstreamResponse->tokensToDelete());
$this->assertCount(2, $downstreamResponse->tokensToModify());
$this->assertCount(2, $downstreamResponse->tokensWithError());
$this->assertEquals($tokens[ 2 ], $downstreamResponse->tokensToDelete()[ 0 ]);
$this->assertEquals($tokens1[ 2 ], $downstreamResponse->tokensToDelete()[ 2 ]);
$this->assertEquals($tokens[ 5 ], $downstreamResponse->tokensToDelete()[ 1 ]);
$this->assertEquals($tokens1[ 5 ], $downstreamResponse->tokensToDelete()[ 3 ]);
$this->assertCount(2, $downstreamResponse->tokensToRetry());
$this->assertEquals('MessageTooBig', $downstreamResponse->tokensWithError()[$tokens[6]]);
$this->assertEquals('MessageTooBig', $downstreamResponse->tokensWithError()[$tokens1[6]]);
}
}

View File

@@ -0,0 +1,92 @@
<?php
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use LaravelFCM\Sender\FCMSender;
class ResponseTest extends FCMTestCase
{
/**
* @test
*/
public function it_send_a_notification_to_a_device()
{
$response = new Response(200, [], '{
"multicast_id": 216,
"success": 3,
"failure": 3,
"canonical_ids": 1,
"results": [
{ "message_id": "1:0408" }
]
}');
$client = Mockery::mock(Client::class);
$client->shouldReceive('request')->once()->andReturn($response);
$tokens = 'uniqueToken';
$fcm = new FCMSender($client, 'http://test.test');
$fcm->sendTo($tokens);
}
/**
* @test
*/
public function it_send_a_notification_to_more_than_1000_devices()
{
$response = new Response(200, [], '{
"multicast_id": 216,
"success": 3,
"failure": 3,
"canonical_ids": 1,
"results": [
{ "message_id": "1:0408" },
{ "error": "Unavailable" },
{ "error": "InvalidRegistration" },
{ "message_id": "1:1516" },
{ "message_id": "1:2342", "registration_id": "32" },
{ "error": "NotRegistered"}
]
}');
$client = Mockery::mock(Client::class);
$client->shouldReceive('request')->times(10)->andReturn($response);
$tokens = [];
for ($i = 0; $i < 10000; ++$i) {
$tokens[$i] = 'token_'.$i;
}
$fcm = new FCMSender($client, 'http://test.test');
$fcm->sendTo($tokens);
}
/**
* @test
*/
public function an_empty_array_of_tokens_thrown_an_exception()
{
$response = new Response(400, [], '{
"multicast_id": 216,
"success": 3,
"failure": 3,
"canonical_ids": 1,
"results": [
{ "message_id": "1:0408" },
{ "error": "Unavailable" },
{ "error": "InvalidRegistration" },
{ "message_id": "1:1516" },
{ "message_id": "1:2342", "registration_id": "32" },
{ "error": "NotRegistered"}
]
}');
$client = Mockery::mock(Client::class);
$client->shouldReceive('request')->once()->andReturn($response);
$fcm = new FCMSender($client, 'http://test.test');
$this->setExpectedException(\LaravelFCM\Response\Exceptions\InvalidRequestException::class);
$fcm->sendTo([]);
}
}

View File

@@ -0,0 +1,22 @@
<?php
use Illuminate\Foundation\Testing\TestCase;
abstract class FCMTestCase extends TestCase
{
public function createApplication()
{
$app = require __DIR__.'/../vendor/laravel/laravel/bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
$app->register(LaravelFCM\FCMServiceProvider::class);
$app['config']['fcm.driver'] = 'http';
$app['config']['fcm.http.timeout'] = 20;
$app['config']['fcm.http.server_send_url'] = 'http://test.test';
$app['config']['fcm.http.server_key'] = 'key=myKey';
$app['config']['fcm.http.sender_id'] = 'SENDER_ID';
return $app;
}
}

View File

@@ -0,0 +1,72 @@
<?php
use LaravelFCM\Response\GroupResponse;
class GroupResponseTest extends FCMTestCase
{
/**
* @test
*/
public function it_construct_a_response_with_successes()
{
$notificationKey = 'notificationKey';
$response = new \GuzzleHttp\Psr7\Response(200, [], '{
"success": 2,
"failure": 0
}');
$responseGroup = new GroupResponse($response, $notificationKey);
$this->assertEquals(2, $responseGroup->numberSuccess());
$this->assertEquals(0, $responseGroup->numberFailure());
$this->assertCount(0, $responseGroup->tokensFailed());
}
/**
* @test
*/
public function it_construct_a_response_with_failures()
{
$notificationKey = 'notificationKey';
$response = new \GuzzleHttp\Psr7\Response(200, [], '{
"success": 0,
"failure": 2,
"failed_registration_ids":[
"regId1",
"regId2"
]}');
$responseGroup = new GroupResponse($response, $notificationKey);
$this->assertEquals(0, $responseGroup->numberSuccess());
$this->assertEquals(2, $responseGroup->numberFailure());
$this->assertCount(2, $responseGroup->tokensFailed());
$this->assertEquals('regId1', $responseGroup->tokensFailed()[ 0]);
$this->assertEquals('regId2', $responseGroup->tokensFailed()[ 1]);
}
/**
* @test
*/
public function it_construct_a_response_with_partials_failures()
{
$notificationKey = 'notificationKey';
$response = new \GuzzleHttp\Psr7\Response(200, [], '{
"success": 1,
"failure": 2,
"failed_registration_ids":[
"regId1",
"regId2"
]}');
$responseGroup = new GroupResponse($response, $notificationKey);
$this->assertEquals(1, $responseGroup->numberSuccess());
$this->assertEquals(2, $responseGroup->numberFailure());
$this->assertCount(2, $responseGroup->tokensFailed());
}
}

View File

@@ -0,0 +1,143 @@
<?php
use LaravelFCM\Message\Exceptions\InvalidOptionsException;
use LaravelFCM\Message\OptionsBuilder;
use LaravelFCM\Message\OptionsPriorities;
use LaravelFCM\Message\PayloadDataBuilder;
use LaravelFCM\Message\PayloadNotificationBuilder;
class PayloadTest extends FCMTestCase
{
/**
* @test
*/
public function it_construct_a_valid_json_with_option()
{
$targetPartial = '{
"collapse_key":"collapseKey",
"content_available":true
}';
$targetFull = '{
"collapse_key":"collapseKey",
"content_available":true,
"priority":"high",
"delay_while_idle":true,
"time_to_live":200,
"restricted_package_name":"customPackageName",
"dry_run": true
}';
$optionBuilder = new OptionsBuilder();
$optionBuilder->setCollapseKey('collapseKey');
$optionBuilder->setContentAvailable(true);
$json = json_encode($optionBuilder->build()->toArray());
$this->assertJsonStringEqualsJsonString($targetPartial, $json);
$optionBuilder->setPriority(OptionsPriorities::high)
->setDelayWhileIdle(true)
->setDryRun(true)
->setRestrictedPackageName('customPackageName')
->setTimeToLive(200);
$json = json_encode($optionBuilder->build()->toArray());
$this->assertJsonStringEqualsJsonString($targetFull, $json);
}
/**
* @test
*/
public function it_construct_a_valid_json_with_data()
{
$targetAdd = '{
"first_data":"first",
"second_data":true
}';
$targetSet = '
{
"third_data":"third",
"fourth_data":4
}';
$dataBuilder = new PayloadDataBuilder();
$dataBuilder->addData(['first_data' => 'first'])
->addData(['second_data' => true]);
$json = json_encode($dataBuilder->build()->toArray());
$this->assertJsonStringEqualsJsonString($targetAdd, $json);
$dataBuilder->setData(['third_data' => 'third', 'fourth_data' => 4]);
$json = json_encode($dataBuilder->build()->toArray());
$this->assertJsonStringEqualsJsonString($targetSet, $json);
}
/**
* @test
*/
public function it_construct_a_valid_json_with_notification()
{
$targetPartial = '{
"title":"test_title",
"body":"test_body",
"badge":"test_badge",
"sound":"test_sound"
}';
$targetFull = '{
"title":"test_title",
"body":"test_body",
"android_channel_id":"test_channel_id",
"badge":"test_badge",
"sound":"test_sound",
"tag":"test_tag",
"color":"test_color",
"click_action":"test_click_action",
"body_loc_key":"test_body_key",
"body_loc_args":"[ body0, body1 ]",
"title_loc_key":"test_title_key",
"title_loc_args":"[ title0, title1 ]",
"icon":"test_icon"
}';
$notificationBuilder = new PayloadNotificationBuilder();
$notificationBuilder->setTitle('test_title')
->setBody('test_body')
->setSound('test_sound')
->setBadge('test_badge');
$json = json_encode($notificationBuilder->build()->toArray());
$this->assertJsonStringEqualsJsonString($targetPartial, $json);
$notificationBuilder
->setChannelId('test_channel_id')
->setTag('test_tag')
->setColor('test_color')
->setClickAction('test_click_action')
->setBodyLocationKey('test_body_key')
->setBodyLocationArgs('[ body0, body1 ]')
->setTitleLocationKey('test_title_key')
->setTitleLocationArgs('[ title0, title1 ]')
->setIcon('test_icon');
$json = json_encode($notificationBuilder->build()->toArray());
$this->assertJsonStringEqualsJsonString($targetFull, $json);
}
/**
* @test
*/
public function it_throws_an_invalidoptionsexception_if_the_interval_is_too_big()
{
$this->setExpectedException(InvalidOptionsException::class);
$optionBuilder = new OptionsBuilder();
$optionBuilder->setTimeToLive(2419200 * 10);
}
}

View File

@@ -0,0 +1,64 @@
<?php
use GuzzleHttp\Psr7\Response;
use LaravelFCM\Response\TopicResponse;
class TopicsResponseTest extends FCMTestCase
{
/**
* @test
*/
public function it_construct_a_topic_response_with_success()
{
$topic = new \LaravelFCM\Message\Topics();
$topic->topic('topicName');
$response = new Response(200, [], '{
"message_id": "1234"
}');
$topicResponse = new TopicResponse($response, $topic);
$this->assertTrue($topicResponse->isSuccess());
$this->assertFalse($topicResponse->shouldRetry());
$this->assertNull($topicResponse->error());
}
/**
* @test
*/
public function it_construct_a_topic_response_with_error()
{
$topic = new \LaravelFCM\Message\Topics();
$topic->topic('topicName');
$response = new Response(200, [], '{
"error": "MessageTooBig"
}');
$topicResponse = new TopicResponse($response, $topic);
$this->assertFalse($topicResponse->isSuccess());
$this->assertFalse($topicResponse->shouldRetry());
$this->assertEquals('MessageTooBig', $topicResponse->error());
}
/**
* @test
*/
public function it_construct_a_topic_response_with_error_and_it_should_retry()
{
$topic = new \LaravelFCM\Message\Topics();
$topic->topic('topicName');
$response = new Response(200, [], '{
"error": "TopicsMessageRateExceeded"
}');
$topicResponse = new TopicResponse($response, $topic);
$this->assertFalse($topicResponse->isSuccess());
$this->assertTrue($topicResponse->shouldRetry());
$this->assertEquals('TopicsMessageRateExceeded', $topicResponse->error());
}
}

View File

@@ -0,0 +1,149 @@
<?php
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use LaravelFCM\Message\Topics;
use LaravelFCM\Sender\FCMSender;
use LaravelFCM\Message\Exceptions\NoTopicProvidedException;
class TopicsTest extends FCMTestCase
{
/**
* @test
*/
public function it_throw_an_exception_if_no_topic_is_provided()
{
$topics = new Topics();
$this->setExpectedException(NoTopicProvidedException::class);
$topics->build();
}
/**
* @test
*/
public function it_has_only_one_topic()
{
$target = '/topics/myTopic';
$topics = new Topics();
$topics->topic('myTopic');
$this->assertEquals($target, $topics->build());
}
/**
* @test
*/
public function it_has_two_topics_and()
{
$target = [
'condition' => "'firstTopic' in topics && 'secondTopic' in topics",
];
$topics = new Topics();
$topics->topic('firstTopic')->andTopic('secondTopic');
$this->assertEquals($target, $topics->build());
}
/**
* @test
*/
public function it_has_two_topics_or()
{
$target = [
'condition' => "'firstTopic' in topics || 'secondTopic' in topics",
];
$topics = new Topics();
$topics->topic('firstTopic')->orTopic('secondTopic');
$this->assertEquals($target, $topics->build());
}
/**
* @test
*/
public function it_has_two_topics_or_and_one_and()
{
$target = [
'condition' => "'firstTopic' in topics || 'secondTopic' in topics && 'thirdTopic' in topics",
];
$topics = new Topics();
$topics->topic('firstTopic')->orTopic('secondTopic')->andTopic('thirdTopic');
$this->assertEquals($target, $topics->build());
}
/**
* @test
*/
public function it_has_a_complex_topic_condition()
{
$target = [
'condition' => "'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics) || ('TopicD' in topics && 'TopicE' in topics)",
];
$topics = new Topics();
$topics->topic('TopicA')
->andTopic(function ($condition) {
$condition->topic('TopicB')->orTopic('TopicC');
})
->orTopic(function ($condition) {
$condition->topic('TopicD')->andTopic('TopicE');
});
$this->assertEquals($target, $topics->build());
}
/**
* @test
*/
public function it_send_a_notification_to_a_topic()
{
$response = new Response(200, [], '{"message_id":6177433633397011933}');
$client = Mockery::mock(Client::class);
$client->shouldReceive('request')->once()->andReturn($response);
$fcm = new FCMSender($client, 'http://test.test');
$topics = new Topics();
$topics->topic('test');
$response = $fcm->sendToTopic($topics);
$this->assertTrue($response->isSuccess());
$this->assertFalse($response->shouldRetry());
$this->assertNull($response->error());
}
/**
* @test
*/
public function it_send_a_notification_to_a_topic_and_return_error()
{
$response = new Response(200, [], '{"error":"TopicsMessageRateExceeded"}');
$client = Mockery::mock(Client::class);
$client->shouldReceive('request')->once()->andReturn($response);
$fcm = new FCMSender($client, 'http://test.test');
$topics = new Topics();
$topics->topic('test');
$response = $fcm->sendToTopic($topics);
$this->assertFalse($response->isSuccess());
$this->assertTrue($response->shouldRetry());
$this->assertEquals('TopicsMessageRateExceeded', $response->error());
}
}

View File

@@ -0,0 +1,232 @@
<?php
namespace LaravelFCM\Mocks;
use LaravelFCM\Response\DownstreamResponse;
use LaravelFCM\Response\DownstreamResponseContract;
/**
* Class MockDownstreamResponse **Only use it for testing**.
*/
class MockDownstreamResponse implements DownstreamResponseContract
{
/**
* @internal
*
* @var int
*/
protected $numberTokensSuccess = 0;
/**
* @internal
*
* @var
*/
protected $messageId;
/**
* @internal
*
* @var array
*/
protected $tokensToDelete = [];
/**
* @internal
*
* @var array
*/
protected $tokensToModify = [];
/**
* @internal
*
* @var array
*/
protected $tokensToRetry = [];
/**
* @internal
*
* @var array
*/
protected $tokensWithError = [];
/**
* @internal
*
* @var bool
*/
protected $hasMissingToken = false;
/**
* DownstreamResponse constructor.
*
* @param $numberSuccess
*/
public function __construct($numberSuccess)
{
$this->numberTokensSuccess = $numberSuccess;
}
/**
* Not using it.
*
* @param DownstreamResponse $response
*
* @throws \Exception
*/
public function merge(DownstreamResponse $response)
{
throw new \Exception('You cannot use this method for mocking response');
}
/**
* Get the number of device reached with success + numberTokenToModify.
*
* @return int
*/
public function numberSuccess()
{
return $this->numberTokensSuccess + count($this->tokensToModify);
}
/**
* Get the number of device which thrown an error.
*
* @return int
*/
public function numberFailure()
{
return count($this->tokensToDelete()) + count($this->tokensWithError);
}
/**
* Get the number of device that you need to modify their token.
*
* @return int
*/
public function numberModification()
{
return count($this->tokensToModify());
}
/**
* Add a token to delete.
*
* @param $token
* @return MockDownstreamResponse
*/
public function addTokenToDelete($token)
{
$this->tokensToDelete[] = $token;
return $this;
}
/**
* get token to delete
* remove all tokens returned by this method in your database.
*
* @return array
*/
public function tokensToDelete()
{
return $this->tokensToDelete;
}
/**
* Add a token to modify.
*
* @param $oldToken
* @param $newToken
* @return MockDownstreamResponse
*/
public function addTokenToModify($oldToken, $newToken)
{
$this->tokensToModify[$oldToken] = $newToken;
return $this;
}
/**
* get token to modify
* key: oldToken
* value: new token
* find the old token in your database and replace it with the new one.
*
* @return array
*/
public function tokensToModify()
{
return $this->tokensToModify;
}
/**
* Add a token to retry.
*
* @param $token
* @return MockDownstreamResponse
*/
public function addTokenToRetry($token)
{
$this->tokensToRetry[] = $token;
return $this;
}
/**
* Get tokens that you should resend using exponential backoof.
*
* @return array
*/
public function tokensToRetry()
{
return $this->tokensToRetry;
}
/**
* Add a token to errors.
*
* @param $token
* @param $message
* @return MockDownstreamResponse
*/
public function addTokenWithError($token, $message)
{
$this->tokensWithError[$token] = $message;
return $this;
}
/**
* Get tokens that thrown an error
* key : token
* value : error
* In production, remove these tokens from you database.
*
* @return array
*/
public function tokensWithError()
{
return $this->tokensWithError;
}
/**
* change missing token state.
*
* @param $hasMissingToken
* @return MockDownstreamResponse
*/
public function setMissingToken($hasMissingToken)
{
$this->hasMissingToken = $hasMissingToken;
return $this;
}
/**
* check if missing tokens was given to the request
* If true, remove all the empty token in your database.
*
* @return bool
*/
public function hasMissingToken()
{
return $this->hasMissingToken;
}
}

View File

@@ -0,0 +1,123 @@
<?php
namespace LaravelFCM\Mocks;
use LaravelFCM\Response\GroupResponseContract;
/**
* Class MockGroupResponse **Only use it for testing**.
*/
class MockGroupResponse implements GroupResponseContract
{
/**
* @internal
*
* @var int
*/
protected $numberTokensSuccess = 0;
/**
* @internal
*
* @var int
*/
protected $numberTokensFailure = 0;
/**
* @internal
*
* @var array
*/
protected $tokensFailed = [];
/**
* @internal
*
* @var string
*/
protected $to;
/**
* set number of success.
*
* @param int $numberSuccess
* @return MockGroupResponse
*/
public function setNumberSuccess($numberSuccess)
{
$this->numberTokensSuccess = $numberSuccess;
return $this;
}
/**
* Get the number of device reached with success.
*
* @return int
*/
public function numberSuccess()
{
return $this->numberTokensSuccess;
}
/**
* set number of failures.
*
* @param $numberFailures
* @return MockGroupResponse
*/
public function setNumberFailure($numberFailures)
{
$this->numberTokensSuccess = $numberFailures;
return $this;
}
/**
* Get the number of device which thrown an error.
*
* @return int
*/
public function numberFailure()
{
return $this->numberTokensFailure;
}
/**
* add a token to the failed list.
*
* @param $tokenFailed
* @return MockGroupResponse
*/
public function addTokenFailed($tokenFailed)
{
$this->tokensFailed[] = $tokenFailed;
return $this;
}
/**
* Get all token in group that fcm cannot reach.
*
* @return array
*/
public function tokensFailed()
{
return $this->tokensFailed;
}
/**
* @return string
*/
public function getTo()
{
return $this->to;
}
/**
* @param string $to
* @return MockGroupResponse
*/
public function setTo($to)
{
$this->to = $to;
return $this;
}
}

View File

@@ -0,0 +1,94 @@
<?php
namespace LaravelFCM\Mocks;
use LaravelFCM\Response\TopicResponseContract;
/**
* Class MockTopicResponse **Only use it for testing**.
*/
class MockTopicResponse implements TopicResponseContract
{
/**
* @internal
*
* @var string
*/
protected $topic;
/**
* @internal
*
* @var string
*/
protected $messageId;
/**
* @internal
*
* @var string
*/
protected $error;
/**
* @internal
*
* @var bool
*/
protected $needRetry = false;
/**
* if success set a message id.
*
* @param $messageId
* @return MockTopicResponse
*/
public function setSuccess($messageId)
{
$this->messageId = $messageId;
return $this;
}
/**
* true if topic sent with success.
*
* @return bool
*/
public function isSuccess()
{
return (bool) $this->messageId;
}
/**
* set error.
*
* @param $error
* @return MockTopicResponse
*/
public function setError($error)
{
$this->error = $error;
return $this;
}
/**
* return error message
* you should test if it's necessary to resent it.
*
* @return string error
*/
public function error()
{
return $this->error;
}
/**
* return true if it's necessary resent it using exponential backoff.
*
* @return bool
*/
public function shouldRetry()
{
return (bool) $this->error;
}
}