- Add PageviewData readonly DTO class for type safety - Update AnalyticsClient to accept PageviewData instead of array - Move analytics sending to terminate() method (runs after response sent) - Update all tests for new DTO pattern
143 lines
3.6 KiB
PHP
143 lines
3.6 KiB
PHP
<?php
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
use Mattitja\Analytics\Data\PageviewData;
|
|
use Mattitja\Analytics\Services\AnalyticsClient;
|
|
|
|
beforeEach(function () {
|
|
Http::fake();
|
|
});
|
|
|
|
it('sends analytics data to the correct endpoint', function () {
|
|
$client = new AnalyticsClient;
|
|
$data = new PageviewData(
|
|
siteKey: 'testsiteky',
|
|
url: 'https://example.com/test',
|
|
ip: '192.168.1.1',
|
|
userAgent: 'Mozilla/5.0 Chrome',
|
|
referrer: 'https://google.com',
|
|
);
|
|
|
|
$client->send($data);
|
|
|
|
Http::assertSent(function ($request) {
|
|
return $request->url() === 'https://analytics.mattitja.cloud/api/send' &&
|
|
$request->method() === 'POST' &&
|
|
$request->data() === [
|
|
'k' => 'testsiteky',
|
|
'u' => 'https://example.com/test',
|
|
'ip' => '192.168.1.1',
|
|
'ua' => 'Mozilla/5.0 Chrome',
|
|
'r' => 'https://google.com',
|
|
];
|
|
});
|
|
});
|
|
|
|
it('handles API failures gracefully', function () {
|
|
Http::fake([
|
|
'analytics.mattitja.cloud/*' => Http::response('Server Error', 500),
|
|
]);
|
|
|
|
$client = new AnalyticsClient;
|
|
$data = new PageviewData(
|
|
siteKey: 'testsiteky',
|
|
url: 'https://example.com/test',
|
|
ip: '192.168.1.1',
|
|
userAgent: 'Mozilla/5.0 Chrome',
|
|
referrer: 'https://google.com',
|
|
);
|
|
|
|
expect(fn () => $client->send($data))->not->toThrow(Exception::class);
|
|
});
|
|
|
|
it('sends data with correct site key', function () {
|
|
$client = new AnalyticsClient;
|
|
$data = new PageviewData(
|
|
siteKey: 'testsiteky',
|
|
url: 'https://example.com/test',
|
|
ip: '192.168.1.1',
|
|
userAgent: 'Mozilla/5.0 Chrome',
|
|
);
|
|
|
|
$client->send($data);
|
|
|
|
Http::assertSent(function ($request) {
|
|
return $request->data()['k'] === 'testsiteky';
|
|
});
|
|
});
|
|
|
|
it('skips sending when required fields are empty', function () {
|
|
$client = new AnalyticsClient;
|
|
|
|
$client->send(new PageviewData(
|
|
siteKey: '',
|
|
url: 'https://example.com',
|
|
ip: '192.168.1.1',
|
|
userAgent: 'Mozilla/5.0',
|
|
));
|
|
|
|
$client->send(new PageviewData(
|
|
siteKey: 'testsiteky',
|
|
url: '',
|
|
ip: '192.168.1.1',
|
|
userAgent: 'Mozilla/5.0',
|
|
));
|
|
|
|
$client->send(new PageviewData(
|
|
siteKey: 'testsiteky',
|
|
url: 'https://example.com',
|
|
ip: '',
|
|
userAgent: 'Mozilla/5.0',
|
|
));
|
|
|
|
Http::assertNothingSent();
|
|
});
|
|
|
|
it('skips sending when URL exceeds 512 characters', function () {
|
|
$client = new AnalyticsClient;
|
|
$longUrl = 'https://example.com/'.str_repeat('a', 500);
|
|
|
|
$client->send(new PageviewData(
|
|
siteKey: 'testsiteky',
|
|
url: $longUrl,
|
|
ip: '192.168.1.1',
|
|
userAgent: 'Mozilla/5.0 Chrome',
|
|
));
|
|
|
|
Http::assertNothingSent();
|
|
});
|
|
|
|
it('excludes empty referrer from payload', function () {
|
|
$client = new AnalyticsClient;
|
|
$data = new PageviewData(
|
|
siteKey: 'testsiteky',
|
|
url: 'https://example.com/test',
|
|
ip: '192.168.1.1',
|
|
userAgent: 'Mozilla/5.0 Chrome',
|
|
referrer: '',
|
|
);
|
|
|
|
$client->send($data);
|
|
|
|
Http::assertSent(function ($request) {
|
|
return ! isset($request->data()['r']);
|
|
});
|
|
});
|
|
|
|
it('excludes null referrer from payload', function () {
|
|
$client = new AnalyticsClient;
|
|
$data = new PageviewData(
|
|
siteKey: 'testsiteky',
|
|
url: 'https://example.com/test',
|
|
ip: '192.168.1.1',
|
|
userAgent: 'Mozilla/5.0 Chrome',
|
|
referrer: null,
|
|
);
|
|
|
|
$client->send($data);
|
|
|
|
Http::assertSent(function ($request) {
|
|
return ! isset($request->data()['r']);
|
|
});
|
|
});
|