Skip to content

Commit

Permalink
Move userClickCount to Visit model
Browse files Browse the repository at this point in the history
  • Loading branch information
realodix committed Jun 4, 2024
1 parent 07e2550 commit 3c523a1
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 62 deletions.
20 changes: 0 additions & 20 deletions app/Models/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,24 +227,4 @@ public function currentUserClickCount(): int
->where('user_id', auth()->id())
->count();
}

/**
* Total clicks from all users
*/
public function userClickCount(): int
{
return Visit::join('urls', 'visits.url_id', '=', 'urls.id')
->where('urls.user_id', '!=', null)
->count('visits.id');
}

/**
* Total clicks from all guest users
*/
public function guestUserClickCount(): int
{
return Visit::join('urls', 'visits.url_id', '=', 'urls.id')
->where('urls.user_id', '=', null)
->count('visits.id');
}
}
26 changes: 26 additions & 0 deletions app/Models/Visit.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,30 @@ public function url()
{
return $this->belongsTo(Url::class);
}

/*
|--------------------------------------------------------------------------
| General
|--------------------------------------------------------------------------
*/

/**
* Total clicks from all users
*/
public function userClickCount(): int
{
return self::join('urls', 'visits.url_id', '=', 'urls.id')
->where('urls.user_id', '!=', Url::GUEST_ID)
->count('visits.id');
}

/**
* Total clicks from all guest users
*/
public function guestUserUrlVisitCount(): int
{
return self::join('urls', 'visits.url_id', '=', 'urls.id')
->where('urls.user_id', Url::GUEST_ID)
->count('visits.id');
}
}
4 changes: 2 additions & 2 deletions resources/views/backend/about.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
$tUrl = numberAbbreviate($url->count());
$tUrlVisit = numberAbbreviate($visit->count());
$userUrlCount = numberAbbreviate($url->where('user_id', '!=' , null)->count());
$userClickCount = numberAbbreviate($url->userClickCount());
$userClickCount = numberAbbreviate($visit->userClickCount());
$guestUrlCount = numberAbbreviate($url->numberOfUrlFromGuests());
$guestUserClickCount = numberAbbreviate($url->guestUserClickCount());
$guestUserClickCount = numberAbbreviate($visit->guestUserUrlVisitCount());
@endphp
<dt>Total</dt>
<dd>{{$tUrl}} ({{$tUrlVisit}} visits)</dd>
Expand Down
40 changes: 0 additions & 40 deletions tests/Unit/Models/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,46 +269,6 @@ public function currentUserClickCount(): void
$this->assertSame($expected, $actual);
}

#[PHPUnit\Test]
public function userClickCount(): void
{
$nUser = 6;
$nGuest = 4;

Visit::factory()->count($nUser)
->for(Url::factory())
->create();

Visit::factory()->count($nGuest)
->for(Url::factory()->state([
'user_id' => Url::GUEST_ID,
]))
->create();

$this->assertSame($nUser, $this->url->userClickCount());
$this->assertSame($nUser + $nGuest, $this->visit->count());
}

#[PHPUnit\Test]
public function guestUserClickCount(): void
{
$nUser = 6;
$nGuest = 4;

Visit::factory()->count($nUser)
->for(Url::factory())
->create();

Visit::factory()->count($nGuest)
->for(Url::factory()->state([
'user_id' => Url::GUEST_ID,
]))
->create();

$this->assertSame($nGuest, $this->url->guestUserClickCount());
$this->assertSame($nUser + $nGuest, $this->visit->count());
}

public function testKeywordColumnIsCaseSensitive(): void
{
$url_1 = Url::factory()->create(['keyword' => 'foo', 'destination' => 'https://example.com']);
Expand Down
42 changes: 42 additions & 0 deletions tests/Unit/Models/VisitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class VisitTest extends TestCase
protected function setUp(): void
{
parent::setUp();

$this->visit = new Visit;
}

#[PHPUnit\Test]
Expand All @@ -23,4 +25,44 @@ public function belongsToUrlModel(): void
$this->assertEquals(1, $visit->url->count());
$this->assertInstanceOf(Url::class, $visit->url);
}

#[PHPUnit\Test]
public function userClickCount(): void
{
$nUser = 6;
$nGuest = 4;

Visit::factory()->count($nUser)
->for(Url::factory())
->create();

Visit::factory()->count($nGuest)
->for(Url::factory()->state([
'user_id' => Url::GUEST_ID,
]))
->create();

$this->assertSame($nUser, $this->visit->userClickCount());
$this->assertSame($nUser + $nGuest, $this->visit->count());
}

#[PHPUnit\Test]
public function guestUserUrlVisitCount(): void
{
$nUser = 6;
$nGuest = 4;

Visit::factory()->count($nUser)
->for(Url::factory())
->create();

Visit::factory()->count($nGuest)
->for(Url::factory()->state([
'user_id' => Url::GUEST_ID,
]))
->create();

$this->assertSame($nGuest, $this->visit->guestUserUrlVisitCount());
$this->assertSame($nUser + $nGuest, $this->visit->count());
}
}

0 comments on commit 3c523a1

Please sign in to comment.