@@ -62,11 +62,11 @@ public function testModeAllowedFlagPositionIrrelevant():void{
6262 $ mode = 'rwarrrrrw++++b12345 ' ;
6363 $ this ::assertTrue (StreamUtil::modeAllowsRead ($ mode ));
6464
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 );
6767
6868 $ this ::assertSame (substr ($ mode , 0 , 15 ), $ meta ['mode ' ]);
69- fclose ($ fh );
69+ fclose ($ resource );
7070 }
7171
7272 public function testGetContentsRewindsStream ():void {
@@ -80,7 +80,8 @@ public function testGetContentsRewindsStream():void{
8080 }
8181
8282 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 );
8485
8586 $ this ::assertFalse ($ stream ->isReadable ());
8687 $ this ::assertNull (StreamUtil::getContents ($ stream ));
@@ -131,10 +132,58 @@ public function testCopyToStreamException():void{
131132 $ this ->expectException (RuntimeException::class);
132133 $ this ->expectExceptionMessage ('$source must be readable and $destination must be writable ' );
133134
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 ();
136138
137139 StreamUtil::copyToStream ($ streamA , $ streamB );
138140 }
139141
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+
140189}
0 commit comments