From 8cbc372d91ddace6898fcb81e2dbd1a96880b90c Mon Sep 17 00:00:00 2001 From: Paulo Castellano Date: Wed, 1 Apr 2026 11:09:26 -0300 Subject: [PATCH] test: add error_context verification tests for all failure scenarios --- .../Jobs/PublishToSocialPlatformTest.php | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/tests/Feature/Jobs/PublishToSocialPlatformTest.php b/tests/Feature/Jobs/PublishToSocialPlatformTest.php index efc7138e..469e5521 100644 --- a/tests/Feature/Jobs/PublishToSocialPlatformTest.php +++ b/tests/Feature/Jobs/PublishToSocialPlatformTest.php @@ -7,6 +7,8 @@ use App\Enums\SocialAccount\Status as AccountStatus; use App\Enums\UserWorkspace\Role; use App\Events\PostPlatformStatusUpdated; +use App\Exceptions\Social\ErrorCategory; +use App\Exceptions\Social\LinkedInPublishException; use App\Exceptions\TokenExpiredException; use App\Jobs\PublishToSocialPlatform; use App\Jobs\SendNotification; @@ -336,3 +338,70 @@ $this->postPlatform->refresh(); expect($this->postPlatform->platform_post_id)->toBe('existing-123'); }); + +test('publish to social platform saves error context on generic failure', function () { + Event::fake(); + + $this->postPlatform->update(['content' => 'Test content here']); + + $publisher = Mockery::mock(LinkedInPublisher::class); + $publisher->shouldReceive('publish')->andThrow(new Exception('Something broke')); + + $this->app->instance(LinkedInPublisher::class, $publisher); + + (new PublishToSocialPlatform($this->postPlatform))->handle(); + + $this->postPlatform->refresh(); + expect($this->postPlatform->error_context)->toBeArray(); + expect($this->postPlatform->error_context['category'])->toBe('unknown'); + expect($this->postPlatform->error_context['failed_at'])->toBeString(); + expect($this->postPlatform->error_context['content_length'])->toBe(17); + expect($this->postPlatform->error_context['media_count'])->toBe(0); +}); + +test('publish to social platform saves error context on social publish exception', function () { + Event::fake(); + + $this->postPlatform->update(['content' => 'Hello world']); + + $publisher = Mockery::mock(LinkedInPublisher::class); + $publisher->shouldReceive('publish')->andThrow( + new LinkedInPublishException( + 'Not authorized to post', + ErrorCategory::Permission, + '403', + '{"error": "forbidden"}', + ) + ); + + $this->app->instance(LinkedInPublisher::class, $publisher); + + (new PublishToSocialPlatform($this->postPlatform))->handle(); + + $this->postPlatform->refresh(); + expect($this->postPlatform->error_context)->toBeArray(); + expect($this->postPlatform->error_context['category'])->toBe('permission'); + expect($this->postPlatform->error_context['platform_error_code'])->toBe('403'); + expect($this->postPlatform->error_context['content_length'])->toBe(11); +}); + +test('publish to social platform saves error context on token expired', function () { + Event::fake(); + Mail::fake(); + + $publisher = Mockery::mock(LinkedInPublisher::class); + $publisher->shouldReceive('publish')->andThrow(new TokenExpiredException('Token expired', '190')); + + $this->app->instance(LinkedInPublisher::class, $publisher); + + $verifier = Mockery::mock(ConnectionVerifier::class); + $verifier->shouldReceive('verify')->andThrow(new TokenExpiredException('Refresh failed')); + $this->app->instance(ConnectionVerifier::class, $verifier); + + (new PublishToSocialPlatform($this->postPlatform))->handle(); + + $this->postPlatform->refresh(); + expect($this->postPlatform->error_context)->toBeArray(); + expect($this->postPlatform->error_context['category'])->toBe('token_expired'); + expect($this->postPlatform->error_context['platform_error_code'])->toBe('190'); +});