StreamDecoratorTraitTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. declare(strict_types=1);
  3. namespace GuzzleHttp\Tests\Psr7;
  4. use GuzzleHttp\Psr7;
  5. use GuzzleHttp\Psr7\StreamDecoratorTrait;
  6. use PHPUnit\Framework\TestCase;
  7. use Psr\Http\Message\StreamInterface;
  8. class Str implements StreamInterface
  9. {
  10. use StreamDecoratorTrait;
  11. /** @var StreamInterface */
  12. private $stream;
  13. }
  14. /**
  15. * @covers \GuzzleHttp\Psr7\StreamDecoratorTrait
  16. */
  17. class StreamDecoratorTraitTest extends TestCase
  18. {
  19. /** @var StreamInterface */
  20. private $a;
  21. /** @var StreamInterface */
  22. private $b;
  23. /** @var resource */
  24. private $c;
  25. protected function setUp(): void
  26. {
  27. $this->c = fopen('php://temp', 'r+');
  28. fwrite($this->c, 'foo');
  29. fseek($this->c, 0);
  30. $this->a = Psr7\Utils::streamFor($this->c);
  31. $this->b = new Str($this->a);
  32. }
  33. /**
  34. * @requires PHP < 7.4
  35. */
  36. public function testCatchesExceptionsWhenCastingToString(): void
  37. {
  38. $s = $this->createMock(Str::class);
  39. $s->expects(self::once())
  40. ->method('read')
  41. ->willThrowException(new \RuntimeException('foo'));
  42. $msg = '';
  43. set_error_handler(function (int $errNo, string $str) use (&$msg): void {
  44. $msg = $str;
  45. });
  46. echo new Str($s);
  47. restore_error_handler();
  48. self::assertStringContainsString('foo', $msg);
  49. }
  50. public function testToString(): void
  51. {
  52. self::assertSame('foo', (string) $this->b);
  53. }
  54. public function testHasSize(): void
  55. {
  56. self::assertSame(3, $this->b->getSize());
  57. }
  58. public function testReads(): void
  59. {
  60. self::assertSame('foo', $this->b->read(10));
  61. }
  62. public function testCheckMethods(): void
  63. {
  64. self::assertSame($this->a->isReadable(), $this->b->isReadable());
  65. self::assertSame($this->a->isWritable(), $this->b->isWritable());
  66. self::assertSame($this->a->isSeekable(), $this->b->isSeekable());
  67. }
  68. public function testSeeksAndTells(): void
  69. {
  70. $this->b->seek(1);
  71. self::assertSame(1, $this->a->tell());
  72. self::assertSame(1, $this->b->tell());
  73. $this->b->seek(0);
  74. self::assertSame(0, $this->a->tell());
  75. self::assertSame(0, $this->b->tell());
  76. $this->b->seek(0, SEEK_END);
  77. self::assertSame(3, $this->a->tell());
  78. self::assertSame(3, $this->b->tell());
  79. }
  80. public function testGetsContents(): void
  81. {
  82. self::assertSame('foo', $this->b->getContents());
  83. self::assertSame('', $this->b->getContents());
  84. $this->b->seek(1);
  85. self::assertSame('oo', $this->b->getContents());
  86. }
  87. public function testCloses(): void
  88. {
  89. $this->b->close();
  90. self::assertFalse(is_resource($this->c));
  91. }
  92. public function testDetaches(): void
  93. {
  94. $this->b->detach();
  95. self::assertFalse($this->b->isReadable());
  96. }
  97. public function testWrapsMetadata(): void
  98. {
  99. self::assertSame($this->b->getMetadata(), $this->a->getMetadata());
  100. self::assertSame($this->b->getMetadata('uri'), $this->a->getMetadata('uri'));
  101. }
  102. public function testWrapsWrites(): void
  103. {
  104. $this->b->seek(0, SEEK_END);
  105. $this->b->write('foo');
  106. self::assertSame('foofoo', (string) $this->a);
  107. }
  108. public function testThrowsWithInvalidGetter(): void
  109. {
  110. $this->expectException(\UnexpectedValueException::class);
  111. $this->b->foo;
  112. }
  113. public function testThrowsWhenGetterNotImplemented(): void
  114. {
  115. $this->expectException(\BadMethodCallException::class);
  116. $s = new BadStream();
  117. $s->stream;
  118. }
  119. }
  120. class BadStream
  121. {
  122. use StreamDecoratorTrait;
  123. public function __construct()
  124. {
  125. }
  126. }