Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest]
php: [8.4]
php: [8.4, 8.5]
laravel: [11.*, 12.*]
include:
- laravel: 11.*
Expand All @@ -27,6 +27,9 @@ jobs:
carbon: ^3.5
contracts: ^12.0
collision: ^8.0
exclude:
- laravel: 11.*
php: 8.5

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.os }}

Expand Down Expand Up @@ -54,5 +57,8 @@ jobs:
- name: List Installed Dependencies
run: composer show -D

- name: Run Security Audit
run: composer audit

- name: Execute tests
run: vendor/bin/pest
run: vendor/bin/pest --ci --no-coverage
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
],
"require": {
"php": "^8.4",
"php": "^8.4|^8.5",
"code16/sharp": "^9.0",
"illuminate/contracts": "^11.0||^12.0",
"spatie/laravel-package-tools": "^1.16"
Expand Down Expand Up @@ -66,6 +66,6 @@
}
}
},
"minimum-stability": "dev",
"minimum-stability": "stable",
"prefer-stable": true
}
13 changes: 11 additions & 2 deletions src/Client/FathomClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,17 @@ public function getEndDate(): Carbon
public function getSite(): ?Site
{
if (config()->boolean('sharp-fathom-dashboard.cache')) {
$data = Cache::get($this->siteId);
return Cache::remember($this->siteId, $this->getCacheDuration(), fn () => $this->executeGetSite());
$site = Cache::get($this->siteId);
if($site === null){
try{
$site = $this->executeGetSite();
Cache::put($this->siteId, $site, $this->getCacheDuration());
}catch (\Throwable $e) {
report($e);
return null;
}
}
return $site;
} else {
return $this->executeGetSite();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ArchTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

arch('it will not use debugging functions')
->expect(['dd', 'dump', 'ray', 'die'])
->expect(['dd', 'dump', 'ray', 'die', 'vardump'])
->each->not->toBeUsed();
2 changes: 1 addition & 1 deletion tests/FathomAnalyticsDateFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

expect($default)
->toBeArray()
->and($default['start'])->toBe('2025-09-23')
->and($default['start'])->toBe('2025-09-24')
->and($default['end'])->toBe('2025-10-23');

Carbon::setTestNow(); // clear
Expand Down
75 changes: 75 additions & 0 deletions tests/FathomClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,81 @@
->and($second->id)->toBe('SITE_123');
});

it('getStats returns day-keyed array and handles 200', function () {
$startDate = \Illuminate\Support\Carbon::parse('2025-01-01');
$endDate = \Illuminate\Support\Carbon::parse('2025-01-02');

Http::fake([
'*/aggregations*' => Http::response([
['date' => '2025-01-01', 'visits' => 10, 'pageviews' => 20],
['date' => '2025-01-02', 'visits' => 15, 'pageviews' => 25],
], 200),
]);

$client = new FathomClient($startDate, $endDate);
$stats = $client->getStats();

expect($stats)->toBeArray()->toHaveCount(2)
->and($stats)->toHaveKeys(['2025-01-01', '2025-01-02'])
->and($stats['2025-01-01']['visits'])->toBe(10);
});

it('getStats uses cache when enabled', function () {
config()->set('sharp-fathom-dashboard.cache', true);
config()->set('sharp-fathom-dashboard.cache_ttl', 60);

Http::fakeSequence()
->push([
['date' => '2025-01-01', 'visits' => 10],
], 200)
->push([], 500);

$startDate = \Illuminate\Support\Carbon::parse('2025-01-01');
$client = new FathomClient($startDate, $startDate);

$first = $client->getStats();
$second = $client->getStats();

expect($first)->toBeArray()
->and($second)->toBe($first);
});

it('getMostViewedPages uses cache when enabled', function () {
config()->set('sharp-fathom-dashboard.cache', true);
config()->set('sharp-fathom-dashboard.cache_ttl', 60);

Http::fakeSequence()
->push([
['pathname' => '/', 'pageviews' => 10],
], 200)
->push([], 500);

$client = new FathomClient();
$first = $client->getMostViewedPages();
$second = $client->getMostViewedPages();

expect($first)->toBeArray()
->and($second)->toBe($first);
});

it('getTopReferrers uses cache when enabled', function () {
config()->set('sharp-fathom-dashboard.cache', true);
config()->set('sharp-fathom-dashboard.cache_ttl', 60);

Http::fakeSequence()
->push([
['referrer_hostname' => 'google.com', 'pageviews' => 10],
], 200)
->push([], 500);

$client = new FathomClient();
$first = $client->getTopReferrers();
$second = $client->getTopReferrers();

expect($first)->toBeArray()
->and($second)->toBe($first);
});

it('executeGetMostViewedPages returns array on 200 and throws on error', function () {
Http::fakeSequence()
->push([
Expand Down
32 changes: 32 additions & 0 deletions tests/OpenFathomSharpCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Code16\SharpFathomDashboard\Sharp\Commands\OpenFathomSharpCommand;

it('has a label', function () {
$command = new OpenFathomSharpCommand();
expect($command->label())->toBe('Open Fathom dashboard');
});

it('authorizes if fathom_access_url is set', function () {
$command = new OpenFathomSharpCommand();

config(['sharp-fathom-dashboard.fathom_access_url' => null]);
expect($command->authorize())->toBeFalse();

config(['sharp-fathom-dashboard.fathom_access_url' => 'https://app.usefathom.com/share/SITE_123/site']);
expect($command->authorize())->toBeTrue();
});

it('returns a link on execute', function () {
$url = 'https://app.usefathom.com/share/SITE_123/site';
config(['sharp-fathom-dashboard.fathom_access_url' => $url]);

$command = new OpenFathomSharpCommand();
$result = $command->execute();

expect($result)->toBe([
'action' => 'link',
'link' => $url,
'openInNewTab' => false,
]);
});
77 changes: 77 additions & 0 deletions tests/SharpFathomDashboardTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

use Code16\SharpFathomDashboard\Sharp\Filters\FathomAnalyticsDateFilter;
use Code16\SharpFathomDashboard\Sharp\SharpFathomDashboard;
use Code16\SharpFathomDashboard\Client\FathomClient;
use Illuminate\Support\Facades\Http;
use Code16\Sharp\Dashboard\Widgets\WidgetsContainer;
use Code16\Sharp\Dashboard\Layout\DashboardLayout;

beforeEach(function () {
config()->set('sharp-fathom-dashboard.fathom_api_key', 'test-key');
config()->set('sharp-fathom-dashboard.fathom_site_id', 'SITE_123');
config()->set('sharp-fathom-dashboard.chart.datasets', ['pageviews', 'unique_visitors']);
});

it('can build widgets', function () {
$dashboard = new SharpFathomDashboard();

expect($dashboard->widgets())->toHaveCount(7)
->and($dashboard->widgets())->toHaveKeys([
'unique_visitors',
'pageviews',
'avg_time_on_site',
'bounce_rate',
'daily_analytics',
'most_viewed_pages',
'top_referrers'
]);
});

it('can build layout', function () {
$dashboard = new SharpFathomDashboard();

$layout = $dashboard->widgetsLayout();

expect($layout['sections'])->toHaveCount(3);
});

it('can build widgets data', function () {
Http::fake([
'*/sites/*' => Http::response(['id' => 'SITE_123', 'name' => 'Test Site'], 200),
'*/aggregations*' => Http::response([
[
'hostname' => 'ex.test',
'pathname' => '/',
'pageviews' => 10,
'date' => now()->subDay()->format('Y-m-d'),
'visits' => 10,
'uniques' => 5,
'avg_duration' => 60,
'bounce_rate' => 0.5,
'referrer_hostname' => 'google.com',
'referrer_pathname' => '/'
],
], 200),
]);

$dashboard = new SharpFathomDashboard();
$dashboard->initQueryParams([
FathomAnalyticsDateFilter::class => '2025-01-01 - 2025-01-31'
]);

$data = $dashboard->data();

expect($data)->toHaveKeys([
'unique_visitors',
'pageviews',
'avg_time_on_site',
'bounce_rate',
'daily_analytics',
'most_viewed_pages',
'top_referrers'
]);

expect($data['unique_visitors']['data']['figure'])->toBe('10')
->and($data['pageviews']['data']['figure'])->toBe('10');
});