Skip to content

Commit

Permalink
Merge pull request #15 from juliangarcess/main
Browse files Browse the repository at this point in the history
FEAT: Added with session function
  • Loading branch information
ousid authored Aug 21, 2022
2 parents 98c9ee2 + a9e2532 commit ee744ee
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()` |

Expand Down
13 changes: 13 additions & 0 deletions src/PendingVisit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
22 changes: 22 additions & 0 deletions tests/Feature/Visits/VisitsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit ee744ee

Please sign in to comment.