Skip to content

Commit 4be6900

Browse files
committed
Fix Test
Signed-off-by: Pushpak Chhajed <[email protected]>
1 parent ab84b98 commit 4be6900

File tree

2 files changed

+44
-30
lines changed

2 files changed

+44
-30
lines changed

src/Telemetry/TelemetryCollector.php

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,91 @@
44

55
namespace Laravel\Boost\Telemetry;
66

7-
use const PHP_OS_FAMILY;
8-
use const PHP_VERSION;
9-
107
use Composer\InstalledVersions;
11-
use Illuminate\Support\Facades\Http;
8+
use Laravel\Boost\Concerns\MakesHttpRequests;
129
use Throwable;
1310

1411
class TelemetryCollector
1512
{
13+
use MakesHttpRequests;
14+
1615
protected const MAX_TOOLS_PER_FLUSH = 20;
1716

1817
public array $toolCounts = [];
1918

20-
protected bool $shutdownRegistered = false;
19+
protected bool $enabled;
2120

22-
public function record(string $toolName): void
23-
{
24-
if (! config('boost.telemetry.enabled')) {
25-
return;
26-
}
21+
protected string $url;
2722

28-
$totalCount = array_sum($this->toolCounts);
29-
if ($totalCount >= self::MAX_TOOLS_PER_FLUSH) {
30-
$this->flush();
31-
}
23+
protected string $sessionId;
24+
25+
protected string $laravelVersion;
26+
27+
public function __construct()
28+
{
29+
$this->enabled = config('boost.telemetry.enabled', false);
30+
if ($this->enabled) {
31+
$this->url = config('boost.telemetry.url', 'https://boost.laravel.com/api/telemetry');
32+
$this->sessionId = hash('sha256', base_path());
33+
$this->laravelVersion = app()->version();
34+
app()->terminating($this->flush(...));
3235

33-
if (! $this->shutdownRegistered) {
3436
if (extension_loaded('pcntl')) {
3537
pcntl_async_signals(true);
3638
pcntl_signal(SIGINT, $this->flush(...));
3739
pcntl_signal(SIGTERM, $this->flush(...));
3840
}
41+
}
42+
}
3943

40-
register_shutdown_function([$this, 'flush']);
44+
public function __destruct()
45+
{
46+
$this->flush();
47+
}
4148

42-
app()->terminating($this->flush(...));
49+
public function record(string $toolName): void
50+
{
51+
if (! $this->enabled) {
52+
return;
53+
}
4354

44-
$this->shutdownRegistered = true;
55+
$totalCount = array_sum($this->toolCounts);
56+
if ($totalCount >= self::MAX_TOOLS_PER_FLUSH) {
57+
$this->flush();
4558
}
4659

4760
$this->toolCounts[$toolName] = ($this->toolCounts[$toolName] ?? 0) + 1;
4861
}
4962

5063
public function flush(): void
5164
{
52-
if ($this->toolCounts === [] || ! config('boost.telemetry.enabled', true)) {
65+
if ($this->toolCounts === [] || ! $this->enabled) {
5366
return;
5467
}
5568

5669
try {
57-
Http::timeout(5)
58-
->withHeaders(['User-Agent' => 'Laravel Boost Telemetry'])
59-
->post(config('boost.telemetry.url'), ['data' => $this->buildPayload()]);
70+
$this->client()
71+
->timeout(5)
72+
->post($this->url, ['data' => $this->buildPayload()]);
6073
} catch (Throwable) {
6174
//
6275
} finally {
6376
$this->toolCounts = [];
6477
}
65-
6678
}
6779

6880
protected function buildPayload(): string
6981
{
7082
$version = InstalledVersions::getVersion('laravel/boost');
7183

7284
return base64_encode(json_encode([
73-
'session_id' => hash('sha256', base_path()),
85+
'session_id' => $this->sessionId,
7486
'boost_version' => $version,
7587
'php_version' => PHP_VERSION,
7688
'os' => PHP_OS_FAMILY,
77-
'laravel_version' => app()->version(),
89+
'laravel_version' => $this->laravelVersion,
7890
'tools' => $this->toolCounts,
79-
'timestamp' => now()->toIso8601String(),
91+
'timestamp' => date('c'),
8092
]));
8193
}
8294
}

tests/Unit/Telemetry/TelemetryCollectorTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@
2727
it('does not record when disabled via config', function (): void {
2828
config(['boost.telemetry.enabled' => false]);
2929

30-
$this->collector->record(DatabaseQuery::class);
30+
$collector = new TelemetryCollector;
31+
$collector->record(DatabaseQuery::class);
3132

32-
expect($this->collector->toolCounts)->toBe([]);
33+
expect($collector->toolCounts)->toBe([]);
3334
});
3435

3536
it('auto-flushes when reaching MAX_TOOLS_PER_FLUSH', function (): void {
@@ -108,8 +109,9 @@
108109
'*' => Http::response(['status' => 'ok'], 200),
109110
]);
110111

111-
$this->collector->toolCounts = ['SomeTool' => 1];
112-
$this->collector->flush();
112+
$collector = new TelemetryCollector;
113+
$collector->toolCounts = ['SomeTool' => 1];
114+
$collector->flush();
113115

114116
expect(Http::recorded())->toHaveCount(0);
115117
});

0 commit comments

Comments
 (0)