Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ public function contentDisposition(): string
*/
public function contents(): string
{
if ($this->contentStream->isSeekable()) {
$this->contentStream->rewind();
}

return $this->contentStream->getContents();
}

Expand Down
30 changes: 27 additions & 3 deletions tests/Unit/AttachmentTest.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
<?php

use DirectoryTree\ImapEngine\Attachment;
use GuzzleHttp\Psr7\LazyOpenStream;
use GuzzleHttp\Psr7\Utils;

test('extension', function () {
$stream = new LazyOpenStream('test.jpg', 'r');
$stream = Utils::streamFor('');

$ext = (new Attachment('test.jpg', null, 'image/jpeg', 'attachment', $stream))->extension();

expect($ext)->toBe('jpg');
});

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);
});
Loading