|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace AcMailerTest\Attachment\Parser; |
|
5
|
|
|
|
|
6
|
|
|
use AcMailer\Attachment\Parser\FilePathAttachmentParser; |
|
7
|
|
|
use AcMailer\Exception\InvalidAttachmentException; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
10
|
|
|
use Zend\Mime\Mime; |
|
11
|
|
|
|
|
12
|
|
|
class FilePathAttachmentParserTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var FilePathAttachmentParser |
|
16
|
|
|
*/ |
|
17
|
|
|
private $parser; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var ObjectProphecy |
|
20
|
|
|
*/ |
|
21
|
|
|
private $finfo; |
|
22
|
|
|
|
|
23
|
|
|
public function setUp() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->finfo = $this->prophesize(\finfo::class); |
|
26
|
|
|
$this->parser = new FilePathAttachmentParser($this->finfo->reveal()); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @test |
|
31
|
|
|
*/ |
|
32
|
|
|
public function exceptionIsThrownIfAttachmentHasInvalidType() |
|
33
|
|
|
{ |
|
34
|
|
|
$this->expectException(InvalidAttachmentException::class); |
|
35
|
|
|
$this->expectExceptionMessage('Provided attachment is not valid. Expected "file path"'); |
|
36
|
|
|
$this->parser->parse(''); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string|null $attachmentName |
|
41
|
|
|
* @test |
|
42
|
|
|
* @dataProvider provideAttachmentNames |
|
43
|
|
|
*/ |
|
44
|
|
|
public function providedAttachmentIsParsedIntoPart(string $attachmentName = null) |
|
45
|
|
|
{ |
|
46
|
|
|
$attachment = __DIR__ . '/../../attachments/file1'; |
|
47
|
|
|
|
|
48
|
|
|
$getMimeType = $this->finfo->file($attachment)->willReturn('text/plain'); |
|
49
|
|
|
|
|
50
|
|
|
$part = $this->parser->parse($attachment, $attachmentName); |
|
51
|
|
|
|
|
52
|
|
|
$this->assertEquals($part->type, 'text/plain'); |
|
53
|
|
|
$this->assertEquals($part->id, $attachmentName ?? \basename($attachment)); |
|
54
|
|
|
$this->assertEquals($part->filename, $attachmentName ?? \basename($attachment)); |
|
55
|
|
|
$this->assertEquals($part->encoding, Mime::ENCODING_BASE64); |
|
56
|
|
|
$this->assertEquals($part->disposition, Mime::DISPOSITION_ATTACHMENT); |
|
57
|
|
|
$getMimeType->shouldHaveBeenCalled(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function provideAttachmentNames(): array |
|
61
|
|
|
{ |
|
62
|
|
|
return [ |
|
63
|
|
|
[null], |
|
64
|
|
|
['the_name'], |
|
65
|
|
|
]; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|