|
1 | 1 | <?php |
2 | 2 |
|
3 | | -namespace Spatie\FailedJobMonitor\Test; |
4 | | - |
5 | | -use Illuminate\Foundation\Testing\DatabaseMigrations; |
6 | 3 | use Illuminate\Queue\Events\JobFailed; |
7 | 4 | use Illuminate\Support\Facades\Notification as NotificationFacade; |
8 | 5 | use Spatie\FailedJobMonitor\Notifiable; |
|
11 | 8 | use Spatie\FailedJobMonitor\Test\Dummy\AnotherNotification; |
12 | 9 | use Spatie\FailedJobMonitor\Test\Dummy\Job; |
13 | 10 |
|
14 | | -class FailedJobMonitorTest extends TestCase |
15 | | -{ |
16 | | - use DatabaseMigrations; |
17 | | - |
18 | | - /** @var \Spatie\FailedJobMonitor\Test\Dummy\TestQueueManager */ |
19 | | - protected $manager; |
20 | | - |
21 | | - public function setUp(): void |
| 11 | +beforeAll(function () { |
| 12 | + function fireFailedEvent() |
22 | 13 | { |
23 | | - parent::setUp(); |
24 | | - |
25 | | - NotificationFacade::fake(); |
| 14 | + return event(new JobFailed('test', new Job(), new \Exception())); |
26 | 15 | } |
27 | | - |
28 | | - /** @test */ |
29 | | - public function it_can_send_notification_when_a_job_failed() |
| 16 | + function returnsFalseWhenExceptionIsEmpty($notification) |
30 | 17 | { |
31 | | - $this->fireFailedEvent(); |
| 18 | + $message = $notification->getEvent()->exception->getMessage(); |
32 | 19 |
|
33 | | - NotificationFacade::assertSentTo(new Notifiable(), Notification::class); |
| 20 | + return !empty($message); |
34 | 21 | } |
| 22 | +}); |
35 | 23 |
|
36 | | - /** @test */ |
37 | | - public function it_can_send_notification_when_job_failed_to_different_notifiable() |
38 | | - { |
39 | | - $this->app['config']->set('failed-job-monitor.notifiable', AnotherNotifiable::class); |
| 24 | +beforeEach(function () { |
| 25 | + NotificationFacade::fake(); |
| 26 | +}); |
40 | 27 |
|
41 | | - $this->fireFailedEvent(); |
| 28 | +it('can send notification when a job failed', function () { |
| 29 | + fireFailedEvent(); |
42 | 30 |
|
43 | | - NotificationFacade::assertSentTo(new AnotherNotifiable(), Notification::class); |
44 | | - } |
| 31 | + NotificationFacade::assertSentTo(new Notifiable(), Notification::class); |
| 32 | +}); |
45 | 33 |
|
46 | | - /** @test */ |
47 | | - public function it_can_send_notification_when_job_failed_to_different_notification() |
48 | | - { |
49 | | - $this->app['config']->set('failed-job-monitor.notification', AnotherNotification::class); |
| 34 | +it('can send notification when job failed to different notifiable', function () { |
| 35 | + config()->set('failed-job-monitor.notifiable', AnotherNotifiable::class); |
50 | 36 |
|
51 | | - $this->fireFailedEvent(); |
| 37 | + fireFailedEvent(); |
52 | 38 |
|
53 | | - NotificationFacade::assertSentTo(new Notifiable(), AnotherNotification::class); |
54 | | - } |
| 39 | + NotificationFacade::assertSentTo(new AnotherNotifiable(), Notification::class); |
| 40 | +}); |
55 | 41 |
|
56 | | - /** @test */ |
57 | | - public function it_filters_out_notifications_when_the_notificationFilter_returns_false() |
58 | | - { |
59 | | - $this->app['config']->set('failed-job-monitor.callback', [$this, 'returnsFalseWhenExceptionIsEmpty']); |
| 42 | +it('can send notification when job failed to different notification', function () { |
| 43 | + config()->set('failed-job-monitor.notification', AnotherNotification::class); |
60 | 44 |
|
61 | | - $this->fireFailedEvent(); |
| 45 | + fireFailedEvent(); |
62 | 46 |
|
63 | | - NotificationFacade::assertNotSentTo(new Notifiable(), AnotherNotification::class); |
64 | | - } |
| 47 | + NotificationFacade::assertSentTo(new Notifiable(), AnotherNotification::class); |
| 48 | +}); |
65 | 49 |
|
66 | | - protected function fireFailedEvent() |
67 | | - { |
68 | | - return event(new JobFailed('test', new Job(), new \Exception())); |
69 | | - } |
| 50 | +it('filters out notifications when the notificationFilter returns `false`', function () { |
| 51 | + config()->set('failed-job-monitor.callback', 'returnsFalseWhenExceptionIsEmpty'); |
70 | 52 |
|
71 | | - public function returnsFalseWhenExceptionIsEmpty($notification) |
72 | | - { |
73 | | - $message = $notification->getEvent()->exception->getMessage(); |
| 53 | + fireFailedEvent(); |
74 | 54 |
|
75 | | - return ! empty($message); |
76 | | - } |
77 | | -} |
| 55 | + NotificationFacade::assertNotSentTo(new Notifiable(), AnotherNotification::class); |
| 56 | +}); |
0 commit comments