update v1.0.7.9 R.C.

This is a Release Candidate. We are still testing.
This commit is contained in:
Sujit Prasad
2016-08-03 20:04:36 +05:30
parent 8b6b924d09
commit ffa56a43cb
3830 changed files with 181529 additions and 495353 deletions

View File

@@ -0,0 +1,462 @@
<?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,71 @@
<?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('post')->once()->andReturn($response);
$this->app->singleton('fcm.client', function($app) use($client) {
return $client;
});
$tokens = 'uniqueToken';
$fcm = new FCMSender();
$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('post')->times(10)->andReturn($response);
$this->app->singleton('fcm.client', function($app) use($client) {
return $client;
});
$tokens = [];
for ($i=0 ; $i<10000 ; $i++) {
$tokens[$i] = 'token_'.$i;
}
$fcm = new FCMSender();
$fcm->sendTo($tokens);
}
}

View File

@@ -0,0 +1,19 @@
<?php
abstract class FCMTestCase extends Illuminate\Foundation\Testing\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,75 @@
<?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,129 @@
<?php
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",
"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->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);
}
}

View File

@@ -0,0 +1,65 @@
<?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,160 @@
<?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('post')->once()->andReturn($response);
$this->app->singleton('fcm.client', function($app) use($client) {
return $client;
});
$fcm = new FCMSender();
$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('post')->once()->andReturn($response);
$this->app->singleton('fcm.client', function($app) use($client) {
return $client;
});
$fcm = new FCMSender();
$topics = new Topics();
$topics->topic('test');
$response = $fcm->sendToTopic($topics);
$this->assertFalse($response->isSuccess());
$this->assertTrue($response->shouldRetry());
$this->assertEquals('TopicsMessageRateExceeded', $response->error());
}
}