1
|
|
|
<?php |
2
|
|
|
namespace RingCentral\Tests\Psr7; |
3
|
|
|
|
4
|
|
|
use RingCentral\Psr7\NoSeekStream; |
5
|
|
|
use RingCentral\Psr7\Stream; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @covers RingCentral\Psr7\Stream |
9
|
|
|
*/ |
10
|
|
|
class 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
|
|
|
$self = $this; |
125
|
|
|
|
126
|
|
|
$throws = function ($fn) use ($stream, $self) { |
127
|
|
|
try { |
128
|
|
|
$fn($stream); |
129
|
|
|
$self->fail(); |
130
|
|
|
} catch (\Exception $e) {} |
|
|
|
|
131
|
|
|
}; |
132
|
|
|
|
133
|
|
|
$throws(function ($stream) { $stream->read(10); }); |
134
|
|
|
$throws(function ($stream) { $stream->write('bar'); }); |
135
|
|
|
$throws(function ($stream) { $stream->seek(10); }); |
136
|
|
|
$throws(function ($stream) { $stream->tell(); }); |
137
|
|
|
$throws(function ($stream) { $stream->eof(); }); |
138
|
|
|
$throws(function ($stream) { $stream->getSize(); }); |
139
|
|
|
$throws(function ($stream) { $stream->getContents(); }); |
140
|
|
|
$this->assertSame('', (string) $stream); |
141
|
|
|
$stream->close(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function testCloseClearProperties() |
145
|
|
|
{ |
146
|
|
|
$handle = fopen('php://temp', 'r+'); |
147
|
|
|
$stream = new Stream($handle); |
148
|
|
|
$stream->close(); |
149
|
|
|
|
150
|
|
|
$this->assertFalse($stream->isSeekable()); |
151
|
|
|
$this->assertFalse($stream->isReadable()); |
152
|
|
|
$this->assertFalse($stream->isWritable()); |
153
|
|
|
$this->assertNull($stream->getSize()); |
154
|
|
|
$this->assertEmpty($stream->getMetadata()); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function testDoesNotThrowInToString() |
158
|
|
|
{ |
159
|
|
|
$s = \RingCentral\Psr7\stream_for('foo'); |
160
|
|
|
$s = new NoSeekStream($s); |
161
|
|
|
$this->assertEquals('foo', (string) $s); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths