Skip to content

Commit

Permalink
refactor with test case code
Browse files Browse the repository at this point in the history
  • Loading branch information
shazeedul committed Oct 9, 2024
1 parent 4f2f9bf commit 2c647f9
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 75 deletions.
16 changes: 14 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
{
"cSpell.words": ["miscapi", "smsapi", "smsapimany"]
}
"cSpell.words": [
"bulksms",
"bulksmsbd",
"HASAN",
"intregation",
"laravel",
"miscapi",
"nanopkg",
"SHAZEEDUL",
"smsapi",
"smsapimany",
"SYED"
]
}
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
"name": "IQBAL HASAN",
"email": "iqbalhasan.dev@gmail.com",
"role": "Developer"
},
{
"name": "SYED SHAZEEDUL ISLAM",
"email": "syedshazeedul@gmail.com",
"role": "Developer"
}
],
"require": {
Expand All @@ -36,11 +41,10 @@
"require-dev": {
"laravel/pint": "^1.0",
"nunomaduro/collision": "^6.0",
"nunomaduro/larastan": "^2.0.1",
"orchestra/testbench": "^7.0",
"pestphp/pest": "^1.21",
"pestphp/pest-plugin-laravel": "^1.1",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan": "^1.12",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^9.5",
Expand All @@ -66,8 +70,7 @@
"config": {
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true,
"phpstan/extension-installer": true
"pestphp/pest-plugin": true
}
},
"extra": {
Expand All @@ -82,4 +85,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
6 changes: 4 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ parameters:
level: 4
paths:
- config
- src
- tests
tmpDir: build/phpstan
checkOctaneCompatibility: true
checkModelProperties: true
# checkOctaneCompatibility: true
# checkModelProperties: true
checkMissingIterableValueType: false

21 changes: 13 additions & 8 deletions src/Broadcasting/BulkSmsBdChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Nanopkg\BulkSmsBd\Broadcasting;

use Illuminate\Notifications\Notification;
use Nanopkg\BulkSmsBd\Jobs\BulkSmsBdOneToMany;
use Nanopkg\BulkSmsBd\Jobs\BulkSmsBdOneToOne;
use Nanopkg\BulkSmsBd\Jobs\BulkSmsBdOneToMany;
use Nanopkg\BulkSmsBd\Contracts\BulkSmsBdNotification;

/**
* Class BulkSmsBdChannel
Expand All @@ -26,16 +27,20 @@ class BulkSmsBdChannel
*/
public function send($notifiable, Notification $notification)
{
$notificationArray = $notification->toBulkSmsBd($notifiable);
// check if contacts and message key exists in notification array
if ($notificationArray[config('bulksmsbd.notification.contacts')] && $notificationArray[config('bulksmsbd.notification.message')]) {
if (is_array($notificationArray[config('bulksmsbd.notification.contacts')])) {
BulkSmsBdOneToMany::dispatch($notificationArray[config('bulksmsbd.notification.contacts')], $notificationArray[config('bulksmsbd.notification.message')]);
if ($notification instanceof BulkSmsBdNotification) {
$notificationArray = $notification->toBulkSmsBd($notifiable);
// check if contacts and message key exists in notification array
if (isset($notificationArray[config('bulksmsbd.notification.contacts')]) && isset($notificationArray[config('bulksmsbd.notification.message')])) {
if (is_array($notificationArray[config('bulksmsbd.notification.contacts')])) {
BulkSmsBdOneToMany::dispatch($notificationArray[config('bulksmsbd.notification.contacts')], $notificationArray[config('bulksmsbd.notification.message')]);
} else {
BulkSmsBdOneToOne::dispatch($notificationArray[config('bulksmsbd.notification.contacts')], $notificationArray[config('bulksmsbd.notification.message')]);
}
} else {
BulkSmsBdOneToOne::dispatch($notificationArray[config('bulksmsbd.notification.contacts')], $notificationArray[config('bulksmsbd.notification.message')]);
throw new \Exception(config('bulksmsbd.notification.contacts') . ' or ' . config('bulksmsbd.notification.message') . ' not found in Notification array.');
}
} else {
throw new \Exception(config('bulksmsbd.notification.contacts').' or '.config('bulksmsbd.notification.message').' not found in Notification array.');
throw new \Exception('Notification is missing toBulkSmsBd method.');
}
}
}
54 changes: 6 additions & 48 deletions src/BulkSmsBd.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private function senderID()
*/
private function mode()
{
return config('bulksmsbd.mode');
return config('bulksmsbd.mode', 'log');
}

/**
Expand All @@ -92,7 +92,7 @@ private function client()
private function logSMS(): void
{
if ($this->mode() == 'log') {
$this->logError([
$this->logError(error: [
'contacts' => $this->contacts,
'msg' => $this->msg,
]);
Expand All @@ -109,9 +109,7 @@ private function logSMS(): void
public function oneToOne(string $contacts, string $msg, string $type = 'text')
{
// Check if contacts is valid
if (\preg_match("/^(?:\+88|88)?(01[3-9]\d{8})$/", $contacts)) {
$contacts = $contacts;
} else {
if (!\preg_match("/^(?:\+88|88)?(01[3-9]\d{8})$/", $contacts)) {
throw new \Exception('Number Not Valid', 1012);
}

Expand Down Expand Up @@ -207,7 +205,8 @@ public function getBalance()
public function send()
{
// check if mode is log
if ($this->logSMS()) {
if ($this->mode() == 'log') {
$this->logSMS();
return true;
}
// check if message is set
Expand Down Expand Up @@ -237,7 +236,7 @@ public function send()
}

/**
*Error Code Meaning
*Error Code Meaning
*1002 Sender Id/Masking Not Found
*1003 API Not Found
*1004 SPAM Detected
Expand All @@ -257,110 +256,69 @@ private function validateResponse($response)
switch ((string) $response->response_code) {
case 202:
return $response;
break;

case 1002:
$this->logError('Validation Error', $response);
throw new \Exception('Sender Id/Masking Not Found', 1002);
break;

case 1003:
$this->logError('Validation Error', $response);
throw new \Exception('API Not Found', 1003);
break;

case 1004:
$this->logError('Validation Error', $response);
throw new \Exception('SPAM Detected', 1004);
break;

case 1005:
$this->logError('Validation Error', $response);
throw new \Exception('Internal Error', 1005);
break;

case 1006:
$this->logError('Validation Error', $response);
throw new \Exception('Internal Error', 1006);
break;

case 1007:
$this->logError('Validation Error', $response);
throw new \Exception('Balance Insufficient', 1007);
break;

case 1008:
$this->logError('Validation Error', $response);
throw new \Exception('Message is empty', 1008);
break;

case 1009:
$this->logError('Validation Error', $response);
throw new \Exception('Message Type Not Set (text/unicode)', 1009);
break;

case 1010:
$this->logError('Validation Error', $response);
throw new \Exception('Invalid User & Password', 1010);
break;

case 1011:
$this->logError('Validation Error', $response);
throw new \Exception('Invalid User Id', 1011);
break;

case 1012:
$this->logError('Validation Error', $response);
throw new \Exception('Invalid Number', 1012);
break;

case 1013:
$this->logError('Validation Error', $response);
throw new \Exception('API limit error', 1013);
break;

case 1014:
$this->logError('Validation Error', $response);
throw new \Exception('No matching template', 1014);
break;

case 1015:
$this->logError('Validation Error', $response);
throw new \Exception('Sender Id has not found Any Valid Gateway by api key', 1015);
break;

case 1016:
$this->logError('Validation Error', $response);
throw new \Exception('Sender Type Name Active Price Info not found by this sender id', 1016);
break;

case 1017:
$this->logError('Validation Error', $response);
throw new \Exception('Sender Type Name Price Info not found by this sender id', 1017);
break;

case 1018:
$this->logError('Validation Error', $response);
throw new \Exception('The Owner of this (username) Account is disabled', 1018);
break;

case 1019:
$this->logError('Validation Error', $response);
throw new \Exception('The (sender type name) Price of this (username) Account is disabled', 1019);
break;

case 1020:
$this->logError('Validation Error', $response);
throw new \Exception('The parent of this account is not found.', 1020);
case 1021:
$this->logError('Validation Error', $response);
throw new \Exception('The parent active (sender type name) price of this account is not found.', 1021);
break;

default:
$this->logError('Validation Error', $response);
throw new \Exception('Unknown', -1);
break;
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/Contracts/BulkSmsBdNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Nanopkg\BulkSmsBd\Contracts;

interface BulkSmsBdNotification
{
/**
* Method to prepare the notification data for BulkSmsBd.
*
* @param mixed $notifiable
* @return array
*/
public function toBulkSmsBd($notifiable): array;
}
5 changes: 3 additions & 2 deletions src/Facades/BulkSmsBd.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
/**
* class BulkSmsBd
*
* @method static \Nanopkg\BulkSmsBd\BulkSmsBd OneToOne(string $to, string $message)
* @method static \Nanopkg\BulkSmsBd\BulkSmsBd ManyToMany(array $to, string $message)
* @method static \Nanopkg\BulkSmsBd\BulkSmsBd OneToOne(string $to, string $message, string $type = 'text')
* @method static \Nanopkg\BulkSmsBd\BulkSmsBd OneToMany(array $contacts, string $msg, string $type = 'text')
* @method static \Nanopkg\BulkSmsBd\BulkSmsBd ManyToMany(array $contacts)
* @method static \Nanopkg\BulkSmsBd\BulkSmsBd send()
* @method static \Nanopkg\BulkSmsBd\BulkSmsBd getBalance()
*
Expand Down
4 changes: 2 additions & 2 deletions src/Jobs/BulkSmsBdManyToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public function __construct(array $messages)
*
* @return void
*/
public function handle()
public function handle(): void
{
try {
return BulkSmsBd::ManyToMany($this->messages)->send();
BulkSmsBd::ManyToMany($this->messages)->send();
} catch (\Throwable $th) {
throw $th;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Jobs/BulkSmsBdOneToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public function __construct(array $number, string $message)
*
* @return void
*/
public function handle()
public function handle(): void
{
try {
return BulkSmsBd::oneToMany($this->number, $this->message)->send();
BulkSmsBd::OneToMany($this->number, $this->message)->send();
} catch (\Throwable $th) {
throw $th;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Jobs/BulkSmsBdOneToOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public function __construct(string $number, string $message)
*
* @return void
*/
public function handle()
public function handle(): void
{
try {
return BulkSmsBd::oneToOne($this->number, $this->message)->send();
BulkSmsBd::OneToOne($this->number, $this->message)->send();
} catch (\Throwable $th) {
throw $th;
}
Expand Down
48 changes: 46 additions & 2 deletions tests/ExampleTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,49 @@
<?php

it('can test', function () {
expect(true)->toBeTrue();
// Test Set BulkSmsBd Config Mode log
test('set BulkSmsBd Config Mode log', function () {
config(['bulksmsbd.mode' => 'log']);
$this->assertEquals('log', config('bulksmsbd.mode'));
});

// Check BulkSmsBd Instance Test
test('check BulkSmsBd instance', function () {
$this->assertInstanceOf(\Nanopkg\BulkSmsBd\BulkSmsBd::class, \Nanopkg\BulkSmsBd\Facades\BulkSmsBd::getFacadeRoot());
});

// Check BulkSmsBd OneToOne Test
test('check BulkSmsBd OneToOne', function () {
$this->assertInstanceOf(\Nanopkg\BulkSmsBd\BulkSmsBd::class, \Nanopkg\BulkSmsBd\Facades\BulkSmsBd::OneToOne('8801700000000', 'message'));
});

// Check BulkSmsBd OneToMany Test
test('check BulkSmsBd OneToMany', function () {
$this->assertInstanceOf(\Nanopkg\BulkSmsBd\BulkSmsBd::class, \Nanopkg\BulkSmsBd\Facades\BulkSmsBd::OneToMany(['8801700000000', '8801800000000'], 'message'));
});

// Check BulkSmsBd ManyToMany Test
test('check BulkSmsBd ManyToMany', function () {
$this->assertInstanceOf(\Nanopkg\BulkSmsBd\BulkSmsBd::class, \Nanopkg\BulkSmsBd\Facades\BulkSmsBd::ManyToMany([
['to' => '8801700000000', 'message' => 'message'],
]));
});

// Check BulkSmsBd Send Test
test('check BulkSmsBd Send', function () {
$this->assertTrue(\Nanopkg\BulkSmsBd\Facades\BulkSmsBd::send());
});

// Check BulkSmsBd OneToOne Job Test
test('check BulkSmsBd OneToOne Job', function () {
$this->assertInstanceOf(\Nanopkg\BulkSmsBd\Jobs\BulkSmsBdOneToOne::class, new \Nanopkg\BulkSmsBd\Jobs\BulkSmsBdOneToOne('8801700000000', 'message'));
});

// Check BulkSmsBd OneToMany Job Test
test('check BulkSmsBd OneToMany Job', function () {
$this->assertInstanceOf(\Nanopkg\BulkSmsBd\Jobs\BulkSmsBdOneToMany::class, new \Nanopkg\BulkSmsBd\Jobs\BulkSmsBdOneToMany(['8801700000000', '8801800000000'], 'message'));
});

// Check BulkSmsBd Channel Test
test('check BulkSmsBd Channel', function () {
$this->assertInstanceOf(\Nanopkg\BulkSmsBd\Broadcasting\BulkSmsBdChannel::class, new \Nanopkg\BulkSmsBd\Broadcasting\BulkSmsBdChannel());
});

0 comments on commit 2c647f9

Please sign in to comment.