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