Init
This commit is contained in:
13 files changed
+10287
No files matched your search
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Mattitja\Analytics\Services\AnalyticsClient;
|
||||
|
||||
beforeEach(function () {
|
||||
Http::fake();
|
||||
});
|
||||
|
||||
it('sends analytics data to the correct endpoint', function () {
|
||||
$client = new AnalyticsClient;
|
||||
$data = [
|
||||
'k' => 'test-site-key',
|
||||
'u' => 'https://example.com/test',
|
||||
'r' => 'https://google.com',
|
||||
'ip' => '192.168.1.1',
|
||||
'ua' => 'Mozilla/5.0 Chrome',
|
||||
];
|
||||
|
||||
$client->send($data);
|
||||
|
||||
Http::assertSent(function ($request) use ($data) {
|
||||
return $request->url() === 'https://analytics.mattitja.cloud/api/send' &&
|
||||
$request->method() === 'POST' &&
|
||||
$request->data() === $data;
|
||||
});
|
||||
});
|
||||
|
||||
it('handles API failures gracefully', function () {
|
||||
Http::fake([
|
||||
'analytics.mattitja.cloud/*' => Http::response('Server Error', 500),
|
||||
]);
|
||||
|
||||
$client = new AnalyticsClient;
|
||||
$data = [
|
||||
'k' => 'test-site-key',
|
||||
'u' => 'https://example.com/test',
|
||||
'r' => 'https://google.com',
|
||||
'ip' => '192.168.1.1',
|
||||
'ua' => 'Mozilla/5.0 Chrome',
|
||||
];
|
||||
|
||||
expect(fn () => $client->send($data))->not->toThrow(Exception::class);
|
||||
});
|
||||
|
||||
it('uses hardcoded timeout', function () {
|
||||
$client = new AnalyticsClient;
|
||||
$data = [
|
||||
'k' => 'test-site-key',
|
||||
'u' => 'https://example.com/test',
|
||||
'r' => '',
|
||||
'ip' => '192.168.1.1',
|
||||
'ua' => 'Mozilla/5.0 Chrome',
|
||||
];
|
||||
|
||||
$client->send($data);
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return $request->data()['k'] === 'test-site-key';
|
||||
});
|
||||
});
|
||||
|
||||
it('validates required data fields', function () {
|
||||
$client = new AnalyticsClient;
|
||||
|
||||
expect(fn () => $client->send([]))->toThrow(InvalidArgumentException::class)
|
||||
->and(fn () => $client->send(['k' => 'test']))->toThrow(InvalidArgumentException::class)
|
||||
->and(fn () => $client->send(['k' => 'test', 'u' => 'url', 'ip' => '1.1.1.1']))->toThrow(InvalidArgumentException::class);
|
||||
});
|
||||
|
||||
it('filters out empty referrer URLs', function () {
|
||||
$client = new AnalyticsClient;
|
||||
$data = [
|
||||
'k' => 'test-site-key',
|
||||
'u' => 'https://example.com/test',
|
||||
'r' => '',
|
||||
'ip' => '192.168.1.1',
|
||||
'ua' => 'Mozilla/5.0 Chrome',
|
||||
];
|
||||
|
||||
$client->send($data);
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
$sentData = $request->data();
|
||||
|
||||
return ! isset($sentData['r']);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user