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