Files
mattitja-analytics/tests/Feature/InstallCommandTest.php
T
mathiaspalmqvist 19f632d652 fix: send pageviews to the analytics API correctly
- Use the correct endpoint (analytics.mattitja.se instead of the
  non-existent .cloud domain)
- Authenticate with the site ingest secret as a Bearer token so the API
  trusts the forwarded visitor IP and user agent
- Forward the visitor Accept-Language header (required by bot detection)
- Only track successful GET requests rendering HTML
- Short timeouts and a file-based circuit breaker that pauses sending
  after connection failures or 5xx responses, so no queue, worker or
  cache is needed and a down API never slows the host site
- Log rejected requests instead of failing silently
2026-11-12 09:08:27 +01:00

67 lines
1.8 KiB
PHP

<?php
use Illuminate\Support\Facades\File;
beforeEach(function () {
$this->envPath = base_path('.env');
$this->configPath = config_path('mattitja-analytics.php');
if (File::exists($this->configPath)) {
File::delete($this->configPath);
}
});
afterEach(function () {
if (File::exists($this->configPath)) {
File::delete($this->configPath);
}
});
it('publishes the config file', function () {
$this->artisan('mattitja:install')
->assertSuccessful();
expect(File::exists($this->configPath))->toBeTrue();
});
it('displays success message', function () {
$this->artisan('mattitja:install')
->expectsOutputToContain('Mattitja Analytics installed successfully')
->assertSuccessful();
});
it('displays warning about site key', function () {
$this->artisan('mattitja:install')
->expectsOutputToContain('MATTITJA_ANALYTICS_SITE_KEY')
->assertSuccessful();
});
it('adds env variable if .env exists and key is missing', function () {
$originalContent = "APP_NAME=Test\n";
File::put($this->envPath, $originalContent);
$this->artisan('mattitja:install')
->assertSuccessful();
$envContent = File::get($this->envPath);
expect($envContent)->toContain('MATTITJA_ANALYTICS_SITE_KEY=')
->toContain('MATTITJA_ANALYTICS_INGEST_SECRET=');
File::put($this->envPath, $originalContent);
});
it('does not duplicate env variable if already exists', function () {
$originalContent = "APP_NAME=Test\nMATTITJA_ANALYTICS_SITE_KEY=existing-key\n";
File::put($this->envPath, $originalContent);
$this->artisan('mattitja:install')
->assertSuccessful();
$envContent = File::get($this->envPath);
expect(substr_count($envContent, 'MATTITJA_ANALYTICS_SITE_KEY'))->toBe(1);
File::put($this->envPath, "APP_NAME=Test\n");
});