Skip to content
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
7 changes: 2 additions & 5 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
php: [8.1, 8.2]
laravel: ['9.*', '10.*', '11.*']
php: [8.2, 8.3, 8.4]
laravel: ['11.*', '12.*']
dependency-version: [prefer-lowest, prefer-stable]
exclude:
- laravel: 11.*
php: 8.1

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}

Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
}
],
"require": {
"php": ">=8.1",
"php": ">=8.2",
"aws/aws-sdk-php": "^3.69.11",
"guzzlehttp/guzzle": "^6.2.1 || ^7.0",
"illuminate/notifications": "^9.0|^10.0 || ^11.0",
"illuminate/support": "^9.0|^10.0 || ^11.0"
"illuminate/notifications": "^11.0||^12.0",
"illuminate/support": "^11.0||^12.0"
},
"require-dev": {
"mockery/mockery": "^1.5.1",
"phpunit/phpunit": "^9.5.10 || ^10.5"
"laravel/pint": "^1.20",
"mockery/mockery": "^1.6",
"phpunit/phpunit": "^11.0"
},
"autoload": {
"psr-4": {
Expand Down
30 changes: 8 additions & 22 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
<phpunit backupGlobals="false"
beStrictAboutTestsThatDoNotTestAnything="true"
bootstrap="vendor/autoload.php"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
stopOnError="false"
stopOnFailure="false"
>
<testsuites>
<testsuite name="AwsSns Test Suite">
<directory>tests</directory>
<testsuite name="Feature">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>
21 changes: 9 additions & 12 deletions src/Sns.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,25 @@

namespace NotificationChannels\AwsSns;

use Aws\Exception\AwsException;
use Aws\Sns\SnsClient as SnsService;
use Aws\Result;
use Aws\Sns\SnsClient;

class Sns
{
/**
* @var SnsService
* Create a new instance of the class.
*/
protected $snsService;

public function __construct(SnsService $snsService)
public function __construct(protected SnsClient $sns)
{
$this->snsService = $snsService;
//
}

/**
* @param string $destination Phone number as described by the E.164 format.
* @return \Aws\Result
* Send the message to the given E.164 destination phone number.
*
* @throws AwsException
* @throws Aws\Exception\AwsException
*/
public function send(SnsMessage $message, $destination)
public function send(SnsMessage $message, string $destination): Result
{
$attributes = [
'AWS.SNS.SMS.SMSType' => [
Expand Down Expand Up @@ -56,6 +53,6 @@ public function send(SnsMessage $message, $destination)
'MessageAttributes' => $attributes,
];

return $this->snsService->publish($parameters);
return $this->sns->publish($parameters);
}
}
30 changes: 10 additions & 20 deletions src/SnsChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,39 @@

namespace NotificationChannels\AwsSns;

use Aws\Result;
use Exception;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Notifications\Events\NotificationFailed;
use Illuminate\Notifications\Notification;
use NotificationChannels\AwsSns\Exceptions\CouldNotSendNotification;

class SnsChannel
{
/**
* @var Sns
*/
protected $sns;

/**
* @var Dispatcher
*/
protected $events;

public function __construct(Sns $sns, Dispatcher $events)
public function __construct(protected Sns $sns, protected Dispatcher $events)
{
$this->sns = $sns;
$this->events = $events;
//
}

/**
* Send the given notification.
*
* @return \Aws\Result
*/
public function send($notifiable, Notification $notification)
public function send($notifiable, Notification $notification): ?Result
{
try {
$destination = $this->getDestination($notifiable, $notification);
$message = $this->getMessage($notifiable, $notification);

return $this->sns->send($message, $destination);
} catch (\Exception $e) {
$event = new NotificationFailed(
} catch (Exception $e) {
$this->events->dispatch(new NotificationFailed(
$notifiable,
$notification,
'sns',
['message' => $e->getMessage(), 'exception' => $e]
);
$this->events->dispatch($event);
));

return null;
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/SnsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ class SnsServiceProvider extends ServiceProvider
*/
public function boot()
{
$this->app->when(SnsChannel::class)
->needs(Sns::class)
->give(function () {
return new Sns($this->app->make(SnsService::class));
});
$this->app->bind(Sns::class, function () {
return new Sns($this->app->make(SnsService::class));
});

$this->app->bind(SnsService::class, function () {
$config = array_merge(['version' => 'latest'], $this->app['config']['services.sns']);
Expand Down
36 changes: 15 additions & 21 deletions tests/SnsChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@
use NotificationChannels\AwsSns\Sns;
use NotificationChannels\AwsSns\SnsChannel;
use NotificationChannels\AwsSns\SnsMessage;
use PHPUnit\Framework\TestCase;

class SnsChannelTest extends TestCase
{
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;

/**
* @var Mockery\LegacyMockInterface|Mockery\MockInterface|Sns
*/
Expand All @@ -40,25 +37,24 @@ protected function setUp(): void
$this->channel = new SnsChannel($this->sns, $this->dispatcher);
}

/** @test */
public function it_will_not_send_a_message_without_known_receiver()
public function test_it_will_not_send_a_message_without_known_receiver()
{
$notifiable = new Notifiable();
$notifiable = new Notifiable;
$notification = Mockery::mock(Notification::class);

$this->dispatcher->shouldReceive('dispatch')
->atLeast()->once()
->atLeast()
->once()
->with(Mockery::type(NotificationFailed::class));

$result = $this->channel->send($notifiable, $notification);

$this->assertNull($result);
}

/** @test */
public function it_will_send_a_sms_message_to_the_result_of_the_route_method_of_the_notifiable()
public function test_it_will_send_a_sms_message_to_the_result_of_the_route_method_of_the_notifiable()
{
$notifiable = new NotifiableWithMethod();
$notifiable = new NotifiableWithMethod;
$message = new SnsMessage('Message text');

$notification = Mockery::mock(Notification::class);
Expand All @@ -71,10 +67,9 @@ public function it_will_send_a_sms_message_to_the_result_of_the_route_method_of_
$this->channel->send($notifiable, $notification);
}

/** @test */
public function it_will_make_a_call_to_the_phone_number_attribute_of_the_notifiable()
public function test_it_will_make_a_call_to_the_phone_number_attribute_of_the_notifiable()
{
$notifiable = new NotifiableWithAttribute();
$notifiable = new NotifiableWithAttribute;
$message = new SnsMessage('Some content to send');

$notification = Mockery::mock(Notification::class);
Expand All @@ -87,10 +82,9 @@ public function it_will_make_a_call_to_the_phone_number_attribute_of_the_notifia
$this->channel->send($notifiable, $notification);
}

/** @test */
public function it_will_convert_a_string_to_a_sms_message()
public function test_it_will_convert_a_string_to_a_sms_message()
{
$notifiable = new NotifiableWithAttribute();
$notifiable = new NotifiableWithAttribute;

$notification = Mockery::mock(Notification::class);
$notification->shouldReceive('toSns')->andReturn('Message text');
Expand All @@ -102,10 +96,9 @@ public function it_will_convert_a_string_to_a_sms_message()
$this->channel->send($notifiable, $notification);
}

/** @test */
public function it_will_dispatch_an_event_in_case_of_an_invalid_message()
public function test_it_will_dispatch_an_event_in_case_of_an_invalid_message()
{
$notifiable = new NotifiableWithAttribute();
$notifiable = new NotifiableWithAttribute;

$notification = Mockery::mock(Notification::class);
$notification->shouldReceive('toSns')->andReturn(-1);
Expand All @@ -117,8 +110,7 @@ public function it_will_dispatch_an_event_in_case_of_an_invalid_message()
$this->channel->send($notifiable, $notification);
}

/** @test */
public function it_will_send_a_sms_to_an_anonymous_notifiable()
public function test_it_will_send_a_sms_to_an_anonymous_notifiable()
{
$notification = Mockery::mock(Notification::class);
$notification->shouldReceive('toSns')->andReturn('Message text');
Expand All @@ -140,6 +132,7 @@ class Notifiable

public function routeNotificationFor()
{
//
}
}

Expand All @@ -157,5 +150,6 @@ class NotifiableWithAttribute

public function routeNotificationFor()
{
//
}
}
Loading
Loading