Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MessageConverterTest.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Mime\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Mime\Email;
  13. use Symfony\Component\Mime\Message;
  14. use Symfony\Component\Mime\MessageConverter;
  15. class MessageConverterTest extends TestCase
  16. {
  17. public function testToEmail()
  18. {
  19. $file = file_get_contents(__DIR__.'/Fixtures/mimetypes/test.gif');
  20. $email = (new Email())->from('fabien@symfony.com');
  21. $this->assertSame($email, MessageConverter::toEmail($email));
  22. $this->assertConversion((clone $email)->text('text content'));
  23. $this->assertConversion((clone $email)->html('HTML content <img src="cid:test.jpg" />'));
  24. $this->assertConversion((clone $email)
  25. ->text('text content')
  26. ->html('HTML content <img src="cid:test.jpg" />')
  27. );
  28. $this->assertConversion((clone $email)
  29. ->text('text content')
  30. ->html('HTML content <img src="cid:test.jpg" />')
  31. ->embed($file, 'test.jpg', 'image/gif')
  32. );
  33. $this->assertConversion((clone $email)
  34. ->text('text content')
  35. ->html('HTML content <img src="cid:test.jpg" />')
  36. ->attach($file, 'test_attached.jpg', 'image/gif')
  37. );
  38. $this->assertConversion((clone $email)
  39. ->text('text content')
  40. ->html('HTML content <img src="cid:test.jpg" />')
  41. ->embed($file, 'test.jpg', 'image/gif')
  42. ->attach($file, 'test_attached.jpg', 'image/gif')
  43. );
  44. $this->assertConversion((clone $email)
  45. ->text('text content')
  46. ->attach($file, 'test_attached.jpg', 'image/gif')
  47. );
  48. $this->assertConversion((clone $email)
  49. ->html('HTML content <img src="cid:test.jpg" />')
  50. ->attach($file, 'test_attached.jpg', 'image/gif')
  51. );
  52. $this->assertConversion((clone $email)
  53. ->html('HTML content <img src="cid:test.jpg" />')
  54. ->embed($file, 'test.jpg', 'image/gif')
  55. );
  56. $this->assertConversion((clone $email)
  57. ->text('text content')
  58. ->embed($file, 'test_attached.jpg', 'image/gif')
  59. );
  60. }
  61. private function assertConversion(Email $expected)
  62. {
  63. $r = new \ReflectionMethod($expected, 'generateBody');
  64. $r->setAccessible(true);
  65. $message = new Message($expected->getHeaders(), $r->invoke($expected));
  66. $converted = MessageConverter::toEmail($message);
  67. if ($expected->getHtmlBody()) {
  68. $this->assertStringMatchesFormat(str_replace('cid:test.jpg', 'cid:%s', $expected->getHtmlBody()), $converted->getHtmlBody());
  69. $expected->html('HTML content');
  70. $converted->html('HTML content');
  71. }
  72. $this->assertEquals($expected, $converted);
  73. }
  74. }