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.

TextPartTest.php 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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\Part;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Mime\Header\Headers;
  13. use Symfony\Component\Mime\Header\ParameterizedHeader;
  14. use Symfony\Component\Mime\Header\UnstructuredHeader;
  15. use Symfony\Component\Mime\Part\TextPart;
  16. class TextPartTest extends TestCase
  17. {
  18. public function testConstructor()
  19. {
  20. $p = new TextPart('content');
  21. $this->assertEquals('content', $p->getBody());
  22. $this->assertEquals('content', $p->bodyToString());
  23. $this->assertEquals('content', implode('', iterator_to_array($p->bodyToIterable())));
  24. // bodyToIterable() can be called several times
  25. $this->assertEquals('content', implode('', iterator_to_array($p->bodyToIterable())));
  26. $this->assertEquals('text', $p->getMediaType());
  27. $this->assertEquals('plain', $p->getMediaSubType());
  28. $p = new TextPart('content', null, 'html');
  29. $this->assertEquals('html', $p->getMediaSubType());
  30. }
  31. public function testConstructorWithResource()
  32. {
  33. $f = fopen('php://memory', 'r+', false);
  34. fwrite($f, 'content');
  35. rewind($f);
  36. $p = new TextPart($f);
  37. $this->assertEquals('content', $p->getBody());
  38. $this->assertEquals('content', $p->bodyToString());
  39. $this->assertEquals('content', implode('', iterator_to_array($p->bodyToIterable())));
  40. fclose($f);
  41. }
  42. public function testConstructorWithNonStringOrResource()
  43. {
  44. $this->expectException(\TypeError::class);
  45. new TextPart(new \stdClass());
  46. }
  47. public function testHeaders()
  48. {
  49. $p = new TextPart('content');
  50. $this->assertEquals(new Headers(
  51. new ParameterizedHeader('Content-Type', 'text/plain', ['charset' => 'utf-8']),
  52. new UnstructuredHeader('Content-Transfer-Encoding', 'quoted-printable')
  53. ), $p->getPreparedHeaders());
  54. $p = new TextPart('content', 'iso-8859-1');
  55. $this->assertEquals(new Headers(
  56. new ParameterizedHeader('Content-Type', 'text/plain', ['charset' => 'iso-8859-1']),
  57. new UnstructuredHeader('Content-Transfer-Encoding', 'quoted-printable')
  58. ), $p->getPreparedHeaders());
  59. }
  60. public function testEncoding()
  61. {
  62. $p = new TextPart('content', 'utf-8', 'plain', 'base64');
  63. $this->assertEquals(base64_encode('content'), $p->bodyToString());
  64. $this->assertEquals(base64_encode('content'), implode('', iterator_to_array($p->bodyToIterable())));
  65. $this->assertEquals(new Headers(
  66. new ParameterizedHeader('Content-Type', 'text/plain', ['charset' => 'utf-8']),
  67. new UnstructuredHeader('Content-Transfer-Encoding', 'base64')
  68. ), $p->getPreparedHeaders());
  69. }
  70. public function testSerialize()
  71. {
  72. $r = fopen('php://memory', 'r+', false);
  73. fwrite($r, 'Text content');
  74. rewind($r);
  75. $p = new TextPart($r);
  76. $p->getHeaders()->addTextHeader('foo', 'bar');
  77. $expected = clone $p;
  78. $this->assertEquals($expected->toString(), unserialize(serialize($p))->toString());
  79. }
  80. }