1<?php
2namespace GuzzleHttp\Tests\Stream;
3
4use GuzzleHttp\Stream\BufferStream;
5
6class BufferStreamTest extends \PHPUnit_Framework_TestCase
7{
8    public function testHasMetadata()
9    {
10        $b = new BufferStream(10);
11        $this->assertTrue($b->isReadable());
12        $this->assertTrue($b->isWritable());
13        $this->assertFalse($b->isSeekable());
14        $this->assertEquals(null, $b->getMetadata('foo'));
15        $this->assertEquals(10, $b->getMetadata('hwm'));
16        $this->assertEquals([], $b->getMetadata());
17    }
18
19    public function testRemovesReadDataFromBuffer()
20    {
21        $b = new BufferStream();
22        $this->assertEquals(3, $b->write('foo'));
23        $this->assertEquals(3, $b->getSize());
24        $this->assertFalse($b->eof());
25        $this->assertEquals('foo', $b->read(10));
26        $this->assertTrue($b->eof());
27        $this->assertEquals('', $b->read(10));
28    }
29
30    public function testCanCastToStringOrGetContents()
31    {
32        $b = new BufferStream();
33        $b->write('foo');
34        $b->write('baz');
35        $this->assertEquals('foo', $b->read(3));
36        $b->write('bar');
37        $this->assertEquals('bazbar', (string) $b);
38        $this->assertFalse($b->tell());
39    }
40
41    public function testDetachClearsBuffer()
42    {
43        $b = new BufferStream();
44        $b->write('foo');
45        $b->detach();
46        $this->assertEquals(0, $b->tell());
47        $this->assertTrue($b->eof());
48        $this->assertEquals(3, $b->write('abc'));
49        $this->assertEquals('abc', $b->read(10));
50    }
51
52    public function testExceedingHighwaterMarkReturnsFalseButStillBuffers()
53    {
54        $b = new BufferStream(5);
55        $this->assertEquals(3, $b->write('hi '));
56        $this->assertFalse($b->write('hello'));
57        $this->assertEquals('hi hello', (string) $b);
58        $this->assertEquals(4, $b->write('test'));
59    }
60
61    /**
62     * @expectedException \GuzzleHttp\Stream\Exception\CannotAttachException
63     */
64    public function testCannotAttach()
65    {
66        $p = new BufferStream();
67        $p->attach('a');
68    }
69}
70