trypost/tests/Unit/Exceptions/Social/FacebookPublishExceptionTest.php

138 lines
4.6 KiB
PHP

<?php
declare(strict_types=1);
use App\Exceptions\Social\ErrorCategory;
use App\Exceptions\Social\FacebookPublishException;
use App\Exceptions\TokenExpiredException;
use Illuminate\Support\Facades\Http;
test('code 1363031 maps to MediaFormat category', function () {
$response = Http::response([
'error' => [
'message' => 'Unsupported file format.',
'type' => 'FacebookApiException',
'code' => 1363031,
],
], 400);
$fakeResponse = Http::fake(['*' => $response])->post('https://graph.facebook.com/test');
$exception = FacebookPublishException::fromApiResponse($fakeResponse);
expect($exception)
->toBeInstanceOf(FacebookPublishException::class)
->and($exception->userMessage)->toBe('Unsupported file format.')
->and($exception->category)->toBe(ErrorCategory::MediaFormat);
});
test('code 190 throws TokenExpiredException', function () {
$response = Http::response([
'error' => [
'message' => 'Invalid OAuth access token.',
'type' => 'FacebookApiException',
'code' => 190,
],
], 400);
$fakeResponse = Http::fake(['*' => $response])->post('https://graph.facebook.com/test');
FacebookPublishException::fromApiResponse($fakeResponse);
})->throws(TokenExpiredException::class);
test('OAuthException type throws TokenExpiredException', function () {
$response = Http::response([
'error' => [
'message' => 'Invalid OAuth access token.',
'type' => 'OAuthException',
'code' => 400,
],
], 400);
$fakeResponse = Http::fake(['*' => $response])->post('https://graph.facebook.com/test');
FacebookPublishException::fromApiResponse($fakeResponse);
})->throws(TokenExpiredException::class);
test('code 4 maps to RateLimit category', function () {
$response = Http::response([
'error' => [
'message' => 'Too many API calls.',
'type' => 'FacebookApiException',
'code' => 4,
],
], 400);
$fakeResponse = Http::fake(['*' => $response])->post('https://graph.facebook.com/test');
$exception = FacebookPublishException::fromApiResponse($fakeResponse);
expect($exception->category)->toBe(ErrorCategory::RateLimit)
->and($exception->userMessage)->toBe('Too many API calls. Please try again later.');
});
test('code 1363042 maps to Permission category', function () {
$response = Http::response([
'error' => [
'message' => 'No permission to upload video here.',
'type' => 'FacebookApiException',
'code' => 1363042,
],
], 400);
$fakeResponse = Http::fake(['*' => $response])->post('https://graph.facebook.com/test');
$exception = FacebookPublishException::fromApiResponse($fakeResponse);
expect($exception->category)->toBe(ErrorCategory::Permission)
->and($exception->userMessage)->toBe('No permission to upload video here.');
});
test('code 506 maps to ContentPolicy category', function () {
$response = Http::response([
'error' => [
'message' => 'Duplicate post.',
'type' => 'FacebookApiException',
'code' => 506,
],
], 400);
$fakeResponse = Http::fake(['*' => $response])->post('https://graph.facebook.com/test');
$exception = FacebookPublishException::fromApiResponse($fakeResponse);
expect($exception->category)->toBe(ErrorCategory::ContentPolicy)
->and($exception->userMessage)->toBe('Duplicate post detected. Please modify content.');
});
test('unknown code maps to Unknown category with error message', function () {
$response = Http::response([
'error' => [
'message' => 'An unexpected error occurred.',
'type' => 'FacebookApiException',
'code' => 9999999,
],
], 400);
$fakeResponse = Http::fake(['*' => $response])->post('https://graph.facebook.com/test');
$exception = FacebookPublishException::fromApiResponse($fakeResponse);
expect($exception->category)->toBe(ErrorCategory::Unknown)
->and($exception->userMessage)->toBe('An unexpected error occurred.');
});
test('subcode 463 throws TokenExpiredException', function () {
$response = Http::response([
'error' => [
'message' => 'Session expired.',
'type' => 'FacebookApiException',
'code' => 102,
'error_subcode' => 463,
],
], 400);
$fakeResponse = Http::fake(['*' => $response])->post('https://graph.facebook.com/test');
FacebookPublishException::fromApiResponse($fakeResponse);
})->throws(TokenExpiredException::class);