|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Notifications\AnonymousNotifiable; |
| 4 | +use Illuminate\Support\Carbon; |
| 5 | +use Illuminate\Support\Facades\Cache; |
| 6 | +use Illuminate\Support\Facades\Http; |
| 7 | +use Statamic\Facades\User; |
| 8 | +use TransformStudios\Front\Notifications\BaseNotification; |
| 9 | +use TransformStudios\Front\Notifications\Channel; |
| 10 | + |
| 11 | +beforeEach(function () { |
| 12 | + config()->set('front.notifications.channel', 'test-channel'); |
| 13 | + Http::preventStrayRequests(); |
| 14 | + Http::fake([ |
| 15 | + 'https://api2.frontapp.com/channels/test-channel/messages' => Http::response([ |
| 16 | + '_links' => [ |
| 17 | + 'related' => [ |
| 18 | + 'conversation' => 'https://transform-studios.api.frontapp.com/conversations/cnv_id', |
| 19 | + ], |
| 20 | + ], |
| 21 | + ], 200), |
| 22 | + 'https://api2.frontapp.com/conversations/cnv_id/messages' => Http::response([], 200), |
| 23 | + ]); |
| 24 | +}); |
| 25 | + |
| 26 | +test('can send front message', function () { |
| 27 | + $users = collect([makeUser('erin@transformstudios.com'), makeUser('erin@silentz.co')]); |
| 28 | + $notification = new TestNotification('some-key', 'Monitor Alert: Error Detected', '', $users); |
| 29 | + |
| 30 | + expect((new Channel)->send(new AnonymousNotifiable, $notification))->toBeTrue(); |
| 31 | +}); |
| 32 | + |
| 33 | +it('stores the conversation id', function () { |
| 34 | + $notification = new TestNotification('some-key', 'Monitor Alert: Error Detected', '', collect([makeUser('foo@bar.com')])); |
| 35 | + |
| 36 | + expect(Cache::get('some-key'))->toBeNull(); |
| 37 | + expect((new Channel)->send(new AnonymousNotifiable, $notification))->toBeTrue(); |
| 38 | + expect(Cache::get('some-key'))->toEqual('cnv_id'); |
| 39 | +}); |
| 40 | + |
| 41 | +it('removes the conversation id when alert cleared', function () { |
| 42 | + $notification = new TestNotification('some-key', 'Monitor Alert: Error Cleared', '', collect([makeUser('foo@bar.com')])); |
| 43 | + |
| 44 | + Cache::put('some-key', 'cnv_id'); |
| 45 | + expect(Cache::get('some-key'))->not->toBeNull(); |
| 46 | + expect((new Channel)->send(new AnonymousNotifiable, $notification))->toBeTrue(); |
| 47 | + expect(Cache::get('some-key'))->toBeNull(); |
| 48 | +}); |
| 49 | + |
| 50 | +it('adds to the conversation when conversation id exists', function () { |
| 51 | + $user = makeUser('foo@bar.com'); |
| 52 | + $notification = new TestNotification('some-key', 'Monitor Alert: Error Detected', '', collect([$user])); |
| 53 | + $anotherNotification = new TestNotification('some-key', 'Monitor Alert: Error Cleared', '', collect([$user])); |
| 54 | + $channel = new Channel; |
| 55 | + |
| 56 | + expect($channel->send(new AnonymousNotifiable, $notification))->toBeTrue(); |
| 57 | + expect($channel->send(new AnonymousNotifiable, $anotherNotification))->toBeTrue(); |
| 58 | +}); |
| 59 | + |
| 60 | +class TestNotification extends BaseNotification {} |
0 commit comments