source: externals/php/vendor/guzzlehttp/psr7/tests/StreamTest.php@ b38cc03

Last change on this file since b38cc03 was 01a620f, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago

Removed submodules for php externals and use composer to provide them and versioned them afterwards

  • Property mode set to 100644
File size: 5.0 KB
Line 
1<?php
2namespace GuzzleHttp\Tests\Psr7;
3
4use GuzzleHttp\Psr7\NoSeekStream;
5use GuzzleHttp\Psr7\Stream;
6
7/**
8 * @covers GuzzleHttp\Psr7\Stream
9 */
10class StreamTest extends \PHPUnit_Framework_TestCase
11{
12 /**
13 * @expectedException \InvalidArgumentException
14 */
15 public function testConstructorThrowsExceptionOnInvalidArgument()
16 {
17 new Stream(true);
18 }
19
20 public function testConstructorInitializesProperties()
21 {
22 $handle = fopen('php://temp', 'r+');
23 fwrite($handle, 'data');
24 $stream = new Stream($handle);
25 $this->assertTrue($stream->isReadable());
26 $this->assertTrue($stream->isWritable());
27 $this->assertTrue($stream->isSeekable());
28 $this->assertEquals('php://temp', $stream->getMetadata('uri'));
29 $this->assertInternalType('array', $stream->getMetadata());
30 $this->assertEquals(4, $stream->getSize());
31 $this->assertFalse($stream->eof());
32 $stream->close();
33 }
34
35 public function testStreamClosesHandleOnDestruct()
36 {
37 $handle = fopen('php://temp', 'r');
38 $stream = new Stream($handle);
39 unset($stream);
40 $this->assertFalse(is_resource($handle));
41 }
42
43 public function testConvertsToString()
44 {
45 $handle = fopen('php://temp', 'w+');
46 fwrite($handle, 'data');
47 $stream = new Stream($handle);
48 $this->assertEquals('data', (string) $stream);
49 $this->assertEquals('data', (string) $stream);
50 $stream->close();
51 }
52
53 public function testGetsContents()
54 {
55 $handle = fopen('php://temp', 'w+');
56 fwrite($handle, 'data');
57 $stream = new Stream($handle);
58 $this->assertEquals('', $stream->getContents());
59 $stream->seek(0);
60 $this->assertEquals('data', $stream->getContents());
61 $this->assertEquals('', $stream->getContents());
62 }
63
64 public function testChecksEof()
65 {
66 $handle = fopen('php://temp', 'w+');
67 fwrite($handle, 'data');
68 $stream = new Stream($handle);
69 $this->assertFalse($stream->eof());
70 $stream->read(4);
71 $this->assertTrue($stream->eof());
72 $stream->close();
73 }
74
75 public function testGetSize()
76 {
77 $size = filesize(__FILE__);
78 $handle = fopen(__FILE__, 'r');
79 $stream = new Stream($handle);
80 $this->assertEquals($size, $stream->getSize());
81 // Load from cache
82 $this->assertEquals($size, $stream->getSize());
83 $stream->close();
84 }
85
86 public function testEnsuresSizeIsConsistent()
87 {
88 $h = fopen('php://temp', 'w+');
89 $this->assertEquals(3, fwrite($h, 'foo'));
90 $stream = new Stream($h);
91 $this->assertEquals(3, $stream->getSize());
92 $this->assertEquals(4, $stream->write('test'));
93 $this->assertEquals(7, $stream->getSize());
94 $this->assertEquals(7, $stream->getSize());
95 $stream->close();
96 }
97
98 public function testProvidesStreamPosition()
99 {
100 $handle = fopen('php://temp', 'w+');
101 $stream = new Stream($handle);
102 $this->assertEquals(0, $stream->tell());
103 $stream->write('foo');
104 $this->assertEquals(3, $stream->tell());
105 $stream->seek(1);
106 $this->assertEquals(1, $stream->tell());
107 $this->assertSame(ftell($handle), $stream->tell());
108 $stream->close();
109 }
110
111 public function testCanDetachStream()
112 {
113 $r = fopen('php://temp', 'w+');
114 $stream = new Stream($r);
115 $stream->write('foo');
116 $this->assertTrue($stream->isReadable());
117 $this->assertSame($r, $stream->detach());
118 $stream->detach();
119
120 $this->assertFalse($stream->isReadable());
121 $this->assertFalse($stream->isWritable());
122 $this->assertFalse($stream->isSeekable());
123
124 $throws = function (callable $fn) use ($stream) {
125 try {
126 $fn($stream);
127 $this->fail();
128 } catch (\Exception $e) {}
129 };
130
131 $throws(function ($stream) { $stream->read(10); });
132 $throws(function ($stream) { $stream->write('bar'); });
133 $throws(function ($stream) { $stream->seek(10); });
134 $throws(function ($stream) { $stream->tell(); });
135 $throws(function ($stream) { $stream->eof(); });
136 $throws(function ($stream) { $stream->getSize(); });
137 $throws(function ($stream) { $stream->getContents(); });
138 $this->assertSame('', (string) $stream);
139 $stream->close();
140 }
141
142 public function testCloseClearProperties()
143 {
144 $handle = fopen('php://temp', 'r+');
145 $stream = new Stream($handle);
146 $stream->close();
147
148 $this->assertFalse($stream->isSeekable());
149 $this->assertFalse($stream->isReadable());
150 $this->assertFalse($stream->isWritable());
151 $this->assertNull($stream->getSize());
152 $this->assertEmpty($stream->getMetadata());
153 }
154
155 public function testDoesNotThrowInToString()
156 {
157 $s = \GuzzleHttp\Psr7\stream_for('foo');
158 $s = new NoSeekStream($s);
159 $this->assertEquals('foo', (string) $s);
160 }
161}
Note: See TracBrowser for help on using the repository browser.