trypost/tests/Unit/Enums/PlatformTest.php
Paulo Castellano 770266d36b refactor: centralize media size limits in config + drop Document type
Three things in one move:

1. Centralize per-type size caps in config/trypost.php under media.max_size_mb.
   The MediaType enum now reads from there:

       MediaType::Image->maxSizeInMb()    // 10  (env: MEDIA_IMAGE_MAX_SIZE_MB)
       MediaType::Video->maxSizeInMb()    // 1024 (env: MEDIA_VIDEO_MAX_SIZE_MB)

   Plus convenience helpers maxSizeInBytes() and maxSizeInKb() so callers
   don't have to multiply themselves. StoreAssetRequest now uses
   MediaType::Video->maxSizeInKb() in its 'max:' rule and mimes derived
   from MediaType::{Image,Video}->allowedMimeTypes(). storeChunked
   validation moved to the new StoreChunkedAssetRequest FormRequest
   (also reads from the enum). MediaAttacher uses
   MediaType::Video->maxSizeInBytes() as the streaming-abort threshold
   and enforces the per-type cap after MIME resolution.

2. Drop MediaType::Document. We never accepted PDFs anywhere — the
   StoreAssetRequest mimes list excluded them, the storeChunked
   extension regex excluded them, MediaAttacher excluded them. The only
   places that referenced Document were:
     - Platform::allowedMediaTypes for LinkedIn/LinkedInPage (declared
       but unreachable)
     - HasMedia::getMediaType fallback when MIME wasn't image/video/*
   Both now cleaned up. HasMedia::getMediaType throws
   InvalidArgumentException for unsupported MIMEs instead of silently
   returning a fake 'document' type. Platform::LinkedIn now matches
   every other social platform: [Image, Video].

3. Add MediaType::fromMime($mime): ?self — replaces the inline mime →
   type loop that MediaAttacher used to roll. Returns null for
   unsupported MIMEs (caller decides how to react).

Tests:
- MediaTypeTest rewritten for the new shape (no Document, config-driven
  sizes, fromMime + size-helper coverage).
- PlatformTest no longer asserts Document on LinkedIn.
- HasMediaTest replaces the 'detects document type' case with one that
  asserts the throw on unsupported MIMEs. The 'add media from path'
  test now uses real PNG bytes from the fixture.
- AssetControllerTest chunked tests use real PNG bytes and assert 422
  (FormRequest unprocessable) for malformed Content-Range headers,
  matching the new validation layer.
2026-05-04 14:54:47 -03:00

110 lines
4.8 KiB
PHP

<?php
declare(strict_types=1);
use App\Enums\Media\Type as MediaType;
use App\Enums\SocialAccount\Platform;
test('platform has correct labels', function () {
expect(Platform::LinkedIn->label())->toBe('LinkedIn');
expect(Platform::LinkedInPage->label())->toBe('LinkedIn Page');
expect(Platform::X->label())->toBe('X');
expect(Platform::TikTok->label())->toBe('TikTok');
expect(Platform::YouTube->label())->toBe('YouTube Shorts');
expect(Platform::Facebook->label())->toBe('Facebook Page');
expect(Platform::Instagram->label())->toBe('Instagram (Standalone)');
expect(Platform::InstagramFacebook->label())->toBe('Instagram (Facebook Business)');
expect(Platform::Threads->label())->toBe('Threads');
expect(Platform::Pinterest->label())->toBe('Pinterest');
expect(Platform::Bluesky->label())->toBe('Bluesky');
expect(Platform::Mastodon->label())->toBe('Mastodon');
});
test('platform has correct colors', function () {
expect(Platform::LinkedIn->color())->toBe('#0A66C2');
expect(Platform::LinkedInPage->color())->toBe('#0A66C2');
expect(Platform::X->color())->toBe('#000000');
expect(Platform::TikTok->color())->toBe('#000000');
expect(Platform::YouTube->color())->toBe('#FF0000');
expect(Platform::Facebook->color())->toBe('#1877F2');
expect(Platform::Instagram->color())->toBe('#E4405F');
expect(Platform::Threads->color())->toBe('#000000');
expect(Platform::Pinterest->color())->toBe('#E60023');
expect(Platform::Bluesky->color())->toBe('#0085FF');
expect(Platform::Mastodon->color())->toBe('#6364FF');
});
test('platform has correct allowed media types', function () {
expect(Platform::LinkedIn->allowedMediaTypes())->toContain(MediaType::Image, MediaType::Video);
expect(Platform::X->allowedMediaTypes())->toContain(MediaType::Image, MediaType::Video);
expect(Platform::TikTok->allowedMediaTypes())->toBe([MediaType::Video]);
expect(Platform::YouTube->allowedMediaTypes())->toBe([MediaType::Video]);
expect(Platform::Instagram->allowedMediaTypes())->toContain(MediaType::Image, MediaType::Video);
});
test('platform has correct max images', function () {
expect(Platform::LinkedIn->maxImages())->toBe(1);
expect(Platform::X->maxImages())->toBe(4);
expect(Platform::TikTok->maxImages())->toBe(0);
expect(Platform::YouTube->maxImages())->toBe(0);
expect(Platform::Facebook->maxImages())->toBe(10);
expect(Platform::Instagram->maxImages())->toBe(10);
expect(Platform::Threads->maxImages())->toBe(10);
expect(Platform::Pinterest->maxImages())->toBe(5);
expect(Platform::Bluesky->maxImages())->toBe(4);
expect(Platform::Mastodon->maxImages())->toBe(4);
});
test('platform has correct max content length', function () {
expect(Platform::LinkedIn->maxContentLength())->toBe(3000);
expect(Platform::X->maxContentLength())->toBe(280);
expect(Platform::TikTok->maxContentLength())->toBe(2200);
expect(Platform::YouTube->maxContentLength())->toBe(100);
expect(Platform::Facebook->maxContentLength())->toBe(63206);
expect(Platform::Instagram->maxContentLength())->toBe(2200);
expect(Platform::Threads->maxContentLength())->toBe(500);
expect(Platform::Pinterest->maxContentLength())->toBe(800);
expect(Platform::Bluesky->maxContentLength())->toBe(300);
expect(Platform::Mastodon->maxContentLength())->toBe(500);
});
test('platform supports text only correctly', function () {
expect(Platform::LinkedIn->supportsTextOnly())->toBeTrue();
expect(Platform::LinkedInPage->supportsTextOnly())->toBeTrue();
expect(Platform::X->supportsTextOnly())->toBeTrue();
expect(Platform::Facebook->supportsTextOnly())->toBeTrue();
expect(Platform::Threads->supportsTextOnly())->toBeTrue();
expect(Platform::Bluesky->supportsTextOnly())->toBeTrue();
expect(Platform::Mastodon->supportsTextOnly())->toBeTrue();
expect(Platform::TikTok->supportsTextOnly())->toBeFalse();
expect(Platform::YouTube->supportsTextOnly())->toBeFalse();
expect(Platform::Instagram->supportsTextOnly())->toBeFalse();
expect(Platform::Pinterest->supportsTextOnly())->toBeFalse();
});
test('platform is enabled by default', function () {
expect(Platform::LinkedIn->isEnabled())->toBeTrue();
expect(Platform::Instagram->isEnabled())->toBeTrue();
});
test('platform can be disabled via config', function () {
config(['trypost.platforms.linkedin.enabled' => false]);
expect(Platform::LinkedIn->isEnabled())->toBeFalse();
});
test('can get all enabled platforms', function () {
$enabled = Platform::enabled();
expect($enabled)->toBeArray();
expect(count($enabled))->toBeGreaterThan(0);
});
test('disabled platforms are excluded from enabled list', function () {
config(['trypost.platforms.linkedin.enabled' => false]);
$enabled = Platform::enabled();
expect($enabled)->not->toContain(Platform::LinkedIn);
});