Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/faraz default sender #2

Merged
merged 6 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

This is a laravel sms package with multi driver support. Supports laravel **v8.0+** and requires php **v7.4+**

> Star! if you used and liked this package.
> Star! :star: if you used and liked this package.

## Supported SMS Providers

- [Kavenegar](https://kavenegar.com)
- [SMS.ir](https://sms.ir)
- [FarazSMS](https://farazsms.com)

## Installation & Configuration

Install using composer
Expand Down
1 change: 1 addition & 0 deletions config/sms.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
'faraz_sms' => [
'driver_class' => \Omalizadeh\Sms\Drivers\FarazSms\FarazSms::class,
'api_key' => '',
'default_sender' => env('FARAZ_SMS_DEFAULT_SENDER', '+983000505'),
],
];
10 changes: 6 additions & 4 deletions src/Drivers/FarazSms/FarazSms.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@ public function getBulkSmsUrl(): string

protected function mergeSmsOptions(array $data, array $options): array
{
if (!isset($options['sender'])) {
throw new InvalidParameterException('sender parameter is required.');
$sender = $options['sender'] ?? $this->getConfig('default_sender');

if (empty($sender)) {
throw new InvalidParameterException('sender parameter is required for Faraz sms driver.');
}

return array_merge($data, [
'sender' => $options['sender'],
'sender' => $sender,
'time' => $options['time'] ?? now()->addSecond(),
]);
}
Expand All @@ -121,7 +123,7 @@ protected function mergeTemplateOptions(array $data, array $options): array
protected function callApi(string $url, array $data)
{
if (empty($apiKey = $this->getConfig('api_key'))) {
throw new InvalidConfigurationException('invalid api_key sms provider config');
throw new InvalidConfigurationException('invalid api_key faraz sms provider config');
}

$response = Http::asJson()->acceptJson()->withHeaders([
Expand Down
23 changes: 15 additions & 8 deletions tests/FarazSmsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Omalizadeh\Sms\Tests;


use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;
use Omalizadeh\Sms\Drivers\FarazSms\FarazSms;
use Omalizadeh\Sms\Exceptions\SendingSmsFailedException;
Expand All @@ -20,7 +21,7 @@ protected function setUp(): void
$this->farazSms = new FarazSms(config('sms.faraz_sms'));
}

public function test_sms_can_be_sent_successfully_by_faraz_driver()
public function test_sms_can_be_sent_successfully_by_faraz_driver(): void
{
Http::fake([
'https://api2.ippanel.com/api/v1/sms/send/webservice/single' => Http::response([
Expand All @@ -30,7 +31,7 @@ public function test_sms_can_be_sent_successfully_by_faraz_driver()
'message_id' => '123456789'
],
'error_message' => null
], 200)
])
]);

$result = $this->farazSms->send('09123456789', 'Test message', ['sender' => '1000']);
Expand All @@ -46,14 +47,14 @@ public function test_sms_can_be_sent_successfully_by_faraz_driver()
});
}

public function test_sms_can_get_failed_successfully()
public function test_sms_can_get_failed_successfully(): void
{
Http::fake([
'https://api2.ippanel.com/api/v1/sms/send/webservice/single' => Http::response([
'status' => 'error',
'code' => 5,
'message' => 'اعتبار کافی نیست.'
], 200)
])
]);

$this->expectException(SendingSmsFailedException::class);
Expand All @@ -62,23 +63,29 @@ public function test_sms_can_get_failed_successfully()
$this->farazSms->send('09123456789', 'Test message', ['sender' => '1000']);
}

public function test_invalid_response_structure_exception()
public function test_invalid_response_structure_exception(): void
{
Http::fake([
'https://api2.ippanel.com/api/v1/sms/send/webservice/single' => Http::response([
'unexpected' => 'response'
], 200)
])
]);

$this->expectException(SendingSmsFailedException::class);

$this->farazSms->send('09123456789', 'Test message', ['sender' => '1000']);
}

public function test_missing_sender_option_leads_to_exception()
public function test_missing_sender_option_leads_to_exception(): void
{
config([
'sms.faraz_sms.default_sender' => '',
]);

$this->farazSms = new FarazSms(config('sms.faraz_sms'));

$this->expectException(\Omalizadeh\Sms\Exceptions\InvalidParameterException::class);
$this->expectExceptionMessage('sender parameter is required.');
$this->expectExceptionMessage('sender parameter is required for Faraz sms driver.');

$this->farazSms->send('09123456789', 'Test message');
}
Expand Down
Loading