From 3c523a183b0d4a67d118878cd1cdef91c38b86e8 Mon Sep 17 00:00:00 2001 From: Kei Date: Wed, 5 Jun 2024 01:46:08 +0700 Subject: [PATCH] Move userClickCount to Visit model --- app/Models/Url.php | 20 ------------ app/Models/Visit.php | 26 +++++++++++++++ resources/views/backend/about.blade.php | 4 +-- tests/Unit/Models/UrlTest.php | 40 ----------------------- tests/Unit/Models/VisitTest.php | 42 +++++++++++++++++++++++++ 5 files changed, 70 insertions(+), 62 deletions(-) diff --git a/app/Models/Url.php b/app/Models/Url.php index d1a8d8e85..4697f5da2 100644 --- a/app/Models/Url.php +++ b/app/Models/Url.php @@ -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'); - } } diff --git a/app/Models/Visit.php b/app/Models/Visit.php index d7a5d8a63..4145a878c 100644 --- a/app/Models/Visit.php +++ b/app/Models/Visit.php @@ -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'); + } } diff --git a/resources/views/backend/about.blade.php b/resources/views/backend/about.blade.php index 3a68281a2..2ea0d0824 100644 --- a/resources/views/backend/about.blade.php +++ b/resources/views/backend/about.blade.php @@ -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
Total
{{$tUrl}} ({{$tUrlVisit}} visits)
diff --git a/tests/Unit/Models/UrlTest.php b/tests/Unit/Models/UrlTest.php index 2c5d8353d..8a0623e5e 100644 --- a/tests/Unit/Models/UrlTest.php +++ b/tests/Unit/Models/UrlTest.php @@ -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']); diff --git a/tests/Unit/Models/VisitTest.php b/tests/Unit/Models/VisitTest.php index 3ff657221..5efbc49d0 100644 --- a/tests/Unit/Models/VisitTest.php +++ b/tests/Unit/Models/VisitTest.php @@ -13,6 +13,8 @@ class VisitTest extends TestCase protected function setUp(): void { parent::setUp(); + + $this->visit = new Visit; } #[PHPUnit\Test] @@ -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()); + } }