2
namespace GuzzleHttp\Tests\Stream;
4
use GuzzleHttp\Stream\StreamInterface;
5
use GuzzleHttp\Stream\Stream;
6
use GuzzleHttp\Stream\StreamDecoratorTrait;
8
class Str implements StreamInterface
10
use StreamDecoratorTrait;
14
* @covers GuzzleHttp\Stream\StreamDecoratorTrait
16
class StreamDecoratorTraitTest extends \PHPUnit_Framework_TestCase
22
public function setUp()
24
$this->c = fopen('php://temp', 'r+');
25
fwrite($this->c, 'foo');
27
$this->a = Stream::factory($this->c);
28
$this->b = new Str($this->a);
31
public function testCatchesExceptionsWhenCastingToString()
33
$s = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface')
34
->setMethods(['read'])
35
->getMockForAbstractClass();
36
$s->expects($this->once())
38
->will($this->throwException(new \Exception('foo')));
40
set_error_handler(function ($errNo, $str) use (&$msg) { $msg = $str; });
42
restore_error_handler();
43
$this->assertContains('foo', $msg);
46
public function testToString()
48
$this->assertEquals('foo', (string) $this->b);
51
public function testHasSize()
53
$this->assertEquals(3, $this->b->getSize());
54
$this->assertSame($this->b, $this->b->setSize(2));
55
$this->assertEquals(2, $this->b->getSize());
58
public function testReads()
60
$this->assertEquals('foo', $this->b->read(10));
63
public function testCheckMethods()
65
$this->assertEquals($this->a->isReadable(), $this->b->isReadable());
66
$this->assertEquals($this->a->isWritable(), $this->b->isWritable());
67
$this->assertEquals($this->a->isSeekable(), $this->b->isSeekable());
70
public function testSeeksAndTells()
72
$this->assertTrue($this->b->seek(1));
73
$this->assertEquals(1, $this->a->tell());
74
$this->assertEquals(1, $this->b->tell());
75
$this->assertTrue($this->b->seek(0));
76
$this->assertEquals(0, $this->a->tell());
77
$this->assertEquals(0, $this->b->tell());
78
$this->assertTrue($this->b->seek(0, SEEK_END));
79
$this->assertEquals(3, $this->a->tell());
80
$this->assertEquals(3, $this->b->tell());
83
public function testGetsContents()
85
$this->assertEquals('foo', $this->b->getContents());
86
$this->assertEquals('', $this->b->getContents());
88
$this->assertEquals('o', $this->b->getContents(1));
89
$this->assertEquals('', $this->b->getContents(0));
92
public function testCloses()
95
$this->assertFalse(is_resource($this->c));
98
public function testDetaches()
101
$this->assertFalse($this->b->isReadable());
104
public function testWrapsMetadata()
106
$this->assertSame($this->b->getMetadata(), $this->a->getMetadata());
107
$this->assertSame($this->b->getMetadata('uri'), $this->a->getMetadata('uri'));
110
public function testWrapsWrites()
112
$this->b->seek(0, SEEK_END);
113
$this->b->write('foo');
114
$this->assertEquals('foofoo', (string) $this->a);
117
public function testWrapsFlush()
123
* @expectedException \UnexpectedValueException
125
public function testThrowsWithInvalidGetter()
131
* @expectedException \BadMethodCallException
133
public function testThrowsWhenGetterNotImplemented()
135
$s = new BadStream();
142
use StreamDecoratorTrait;
144
public function __construct() {}