-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
429 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
bootstrap="vendor/autoload.php" | ||
backupGlobals="false" | ||
colors="true" | ||
processIsolation="false" | ||
stopOnError="true" | ||
stopOnFailure="true" | ||
cacheDirectory=".phpunit.cache" | ||
cacheResult="true" | ||
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd" | ||
> | ||
<source> | ||
<include> | ||
<directory suffix=".php">src/</directory> | ||
</include> | ||
</source> | ||
<testsuites> | ||
<testsuite name="Netgsm Test Suite"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
namespace Macellan\Netgsm\Tests; | ||
|
||
use Exception; | ||
use Macellan\Netgsm\Exceptions\NetgsmException; | ||
use Macellan\Netgsm\Netgsm; | ||
use Macellan\Netgsm\NetgsmChannel; | ||
use Macellan\Netgsm\Tests\Notifications\TestNotifiable; | ||
use Macellan\Netgsm\Tests\Notifications\TestSmsNotification; | ||
use Mockery; | ||
|
||
class NetgsmChannelTest extends TestCase | ||
{ | ||
private array $config; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->config = $this->app['config']->get('services.sms.netgsm'); | ||
} | ||
|
||
public function test_send_notification(): void | ||
{ | ||
$this->config['enable'] = true; | ||
$this->config['debug'] = true; | ||
$this->config['sandbox_mode'] = false; | ||
|
||
$netgsm = Mockery::mock(Netgsm::class, [$this->config])->makePartial(); | ||
$netgsm | ||
->shouldReceive('sendSms') | ||
->andReturn([]); | ||
|
||
$channel = new NetgsmChannel($netgsm); | ||
|
||
$channel->send(new TestNotifiable(), new TestSmsNotification()); | ||
|
||
$netgsm->shouldHaveReceived('sendSms'); | ||
} | ||
|
||
public function test_can_not_send_notification_with_disable(): void | ||
{ | ||
$this->config['enable'] = false; | ||
|
||
$netgsm = Mockery::mock(Netgsm::class, [$this->config])->makePartial(); | ||
|
||
$channel = new NetgsmChannel($netgsm); | ||
|
||
$notification = Mockery::mock(TestSmsNotification::class); | ||
|
||
$channel->send(new TestNotifiable(), $notification); | ||
|
||
$notification->shouldNotHaveReceived('toNetgsm'); | ||
} | ||
|
||
public function test_can_not_send_notification_invalid_message(): void | ||
{ | ||
$this->config['enable'] = true; | ||
$this->config['sandbox_mode'] = false; | ||
|
||
$this->expectException(NetgsmException::class); | ||
|
||
$netgsm = Mockery::mock(Netgsm::class, [$this->config])->makePartial(); | ||
|
||
$channel = new NetgsmChannel($netgsm); | ||
|
||
$notification = Mockery::mock(TestSmsNotification::class)->makePartial(); | ||
$notification | ||
->shouldReceive('toNetgsm') | ||
->andReturn(null); | ||
|
||
$channel->send(new TestNotifiable(), $notification); | ||
$netgsm->shouldNotHaveReceived('sendSms'); | ||
} | ||
|
||
public function test_can_not_send_notification_with_sandbox_mode(): void | ||
{ | ||
$this->config['enable'] = true; | ||
$this->config['sandbox_mode'] = true; | ||
|
||
$netgsm = Mockery::mock(Netgsm::class, [$this->config])->makePartial(); | ||
|
||
$channel = new NetgsmChannel($netgsm); | ||
|
||
$channel->send(new TestNotifiable(), new TestSmsNotification()); | ||
|
||
$netgsm->shouldNotHaveReceived('sendSms'); | ||
} | ||
|
||
public function test_can_not_send_notification_throw_exception(): void | ||
{ | ||
$this->config['enable'] = true; | ||
$this->config['debug'] = true; | ||
$this->config['sandbox_mode'] = false; | ||
|
||
$this->expectException(Exception::class); | ||
|
||
$netgsm = Mockery::mock(Netgsm::class, [$this->config])->makePartial(); | ||
$netgsm | ||
->shouldReceive('sendSms') | ||
->andThrow(new Exception()); | ||
|
||
$channel = new NetgsmChannel($netgsm); | ||
|
||
$channel->send(new TestNotifiable(), new TestSmsNotification()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Macellan\Netgsm\Tests; | ||
|
||
use Illuminate\Support\Facades\Http; | ||
use Macellan\Netgsm\Api\BaseApi; | ||
use Macellan\Netgsm\DTO\Sms\SmsMessage; | ||
use Macellan\Netgsm\Netgsm; | ||
|
||
class NetgsmTest extends TestCase | ||
{ | ||
private Netgsm $netgsm; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->netgsm = new Netgsm($this->app['config']->get('services.sms.netgsm')); | ||
} | ||
|
||
public function test_send_sms(): void | ||
{ | ||
Http::fake([ | ||
BaseApi::BASE_URL.'/*' => Http::response('') | ||
]); | ||
|
||
$smsMessage = (new SmsMessage('Test message')) | ||
->setNumbers(['123456']); | ||
|
||
$this->assertIsArray($this->netgsm->sendSms($smsMessage)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Macellan\Netgsm\Tests\Notifications; | ||
|
||
use Illuminate\Notifications\Notifiable; | ||
|
||
class TestNotifiable | ||
{ | ||
use Notifiable; | ||
|
||
public function routeNotificationForSms(): string | ||
{ | ||
return '+905554443322'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Macellan\Netgsm\Tests\Notifications; | ||
|
||
use Illuminate\Notifications\Notification; | ||
use Macellan\Netgsm\DTO\Sms\BaseSmsMessage; | ||
use Macellan\Netgsm\DTO\Sms\OtpSmsMessage; | ||
use Macellan\Netgsm\DTO\Sms\SmsMessage; | ||
|
||
class TestSmsNotification extends Notification | ||
{ | ||
public function __construct(private bool $isOtpSms = false) | ||
{ | ||
} | ||
|
||
public function toNetgsm() | ||
{ | ||
if ($this->isOtpSms) { | ||
return new OtpSmsMessage('Test otp message'); | ||
} | ||
|
||
return new SmsMessage('Test sms message'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace Macellan\Netgsm\Tests; | ||
|
||
use Illuminate\Support\Facades\Http; | ||
use Macellan\Netgsm\Api\BaseApi; | ||
use Macellan\Netgsm\Api\Sms\OtpSms; | ||
use Macellan\Netgsm\Api\Sms\Sms; | ||
use Macellan\Netgsm\DTO\Sms\OtpSmsMessage; | ||
use Macellan\Netgsm\Exceptions\NetgsmException; | ||
use Spatie\ArrayToXml\ArrayToXml; | ||
|
||
class OtpSmsApiTest extends TestCase | ||
{ | ||
private OtpSms $otpSmsApi; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->otpSmsApi = new OtpSms($this->app['config']->get('services.sms.netgsm')); | ||
} | ||
|
||
private function initOtpSmsMessage(): OtpSmsMessage | ||
{ | ||
return (new OtpSmsMessage('Test message')) | ||
->setNumbers(['123456']); | ||
} | ||
|
||
private function mockSendOtpSmsResponse(string $code, ?string $jobId = null, ?string $error = null): void | ||
{ | ||
$responseArr = [ | ||
'main' => [ | ||
'code' => $code, | ||
'jobID' => $jobId, | ||
'error' => $error, | ||
], | ||
]; | ||
|
||
Http::fake([ | ||
BaseApi::BASE_URL.'/sms/send/otp' => Http::response( | ||
ArrayToXml::convert($responseArr, 'xml', true, 'UTF-8') | ||
) | ||
]); | ||
} | ||
|
||
public function test_send_otp_sms_success(): void | ||
{ | ||
$otpSmsMessage = $this->initOtpSmsMessage(); | ||
$this->mockSendOtpSmsResponse('0', '123'); | ||
|
||
$data = $this->otpSmsApi->send($otpSmsMessage); | ||
|
||
$this->assertEquals(['code' => '0', 'id' => '123', 'error' => null], $data); | ||
} | ||
|
||
public function test_send_otp_sms_response_error_code(): void | ||
{ | ||
$this->expectException(NetgsmException::class); | ||
|
||
$otpSmsMessage = $this->initOtpSmsMessage(); | ||
$this->mockSendOtpSmsResponse('20', null, 'Error'); | ||
|
||
$data = $this->otpSmsApi->send($otpSmsMessage); | ||
|
||
$this->assertEquals(['code' => '20', 'id' => null, 'error' => 'Error'], $data); | ||
} | ||
|
||
public function test_send_otp_sms_xml_parse_exception(): void | ||
{ | ||
$this->expectException(NetgsmException::class); | ||
|
||
$otpSmsMessage = $this->initOtpSmsMessage(); | ||
Http::fake([ | ||
'*/sms/send/otp' => Http::response('Wrong data') | ||
]); | ||
|
||
$this->otpSmsApi->send($otpSmsMessage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace Macellan\Netgsm\Tests; | ||
|
||
use DateTime; | ||
use Illuminate\Support\Facades\Http; | ||
use Macellan\Netgsm\Api\BaseApi; | ||
use Macellan\Netgsm\Api\Sms\Sms; | ||
use Macellan\Netgsm\DTO\Sms\SmsMessage; | ||
use Macellan\Netgsm\Enums\SmsSendType; | ||
use Macellan\Netgsm\Exceptions\HttpClientException; | ||
use Macellan\Netgsm\Exceptions\NetgsmException; | ||
|
||
class SmsApiTest extends TestCase | ||
{ | ||
private Sms $smsApi; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->smsApi = new Sms($this->app['config']->get('services.sms.netgsm')); | ||
} | ||
|
||
private function initSmsMessage(): SmsMessage | ||
{ | ||
return (new SmsMessage('Test message')) | ||
->setNumbers(['123456']); | ||
} | ||
|
||
private function mockSendSmsResponse(string $code, ?string $id = null): void | ||
{ | ||
Http::fake([ | ||
BaseApi::BASE_URL.'/sms/send/xml' => Http::response($code.' '.$id) | ||
]); | ||
} | ||
|
||
public function test_send_sms_success(): void | ||
{ | ||
$smsMessage = $this->initSmsMessage(); | ||
$this->mockSendSmsResponse('00', '123'); | ||
|
||
$data = $this->smsApi->send($smsMessage); | ||
|
||
$this->assertEquals(['code' => '00', 'id' => '123'], $data); | ||
} | ||
|
||
public function test_send_sms_with_dates_and_header(): void | ||
{ | ||
$smsMessage = ($this->initSmsMessage()) | ||
->setStartDate(new DateTime()) | ||
->setStopDate(new DateTime()) | ||
->setHeader('Test Header'); | ||
|
||
$this->mockSendSmsResponse('00', '123'); | ||
|
||
$data = $this->smsApi->send($smsMessage); | ||
|
||
$this->assertEquals(['code' => '00', 'id' => '123'], $data); | ||
} | ||
|
||
public function test_send_sms_many_to_many(): void | ||
{ | ||
$smsMessage = ($this->initSmsMessage()) | ||
->setType(SmsSendType::MANY_TO_MANY) | ||
->setManyToData([ | ||
['message' => 'Test message', 'number' => '123456'], | ||
['message' => 'Test message 2', 'number' => '1234567'], | ||
]); | ||
|
||
$this->mockSendSmsResponse('00', '123'); | ||
|
||
$data = $this->smsApi->send($smsMessage); | ||
|
||
$this->assertEquals(['code' => '00', 'id' => '123'], $data); | ||
} | ||
|
||
public function test_send_sms_response_error_code(): void | ||
{ | ||
$this->expectException(NetgsmException::class); | ||
|
||
$smsMessage = $this->initSmsMessage(); | ||
$this->mockSendSmsResponse('20'); | ||
|
||
$data = $this->smsApi->send($smsMessage); | ||
|
||
$this->assertEquals(['code' => '20', 'id' => null], $data); | ||
} | ||
|
||
public function test_send_sms_http_client_exception(): void | ||
{ | ||
$this->expectException(HttpClientException::class); | ||
|
||
$smsMessage = $this->initSmsMessage(); | ||
Http::fake([ | ||
'*/sms/send/xml' => Http::response('', 500) | ||
]); | ||
|
||
$this->smsApi->send($smsMessage); | ||
} | ||
} |
Oops, something went wrong.