Skip to content

Commit

Permalink
support AfricasTalkingChannel::class (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedNourJamalElDin authored Dec 12, 2022
1 parent 4a9569c commit b66043e
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 12 deletions.
30 changes: 23 additions & 7 deletions src/AfricasTalkingChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,7 @@ public function send($notifiable, Notification $notification)
{
$message = $notification->toAfricasTalking($notifiable);

if (!$phoneNumber = $notifiable->routeNotificationFor('africasTalking')) {
$phoneNumber = $notifiable->phone_number;
}

if(!empty($message->getTo())) {
$phoneNumber = $message->getTo();
}
$phoneNumber = $this->getTo($notifiable, $notification, $message);

if(empty($phoneNumber)) {
throw InvalidPhonenumber::configurationNotSet();
Expand All @@ -61,4 +55,26 @@ public function send($notifiable, Notification $notification)
throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage());
}
}


private function getTo($notifiable, Notification $notification, AfricasTalkingMessage $message)
{
if(!empty($message->getTo())) {
return $message->getTo();
}

if ($notifiable->routeNotificationFor(static::class, $notification)) {
return $notifiable->routeNotificationFor(static::class, $notification);
}

if ($notifiable->routeNotificationFor('africasTalking', $notification)) {
return $notifiable->routeNotificationFor('africasTalking', $notification);
}

if (isset($notifiable->phone_number)) {
return $notifiable->phone_number;
}

throw CouldNotSendNotification::invalidReceiver();
}
}
7 changes: 7 additions & 0 deletions src/Exceptions/CouldNotSendNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ public static function serviceRespondedWithAnError(string $error): self
{
return new static("AfricasTalking service responded with an error: {$error}");
}


public static function invalidReceiver(): self
{
return new static("The notifiable did not have a receiving phone number. Add a routeNotificationForAfricasTalking
method or a phone_number attribute to your notifiable.");
}
}
106 changes: 101 additions & 5 deletions tests/AfricasTalkingChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace NotificationChannels\AfricasTalking\Test;

use AfricasTalking\SDK\AfricasTalking as AfricasTalkingSDK;
use AfricasTalking\SDK\SMS;
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Notifications\Notification;
use Mockery;
use NotificationChannels\AfricasTalking\AfricasTalkingChannel;
Expand All @@ -14,13 +16,14 @@ class AfricasTalkingChannelTest extends TestCase
/** @var Mockery\Mock */
protected $africasTalking;

/** @var \NotificationChannels\Twitter\AfricasTalkingChannel */
/** @var \NotificationChannels\AfricasTalking\AfricasTalkingChannel */
protected $channel;

public function setUp(): void
{
parent::setUp();
$this->africasTalking = Mockery::mock(AfricasTalkingSDK::class);
$this->sms = Mockery::mock(SMS::class);
$this->channel = new AfricasTalkingChannel($this->africasTalking);
}

Expand All @@ -32,17 +35,77 @@ public function it_can_be_instantiated()
}

/** @test */
public function it_can_send_sms_notification()
public function it_can_send_sms_notification_to_notifiable_with_method()
{
$this->africasTalking->shouldReceive('send')
$this->africasTalking->expects('sms')
->once()
->andReturn($this->sms);

$this->sms->expects('send')
->once()
->andReturn(200);

$this->channel->send(new NotifiableWithMethod, new TestNotification);
}

/** @test */
public function it_can_send_sms_notification_to_anonymous_notifiable_using_class_name()
{
$this->africasTalking->expects('sms')
->once()
->andReturn($this->sms);

$this->sms->expects('send')
->once()
->andReturn(200);

$this->channel->send((new AnonymousNotifiable())->route(AfricasTalkingChannel::class, "+1111111111"), new TestNotification);
}

/** @test */
public function it_can_send_sms_notification_to_anonymous_notifiable_using_string_name()
{
$this->africasTalking->expects('sms')
->once()
->andReturn($this->sms);

$this->sms->expects('send')
->once()
->andReturn(200);

$this->channel->send((new AnonymousNotifiable())->route('africasTalking', "+1111111111"), new TestNotification);
}

/** @test */
public function it_can_send_sms_notification_to_notifiable_with_attribute()
{
$this->africasTalking->expects('sms')
->once()
->andReturn($this->sms);

$this->sms->expects('send')
->once()
->andReturn(200);

$this->channel->send(new NotifiableWithAttribute(), new TestNotification);
}

/** @test */
public function it_can_send_sms_notification_to_message_get_to()
{
$this->africasTalking->expects('sms')
->once()
->andReturn($this->sms);

$this->sms->expects('send')
->once()
->andReturn(200);

$this->channel->send(new TestNotifiable, new TestNotification);
$this->channel->send(new AnonymousNotifiable(), new TestNotificationWithGetTo);
}
}

class TestNotifiable
class NotifiableWithMethod
{
use \Illuminate\Notifications\Notifiable;

Expand All @@ -67,3 +130,36 @@ public function toAfricasTalking($notifiable)
return new AfricasTalkingMessage();
}
}

class TestNotificationWithGetTo extends Notification
{
/**
* @param $notifiable
* @return AfricasTalkingMessage
* @throws CouldNotSendNotification
*/
public function toAfricasTalking($notifiable)
{
return (new AfricasTalkingMessage())
->to('+22222222222');
}
}


class Notifiable
{
public $phone_number = null;

public function routeNotificationFor()
{
}
}

class NotifiableWithAttribute
{
public $phone_number = '+22222222222';

public function routeNotificationFor()
{
}
}

0 comments on commit b66043e

Please sign in to comment.