|
2 | 2 |
|
3 | 3 | namespace ProtoneMedia\AnalyticsEventTracking\Tests;
|
4 | 4 |
|
| 5 | +use Illuminate\Foundation\Auth\User; |
| 6 | +use Illuminate\Support\Facades\Auth; |
5 | 7 | use Illuminate\Support\Facades\Bus;
|
6 | 8 | use ProtoneMedia\AnalyticsEventTracking\Jobs\SendEventToAnalytics;
|
7 | 9 | use ProtoneMedia\AnalyticsEventTracking\Tests\Fakes\BroadcastMe;
|
@@ -33,4 +35,57 @@ public function it_can_dispatch_jobs_on_a_dedicated_queue()
|
33 | 35 | return $job->queue === 'http';
|
34 | 36 | });
|
35 | 37 | }
|
| 38 | + |
| 39 | + private function authenticateUser() |
| 40 | + { |
| 41 | + Auth::login( |
| 42 | + User::unguarded(fn () => new User(['id' => 1337])) |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + /** @test */ |
| 47 | + public function it_sets_the_user_id_if_authenticated_and_configuration_setting_is_enabled() |
| 48 | + { |
| 49 | + config(['analytics-event-tracking.send_user_id' => true]); |
| 50 | + |
| 51 | + $this->authenticateUser(); |
| 52 | + |
| 53 | + Bus::fake(); |
| 54 | + |
| 55 | + event(new BroadcastMe); |
| 56 | + |
| 57 | + Bus::assertDispatched(SendEventToAnalytics::class, function ($job) { |
| 58 | + return $job->userId == 1337; |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + /** @test */ |
| 63 | + public function it_doesnt_set_the_user_id_if_authenticated_and_configuration_setting_is_disabled() |
| 64 | + { |
| 65 | + config(['analytics-event-tracking.send_user_id' => false]); |
| 66 | + |
| 67 | + $this->authenticateUser(); |
| 68 | + |
| 69 | + Bus::fake(); |
| 70 | + |
| 71 | + event(new BroadcastMe); |
| 72 | + |
| 73 | + Bus::assertDispatched(SendEventToAnalytics::class, function ($job) { |
| 74 | + return is_null($job->userId); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + /** @test */ |
| 79 | + public function it_doesnt_set_the_user_id_if_not_authenticated() |
| 80 | + { |
| 81 | + config(['analytics-event-tracking.send_user_id' => true]); |
| 82 | + |
| 83 | + Bus::fake(); |
| 84 | + |
| 85 | + event(new BroadcastMe); |
| 86 | + |
| 87 | + Bus::assertDispatched(SendEventToAnalytics::class, function ($job) { |
| 88 | + return is_null($job->userId); |
| 89 | + }); |
| 90 | + } |
36 | 91 | }
|
0 commit comments