diff --git a/README.md b/README.md index 60c2a06..c22907e 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,7 @@ You can chain methods to the `visit` method. Here are a list of the available me | METHOD | SYNTAX | DESCRIPTION | EXAMPLE | | ----------- | ----------- | ----------- | ----------- | | `withIp()` | string `$ip = null` | Set an Ip address (default `request()->ip()`) | `$post->visit()->withIp()` | +| `withSession()` | string `$session = null` | Set an Session ID (default `session()->getId()`) | `$post->visit()->withSession()` | |`withData()` | array `$data` | Set custom data | `$post->visit()->withData(['region' => 'USA'])` | | `withUser()` | Model `$user = null` | Set a user model (default `auth()->user()`) | `$user->visit()->withUser()` | diff --git a/src/PendingVisit.php b/src/PendingVisit.php index a41aa6b..b248a34 100644 --- a/src/PendingVisit.php +++ b/src/PendingVisit.php @@ -47,6 +47,19 @@ public function withIP(string $ip = null): self return $this; } + /** + * Set Session attribute + * + * @param string $session + * @return $this + */ + public function withSession(string $session = null): self + { + $this->attributes['session'] = $session ?? session()->getId(); + + return $this; + } + /** * Set Custom Data attribute * diff --git a/tests/Feature/Visits/VisitsTest.php b/tests/Feature/Visits/VisitsTest.php index 3bebb3b..08decb8 100644 --- a/tests/Feature/Visits/VisitsTest.php +++ b/tests/Feature/Visits/VisitsTest.php @@ -44,6 +44,28 @@ ]); }); +it('creates a visit with the default session id', function () { + $post = Post::factory()->create(); + + $post->visit()->withSession(); + + expect($post->visits->first()->data) + ->toMatchArray([ + 'session' => session()->getId(), + ]); +}); + +it('creates a visit with the given session id', function () { + $post = Post::factory()->create(); + + $post->visit()->withSession('RSXXvRiviUu2wO3RTmLESztufikmuV2F8KSugDzu'); + + expect($post->visits->first()->data) + ->toMatchArray([ + 'session' => 'RSXXvRiviUu2wO3RTmLESztufikmuV2F8KSugDzu', + ]); +}); + it('gets the correct ip when creating a visit', function () { $post = Post::factory()->create();