Skip to content

Commit 5674b9f

Browse files
FEAT: Added with session function
1 parent 98c9ee2 commit 5674b9f

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/PendingVisit.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ public function withIP(string $ip = null): self
4747
return $this;
4848
}
4949

50+
/**
51+
* Set Session attribute
52+
*
53+
* @param string $session
54+
* @return $this
55+
*/
56+
public function withSession(string $session = null): self
57+
{
58+
$this->attributes['session'] = $ip ?? request()->session()->getId();
59+
60+
return $this;
61+
}
62+
5063
/**
5164
* Set Custom Data attribute
5265
*

tests/Feature/Visits/VisitsTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,28 @@
4444
]);
4545
});
4646

47+
it('creates a visit with the default session id', function () {
48+
$post = Post::factory()->create();
49+
50+
$post->visit()->withSession();
51+
52+
expect($post->visits->first()->data)
53+
->toMatchArray([
54+
'session' => request()->session()->getId(),
55+
]);
56+
});
57+
58+
it('creates a visit with the given session id', function () {
59+
$post = Post::factory()->create();
60+
61+
$post->visit()->withSession('RSXXvRiviUu2wO3RTmLESztufikmuV2F8KSugDzu');
62+
63+
expect($post->visits->first()->data)
64+
->toMatchArray([
65+
'session' => 'RSXXvRiviUu2wO3RTmLESztufikmuV2F8KSugDzu',
66+
]);
67+
});
68+
4769
it('gets the correct ip when creating a visit', function () {
4870
$post = Post::factory()->create();
4971

0 commit comments

Comments
 (0)