Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

refactor: optimize json parsing and add missing test coverage

Paulo Castellano817d6822b91b
diff --git a/app/Services/Social/YouTubeAnalytics.php b/app/Services/Social/YouTubeAnalytics.php
index 8a9efc3d1..043cd6de6 100644
--- a/app/Services/Social/YouTubeAnalytics.php
+++ b/app/Services/Social/YouTubeAnalytics.php
@@ -60,13 +60,14 @@ class YouTubeAnalytics
return [];
}
- $rows = data_get($response->json(), 'rows', []);
+ $json = $response->json();
+ $rows = data_get($json, 'rows', []);
if (empty($rows)) {
return [];
}
- $columnHeaders = data_get($response->json(), 'columnHeaders', []);
+ $columnHeaders = data_get($json, 'columnHeaders', []);
$metricNames = collect($columnHeaders)->pluck('name')->toArray();
$values = data_get($rows, '0', []);
diff --git a/tests/Feature/YouTubeAnalyticsTest.php b/tests/Feature/YouTubeAnalyticsTest.php
index dba2df199..c52820279 100644
--- a/tests/Feature/YouTubeAnalyticsTest.php
+++ b/tests/Feature/YouTubeAnalyticsTest.php
@@ -5,6 +5,7 @@ declare(strict_types=1);
use App\Enums\SocialAccount\Platform;
use App\Enums\SocialAccount\Status as AccountStatus;
use App\Enums\User\Setup;
+use App\Exceptions\TokenExpiredException;
use App\Models\SocialAccount;
use App\Models\User;
use App\Models\Workspace;
@@ -166,6 +167,27 @@ test('youtube analytics refreshes expired token', function () {
expect($this->youtubeAccount->access_token)->toBe('new_access_token');
});
+test('youtube analytics throws exception when no refresh token', function () {
+ $this->youtubeAccount->update([
+ 'token_expires_at' => now()->subMinutes(5),
+ 'refresh_token' => null,
+ ]);
+
+ $analytics = app(YouTubeAnalytics::class);
+ $analytics->getMetrics($this->youtubeAccount);
+})->throws(TokenExpiredException::class);
+
+test('youtube analytics throws exception on token refresh failure', function () {
+ $this->youtubeAccount->update(['token_expires_at' => now()->subMinutes(5)]);
+
+ Http::fake([
+ 'https://oauth2.googleapis.com/token' => Http::response(['error' => 'invalid_grant'], 400),
+ ]);
+
+ $analytics = app(YouTubeAnalytics::class);
+ $analytics->getMetrics($this->youtubeAccount);
+})->throws(TokenExpiredException::class);
+
test('youtube is in supported analytics platforms', function () {
config(['trypost.self_hosted' => true]);