@@ -62,11 +62,11 @@ public function testModeAllowedFlagPositionIrrelevant():void{
62
62
$ mode = 'rwarrrrrw++++b12345 ' ;
63
63
$ this ::assertTrue (StreamUtil::modeAllowsRead ($ mode ));
64
64
65
- $ fh = fopen (__DIR__ .'/fopen-test.txt ' , $ mode );
66
- $ meta = stream_get_meta_data ($ fh );
65
+ $ resource = fopen (__DIR__ .'/fopen-test.txt ' , $ mode );
66
+ $ meta = stream_get_meta_data ($ resource );
67
67
68
68
$ this ::assertSame (substr ($ mode , 0 , 15 ), $ meta ['mode ' ]);
69
- fclose ($ fh );
69
+ fclose ($ resource );
70
70
}
71
71
72
72
public function testGetContentsRewindsStream ():void {
@@ -80,7 +80,8 @@ public function testGetContentsRewindsStream():void{
80
80
}
81
81
82
82
public function testGetContentsFromUnreadableStream ():void {
83
- $ stream = $ this ->streamFactory ->createStreamFromResource (fopen (__DIR__ .'/fopen-test.txt ' , 'a ' ));
83
+ $ resource = fopen (__DIR__ .'/fopen-test.txt ' , 'a ' );
84
+ $ stream = $ this ->streamFactory ->createStreamFromResource ($ resource );
84
85
85
86
$ this ::assertFalse ($ stream ->isReadable ());
86
87
$ this ::assertNull (StreamUtil::getContents ($ stream ));
@@ -131,10 +132,58 @@ public function testCopyToStreamException():void{
131
132
$ this ->expectException (RuntimeException::class);
132
133
$ this ->expectExceptionMessage ('$source must be readable and $destination must be writable ' );
133
134
134
- $ streamA = $ this ->streamFactory ->createStreamFromResource (fopen (__DIR__ .'/fopen-test.txt ' , 'a ' ));
135
- $ streamB = $ this ->streamFactory ->createStream ();
135
+ $ resource = fopen (__DIR__ .'/fopen-test.txt ' , 'a ' );
136
+ $ streamA = $ this ->streamFactory ->createStreamFromResource ($ resource );
137
+ $ streamB = $ this ->streamFactory ->createStream ();
136
138
137
139
StreamUtil::copyToStream ($ streamA , $ streamB );
138
140
}
139
141
142
+ public function testTryFopen ():void {
143
+ $ resource = StreamUtil::tryFopen (__DIR__ .'/fopen-test.txt ' , 'r ' );
144
+
145
+ $ this ::assertIsResource ($ resource );
146
+
147
+ fclose ($ resource );
148
+ }
149
+
150
+ public function testTryFopenThrowsExceptionInsteadOfWarning ():void {
151
+ $ this ->expectException (RuntimeException::class);
152
+ $ this ->expectExceptionMessage ('Unable to open "/path/not/found" using mode "r": fopen(/path/not/found) ' );
153
+
154
+ StreamUtil::tryFopen ('/path/not/found ' , 'r ' );
155
+ }
156
+
157
+ public function testTryFopenThrowsExceptionInsteadOfValueError ():void {
158
+ $ this ->expectException (RuntimeException::class);
159
+ $ this ->expectExceptionMessage ('Unable to open "" using mode "r": Path cannot be empty ' );
160
+
161
+ StreamUtil::tryFopen ('' , 'r ' );
162
+ }
163
+
164
+ public function testTryGetContents ():void {
165
+ $ resource = StreamUtil::tryFopen (__DIR__ .'/fopen-test.txt ' , 'r ' );
166
+
167
+ $ this ::assertStringContainsString ('foo ' , StreamUtil::tryGetContents ($ resource ));
168
+ }
169
+
170
+ public function testTryGetContentsThrowsExceptionOnUnreadableResource ():void {
171
+ $ this ->expectException (RuntimeException::class);
172
+ $ this ->expectExceptionMessage ('Unable to read stream contents: ' );
173
+
174
+ $ resource = StreamUtil::tryFopen (__DIR__ .'/fopen-test.txt ' , 'a ' );
175
+
176
+ StreamUtil::tryGetContents ($ resource );
177
+ }
178
+
179
+ public function testTryGetContentsThrowsExceptionOnInvalidResource ():void {
180
+ $ this ->expectException (RuntimeException::class);
181
+ $ this ->expectExceptionMessage ('supplied resource is not a valid stream resource ' );
182
+
183
+ $ resource = StreamUtil::tryFopen (__DIR__ .'/fopen-test.txt ' , 'r ' );
184
+ fclose ($ resource );
185
+
186
+ StreamUtil::tryGetContents ($ resource );
187
+ }
188
+
140
189
}
0 commit comments