diff --git a/src/Attachment.php b/src/Attachment.php index 06b5fc8..ea9952c 100644 --- a/src/Attachment.php +++ b/src/Attachment.php @@ -117,6 +117,10 @@ public function contentDisposition(): string */ public function contents(): string { + if ($this->contentStream->isSeekable()) { + $this->contentStream->rewind(); + } + return $this->contentStream->getContents(); } diff --git a/tests/Unit/AttachmentTest.php b/tests/Unit/AttachmentTest.php index 247babf..79e61a7 100644 --- a/tests/Unit/AttachmentTest.php +++ b/tests/Unit/AttachmentTest.php @@ -1,10 +1,10 @@ extension(); @@ -12,9 +12,33 @@ }); test('extension with content type', function () { - $stream = new LazyOpenStream('test', 'r'); + $stream = Utils::streamFor(''); $ext = (new Attachment('test', null, 'image/jpeg', 'attachment', $stream))->extension(); expect($ext)->toBe('jpg'); }); + +test('contents can be read multiple times', function () { + $stream = Utils::streamFor('Hello World!'); + + $attachment = new Attachment('test.txt', null, 'text/plain', 'attachment', $stream); + + expect($attachment->contents())->toBe('Hello World!'); + expect($attachment->contents())->toBe('Hello World!'); +}); + +test('save writes contents after reading contents', function () { + $stream = Utils::streamFor('Hello World!'); + + $attachment = new Attachment('test.txt', null, 'text/plain', 'attachment', $stream); + + $path = tempnam(sys_get_temp_dir(), 'imap-engine-attachment-'); + + $attachment->contents(); + $attachment->save($path); + + expect(file_get_contents($path))->toBe('Hello World!'); + + unlink($path); +});