Skip to content
This repository was archived by the owner on Sep 15, 2024. It is now read-only.

Commit c740e09

Browse files
committed
Added the ability to queue the notification.
1 parent 8049c53 commit c740e09

File tree

5 files changed

+58
-41
lines changed

5 files changed

+58
-41
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,23 @@ This package allows you to send notifications with audit data instead of saving
1313

1414
If you have some microservices and want to have centralized auditing, this could be helpful.
1515

16+
## Requirements
17+
18+
- PHP >= 7.1
19+
- Laravel >= 5.2 && < 5.8
20+
1621
## Installation
1722

1823
### Step 1
1924

25+
> This package depends on [owen-it/laravel-auditing:^8.0](https://github.com/owen-it/laravel-auditing) and [lab123it/aws-sns:dev-master](https://github.com/lab123it/aws-sns), so you have to install and configure them first in order to make this work.
26+
2027
You can install the package via composer:
2128

2229
```bash
2330
composer require andreshg112/laravel-auditing-notifications
2431
```
2532

26-
> This package depends on [owen-it/laravel-auditing:^8.0](https://github.com/owen-it/laravel-auditing) and [lab123it/aws-sns:dev-master](https://github.com/lab123it/aws-sns), so you have to install and configure them first in order to make this work.
27-
2833
### Step 2
2934

3035
Change the audit default driver:
@@ -61,6 +66,10 @@ return [
6166
// Required if you're going to use different notifications channels for sending audit data.
6267
'notifications' => [
6368
Andreshg112\LaravelAuditingNotifications\AuditSns::class,
69+
70+
// Or this if you want to queue the delivery of the message.
71+
// https://laravel.com/docs/5.2/queues
72+
// Andreshg112\LaravelAuditingNotifications\AuditSnsQueue::class,
6473
],
6574

6675
// Required if you're going to use the default Andreshg112\LaravelAuditingNotifications\AuditSns Notification.

src/AuditSns.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Andreshg112\LaravelAuditingNotifications;
44

5-
use Illuminate\Bus\Queueable;
65
use Illuminate\Support\Facades\Config;
76
use Lab123\AwsSns\Messages\AwsSnsMessage;
87
use Illuminate\Notifications\Notification;
@@ -11,8 +10,6 @@
1110

1211
class AuditSns extends Notification
1312
{
14-
use Queueable;
15-
1613
/** @var array $auditData */
1714
protected $auditData = null;
1815

@@ -38,7 +35,7 @@ public function via($notifiable)
3835
}
3936

4037
/**
41-
* Get the mail representation of the notification.
38+
* Get the SNS representation of the notification.
4239
*
4340
* @param \OwenIt\Auditing\Contracts\Auditable $notifiable
4441
* @return \Lab123\AwsSns\Messages\AwsSnsMessage

src/AuditSnsQueue.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Andreshg112\LaravelAuditingNotifications;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Contracts\Queue\ShouldQueue;
7+
8+
/**
9+
* Use this notification if you want to queue the delivery of the SNS message.
10+
*/
11+
class AuditSnsQueue extends AuditSns implements ShouldQueue
12+
{
13+
use Queueable;
14+
}

tests/AuditSnsQueueTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Andreshg112\LaravelAuditingNotifications\Tests;
4+
5+
use Orchestra\Testbench\TestCase;
6+
use Illuminate\Support\Facades\Notification;
7+
use Andreshg112\LaravelAuditingNotifications\AuditSnsQueue;
8+
9+
class AuditSnsQueueTest extends TestCase
10+
{
11+
/** @test */
12+
public function it_sends_the_notification()
13+
{
14+
Notification::fake();
15+
16+
$model = new AuditableModel;
17+
18+
$model->setAuditEvent('created');
19+
20+
$model->notify(new AuditSnsQueue($model->toAudit()));
21+
22+
Notification::assertSentTo($model, AuditSnsQueue::class);
23+
}
24+
}

tests/AuditSnsTest.php

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ class AuditSnsTest extends TestCase
1212
/** @test */
1313
public function it_returns_a_sns_message()
1414
{
15-
$this->mockConfig();
15+
/**
16+
* You should not mock the Request facade. Instead, pass the input you desire into the HTTP
17+
* helper methods such as get and post when running your test. Likewise, instead of mocking
18+
* the Config facade, call the Config::set method in your tests.
19+
*
20+
* https://laravel.com/docs/5.6/mocking#mocking-facades
21+
*/
22+
Config::set('audit.notification-driver.sns.topic_arn', 'topic:arn');
1623

1724
$model = new AuditableModel;
1825

@@ -24,38 +31,4 @@ public function it_returns_a_sns_message()
2431

2532
$this->assertInstanceOf(AwsSnsMessage::class, $snsMessage);
2633
}
27-
28-
public function mockConfig()
29-
{
30-
$events = ['created', 'updated', 'deleted', 'restored'];
31-
32-
Config::shouldReceive('get')
33-
->with('audit.events', $events)
34-
->andReturn($events);
35-
36-
Config::shouldReceive('get')
37-
->with('audit.notification-driver.sns.topic_arn')
38-
->once()
39-
->andReturn('topic:arn');
40-
41-
Config::shouldReceive('get')
42-
->with('audit.console', false)
43-
->once()
44-
->andReturn(false);
45-
46-
Config::shouldReceive('get')
47-
->with('audit.strict', false)
48-
->once()
49-
->andReturn(false);
50-
51-
Config::shouldReceive('get')
52-
->with('audit.timestamps', false)
53-
->once()
54-
->andReturn(false);
55-
56-
Config::shouldReceive('get')
57-
->with('audit.user.morph_prefix', 'user')
58-
->once()
59-
->andReturn('user');
60-
}
6134
}

0 commit comments

Comments
 (0)