MailMessageTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Illuminate\Tests\Mail;
  3. use Illuminate\Mail\Message;
  4. use Mockery as m;
  5. use PHPUnit\Framework\TestCase;
  6. use stdClass;
  7. use Swift_Mime_Message;
  8. class MailMessageTest extends TestCase
  9. {
  10. /**
  11. * @var \Mockery::mock
  12. */
  13. protected $swift;
  14. /**
  15. * @var \Illuminate\Mail\Message
  16. */
  17. protected $message;
  18. protected function setUp(): void
  19. {
  20. parent::setUp();
  21. $this->swift = m::mock(Swift_Mime_Message::class);
  22. $this->message = new Message($this->swift);
  23. }
  24. protected function tearDown(): void
  25. {
  26. m::close();
  27. }
  28. public function testFromMethod()
  29. {
  30. $this->swift->shouldReceive('setFrom')->once()->with('foo@bar.baz', 'Foo');
  31. $this->assertInstanceOf(Message::class, $this->message->from('foo@bar.baz', 'Foo'));
  32. }
  33. public function testSenderMethod()
  34. {
  35. $this->swift->shouldReceive('setSender')->once()->with('foo@bar.baz', 'Foo');
  36. $this->assertInstanceOf(Message::class, $this->message->sender('foo@bar.baz', 'Foo'));
  37. }
  38. public function testReturnPathMethod()
  39. {
  40. $this->swift->shouldReceive('setReturnPath')->once()->with('foo@bar.baz');
  41. $this->assertInstanceOf(Message::class, $this->message->returnPath('foo@bar.baz'));
  42. }
  43. public function testToMethod()
  44. {
  45. $this->swift->shouldReceive('addTo')->once()->with('foo@bar.baz', 'Foo');
  46. $this->assertInstanceOf(Message::class, $this->message->to('foo@bar.baz', 'Foo', false));
  47. }
  48. public function testToMethodWithOverride()
  49. {
  50. $this->swift->shouldReceive('setTo')->once()->with('foo@bar.baz', 'Foo');
  51. $this->assertInstanceOf(Message::class, $this->message->to('foo@bar.baz', 'Foo', true));
  52. }
  53. public function testCcMethod()
  54. {
  55. $this->swift->shouldReceive('addCc')->once()->with('foo@bar.baz', 'Foo');
  56. $this->assertInstanceOf(Message::class, $this->message->cc('foo@bar.baz', 'Foo'));
  57. }
  58. public function testBccMethod()
  59. {
  60. $this->swift->shouldReceive('addBcc')->once()->with('foo@bar.baz', 'Foo');
  61. $this->assertInstanceOf(Message::class, $this->message->bcc('foo@bar.baz', 'Foo'));
  62. }
  63. public function testReplyToMethod()
  64. {
  65. $this->swift->shouldReceive('addReplyTo')->once()->with('foo@bar.baz', 'Foo');
  66. $this->assertInstanceOf(Message::class, $this->message->replyTo('foo@bar.baz', 'Foo'));
  67. }
  68. public function testSubjectMethod()
  69. {
  70. $this->swift->shouldReceive('setSubject')->once()->with('foo');
  71. $this->assertInstanceOf(Message::class, $this->message->subject('foo'));
  72. }
  73. public function testPriorityMethod()
  74. {
  75. $this->swift->shouldReceive('setPriority')->once()->with(1);
  76. $this->assertInstanceOf(Message::class, $this->message->priority(1));
  77. }
  78. public function testGetSwiftMessageMethod()
  79. {
  80. $this->assertInstanceOf(Swift_Mime_Message::class, $this->message->getSwiftMessage());
  81. }
  82. public function testBasicAttachment()
  83. {
  84. $swift = m::mock(stdClass::class);
  85. $message = $this->getMockBuilder(Message::class)->onlyMethods(['createAttachmentFromPath'])->setConstructorArgs([$swift])->getMock();
  86. $attachment = m::mock(stdClass::class);
  87. $message->expects($this->once())->method('createAttachmentFromPath')->with($this->equalTo('foo.jpg'))->willReturn($attachment);
  88. $swift->shouldReceive('attach')->once()->with($attachment);
  89. $attachment->shouldReceive('setContentType')->once()->with('image/jpeg');
  90. $attachment->shouldReceive('setFilename')->once()->with('bar.jpg');
  91. $message->attach('foo.jpg', ['mime' => 'image/jpeg', 'as' => 'bar.jpg']);
  92. }
  93. public function testDataAttachment()
  94. {
  95. $swift = m::mock(stdClass::class);
  96. $message = $this->getMockBuilder(Message::class)->onlyMethods(['createAttachmentFromData'])->setConstructorArgs([$swift])->getMock();
  97. $attachment = m::mock(stdClass::class);
  98. $message->expects($this->once())->method('createAttachmentFromData')->with($this->equalTo('foo'), $this->equalTo('name'))->willReturn($attachment);
  99. $swift->shouldReceive('attach')->once()->with($attachment);
  100. $attachment->shouldReceive('setContentType')->once()->with('image/jpeg');
  101. $message->attachData('foo', 'name', ['mime' => 'image/jpeg']);
  102. }
  103. }