scratch – Blame information for rev 87

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 1 <?php
2 namespace GuzzleHttp\Tests\Stream;
3  
4 use GuzzleHttp\Stream\StreamInterface;
5 use GuzzleHttp\Stream\Stream;
6 use GuzzleHttp\Stream\StreamDecoratorTrait;
7  
8 class Str implements StreamInterface
9 {
10 use StreamDecoratorTrait;
11 }
12  
13 /**
14 * @covers GuzzleHttp\Stream\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 = Stream::factory($this->c);
28 $this->b = new Str($this->a);
29 }
30  
31 public function testCatchesExceptionsWhenCastingToString()
32 {
33 $s = $this->getMockBuilder('GuzzleHttp\Stream\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 $this->assertSame($this->b, $this->b->setSize(2));
55 $this->assertEquals(2, $this->b->getSize());
56 }
57  
58 public function testReads()
59 {
60 $this->assertEquals('foo', $this->b->read(10));
61 }
62  
63 public function testCheckMethods()
64 {
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());
68 }
69  
70 public function testSeeksAndTells()
71 {
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());
81 }
82  
83 public function testGetsContents()
84 {
85 $this->assertEquals('foo', $this->b->getContents());
86 $this->assertEquals('', $this->b->getContents());
87 $this->b->seek(1);
88 $this->assertEquals('o', $this->b->getContents(1));
89 $this->assertEquals('', $this->b->getContents(0));
90 }
91  
92 public function testCloses()
93 {
94 $this->b->close();
95 $this->assertFalse(is_resource($this->c));
96 }
97  
98 public function testDetaches()
99 {
100 $this->b->detach();
101 $this->assertFalse($this->b->isReadable());
102 }
103  
104 public function testWrapsMetadata()
105 {
106 $this->assertSame($this->b->getMetadata(), $this->a->getMetadata());
107 $this->assertSame($this->b->getMetadata('uri'), $this->a->getMetadata('uri'));
108 }
109  
110 public function testWrapsWrites()
111 {
112 $this->b->seek(0, SEEK_END);
113 $this->b->write('foo');
114 $this->assertEquals('foofoo', (string) $this->a);
115 }
116  
117 public function testWrapsFlush()
118 {
119 $this->b->flush();
120 }
121  
122 /**
123 * @expectedException \UnexpectedValueException
124 */
125 public function testThrowsWithInvalidGetter()
126 {
127 $this->b->foo;
128 }
129  
130 /**
131 * @expectedException \BadMethodCallException
132 */
133 public function testThrowsWhenGetterNotImplemented()
134 {
135 $s = new BadStream();
136 $s->stream;
137 }
138 }
139  
140 class BadStream
141 {
142 use StreamDecoratorTrait;
143  
144 public function __construct() {}
145 }