Skip to content

Commit ee744ee

Browse files
authored
Merge pull request #15 from juliangarcess/main
FEAT: Added with session function
2 parents 98c9ee2 + a9e2532 commit ee744ee

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ You can chain methods to the `visit` method. Here are a list of the available me
8080
| METHOD | SYNTAX | DESCRIPTION | EXAMPLE |
8181
| ----------- | ----------- | ----------- | ----------- |
8282
| `withIp()` | string `$ip = null` | Set an Ip address (default `request()->ip()`) | `$post->visit()->withIp()` |
83+
| `withSession()` | string `$session = null` | Set an Session ID (default `session()->getId()`) | `$post->visit()->withSession()` |
8384
|`withData()` | array `$data` | Set custom data | `$post->visit()->withData(['region' => 'USA'])` |
8485
| `withUser()` | Model `$user = null` | Set a user model (default `auth()->user()`) | `$user->visit()->withUser()` |
8586

src/PendingVisit.php

+13
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'] = $session ?? session()->getId();
59+
60+
return $this;
61+
}
62+
5063
/**
5164
* Set Custom Data attribute
5265
*

tests/Feature/Visits/VisitsTest.php

+22
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' => 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)