Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
34 / 34 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
||
| 1 | <?php |
| 2 | |
| 3 | // ╔════════════════════════════════════════════════════════════╗ |
| 4 | // ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║ |
| 5 | // ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com> ║ |
| 6 | // ╚════════════════════════════════════════════════════════════╝ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | pest()->group('unit'); |
| 11 | |
| 12 | use Phexium\Plugin\Mailer\Attachment; |
| 13 | |
| 14 | describe('Creation', function (): void { |
| 15 | it('creates from path with auto-detected filename', function (): void { |
| 16 | $path = __FILE__; |
| 17 | $attachment = Attachment::fromPath($path); |
| 18 | |
| 19 | expect($attachment->getPath())->toBe($path); |
| 20 | expect($attachment->getFilename())->toBe('AttachmentTest.php'); |
| 21 | expect($attachment->getContent())->toBeNull(); |
| 22 | expect($attachment->getMimeType())->toBeNull(); |
| 23 | expect($attachment->isFile())->toBeTrue(); |
| 24 | }); |
| 25 | |
| 26 | it('creates from path with custom filename and mime type', function (): void { |
| 27 | $path = __FILE__; |
| 28 | $attachment = Attachment::fromPath($path, 'custom.txt', 'text/plain'); |
| 29 | |
| 30 | expect($attachment->getFilename())->toBe('custom.txt'); |
| 31 | expect($attachment->getMimeType())->toBe('text/plain'); |
| 32 | expect($attachment->isFile())->toBeTrue(); |
| 33 | }); |
| 34 | |
| 35 | it('creates from content', function (): void { |
| 36 | $attachment = Attachment::fromContent('binary-data', 'report.pdf', 'application/pdf'); |
| 37 | |
| 38 | expect($attachment->getContent())->toBe('binary-data'); |
| 39 | expect($attachment->getFilename())->toBe('report.pdf'); |
| 40 | expect($attachment->getMimeType())->toBe('application/pdf'); |
| 41 | expect($attachment->getPath())->toBeNull(); |
| 42 | expect($attachment->isFile())->toBeFalse(); |
| 43 | }); |
| 44 | }); |
| 45 | |
| 46 | describe('Validation', function (): void { |
| 47 | it('rejects non-existent file', function (): void { |
| 48 | expect(fn (): Attachment => Attachment::fromPath('/non/existent/file.txt')) |
| 49 | ->toThrow(InvalidArgumentException::class) |
| 50 | ; |
| 51 | }); |
| 52 | }); |