StreamDecoratorTraitTest.php 3.8 KB

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