StreamDecoratorTraitTest.php 3.5 KB

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