From 616ffe4fa095d4f29c4574e428748e9fd423d2d6 Mon Sep 17 00:00:00 2001 From: phrshteh Date: Wed, 3 Jul 2024 00:24:04 +0330 Subject: [PATCH] feat: add tests for sending sms by faraz drier --- phpunit.xml | 22 +++-------- tests/FarazSmsTest.php | 85 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 16 deletions(-) create mode 100644 tests/FarazSmsTest.php diff --git a/phpunit.xml b/phpunit.xml index e995239..9e0c5bf 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,18 +1,8 @@ - - - - ./tests - - + + + + ./tests + + diff --git a/tests/FarazSmsTest.php b/tests/FarazSmsTest.php new file mode 100644 index 0000000..058e897 --- /dev/null +++ b/tests/FarazSmsTest.php @@ -0,0 +1,85 @@ + 'test_api_key']); + + $this->farazSms = new FarazSms(config('sms.faraz_sms')); + } + + public function test_sms_can_be_sent_successfully_by_faraz_driver() + { + Http::fake([ + 'https://api2.ippanel.com/api/v1/sms/send/webservice/single' => Http::response([ + 'status' => 'OK', + 'code' => 200, + 'data' => [ + 'message_id' => '123456789' + ], + 'error_message' => null + ], 200) + ]); + + $result = $this->farazSms->send('09123456789', 'Test message', ['sender' => '1000']); + + $this->assertEquals('123456789', $result->getMessageId()); + + Http::assertSent(function ($request) { + return $request->url() == 'https://api2.ippanel.com/api/v1/sms/send/webservice/single' && + $request['recipient'] == ['09123456789'] && + $request['message'] == 'Test message' && + $request['sender'] == '1000' && + $request->header('apiKey')[0] == 'test_api_key'; + }); + } + + public function test_sms_can_get_failed_successfully() + { + Http::fake([ + 'https://api2.ippanel.com/api/v1/sms/send/webservice/single' => Http::response([ + 'status' => 'error', + 'code' => 5, + 'message' => 'اعتبار کافی نیست.' + ], 200) + ]); + + $this->expectException(SendingSmsFailedException::class); + $this->expectExceptionMessage('اعتبار کافی نیست.'); + + $this->farazSms->send('09123456789', 'Test message', ['sender' => '1000']); + } + + public function test_invalid_response_structure_exception() + { + 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() + { + $this->expectException(\Omalizadeh\Sms\Exceptions\InvalidParameterException::class); + $this->expectExceptionMessage('sender parameter is required.'); + + $this->farazSms->send('09123456789', 'Test message'); + } +}