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.

CharacterStreamTest.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\CharacterStream;
  13. class CharacterStreamTest extends TestCase
  14. {
  15. public function testReadCharactersAreInTact()
  16. {
  17. $stream = new CharacterStream(pack('C*', 0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE));
  18. $stream->write(pack('C*',
  19. 0xD0, 0xBB,
  20. 0xD1, 0x8E,
  21. 0xD0, 0xB1,
  22. 0xD1, 0x8B,
  23. 0xD1, 0x85
  24. ));
  25. $this->assertSame(pack('C*', 0xD0, 0x94), $stream->read(1));
  26. $this->assertSame(pack('C*', 0xD0, 0xB6, 0xD0, 0xBE), $stream->read(2));
  27. $this->assertSame(pack('C*', 0xD0, 0xBB), $stream->read(1));
  28. $this->assertSame(pack('C*', 0xD1, 0x8E, 0xD0, 0xB1, 0xD1, 0x8B), $stream->read(3));
  29. $this->assertSame(pack('C*', 0xD1, 0x85), $stream->read(1));
  30. $this->assertNull($stream->read(1));
  31. }
  32. public function testCharactersCanBeReadAsByteArrays()
  33. {
  34. $stream = new CharacterStream(pack('C*', 0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE));
  35. $stream->write(pack('C*',
  36. 0xD0, 0xBB,
  37. 0xD1, 0x8E,
  38. 0xD0, 0xB1,
  39. 0xD1, 0x8B,
  40. 0xD1, 0x85
  41. ));
  42. $this->assertEquals([0xD0, 0x94], $stream->readBytes(1));
  43. $this->assertEquals([0xD0, 0xB6, 0xD0, 0xBE], $stream->readBytes(2));
  44. $this->assertEquals([0xD0, 0xBB], $stream->readBytes(1));
  45. $this->assertEquals([0xD1, 0x8E, 0xD0, 0xB1, 0xD1, 0x8B], $stream->readBytes(3));
  46. $this->assertEquals([0xD1, 0x85], $stream->readBytes(1));
  47. $this->assertNull($stream->readBytes(1));
  48. }
  49. public function testRequestingLargeCharCountPastEndOfStream()
  50. {
  51. $stream = new CharacterStream(pack('C*', 0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE));
  52. $this->assertSame(pack('C*', 0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE), $stream->read(100));
  53. $this->assertNull($stream->read(1));
  54. }
  55. public function testRequestingByteArrayCountPastEndOfStream()
  56. {
  57. $stream = new CharacterStream(pack('C*', 0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE));
  58. $this->assertEquals([0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE], $stream->readBytes(100));
  59. $this->assertNull($stream->readBytes(1));
  60. }
  61. public function testPointerOffsetCanBeSet()
  62. {
  63. $stream = new CharacterStream(pack('C*', 0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE));
  64. $this->assertSame(pack('C*', 0xD0, 0x94), $stream->read(1));
  65. $stream->setPointer(0);
  66. $this->assertSame(pack('C*', 0xD0, 0x94), $stream->read(1));
  67. $stream->setPointer(2);
  68. $this->assertSame(pack('C*', 0xD0, 0xBE), $stream->read(1));
  69. }
  70. public function testAlgorithmWithFixedWidthCharsets()
  71. {
  72. $stream = new CharacterStream(pack('C*', 0xD1, 0x8D, 0xD0, 0xBB, 0xD0, 0xB0));
  73. $this->assertSame(pack('C*', 0xD1, 0x8D), $stream->read(1));
  74. $this->assertSame(pack('C*', 0xD0, 0xBB), $stream->read(1));
  75. $this->assertSame(pack('C*', 0xD0, 0xB0), $stream->read(1));
  76. $this->assertNull($stream->read(1));
  77. }
  78. }