@@ -22,9 +22,9 @@ class UploadedFileTest extends TestCase
2222 public static $ is_resource = null ;
2323
2424 /**
25- * Before each test, reset functions overrides.
25+ * After each test, reset functions overrides.
2626 */
27- protected function setUp (): void
27+ protected function tearDown (): void
2828 {
2929 self ::$ rename = null ;
3030 self ::$ is_uploaded_file = null ;
@@ -58,13 +58,16 @@ protected function getTempFilePath(): string
5858 return tempnam (sys_get_temp_dir (), 'UploadedFileTest ' );
5959 }
6060
61+ /**
62+ * @suppress PhanAccessMethodInternal
63+ * @suppress PhanTypeMismatchReturn
64+ */
6165 protected function getStreamMock (?string $ path = null , $ methods = []): StreamInterface
6266 {
6367 if ($ path === null ) {
6468 $ path = $ this ->getTempFilePath ();
6569 }
6670
67- /** @var StreamInterface */
6871 $ streamMock = $ this ->createMock (StreamInterface::class);
6972
7073 foreach ($ methods as $ name => $ returnValue ) {
@@ -89,21 +92,22 @@ protected function getStreamMock(?string $path = null, $methods = []): StreamInt
8992 */
9093 protected function getUploadedFile (?string $ path = null , ?int $ size = null , ?int $ error = null , ?string $ clientFilename = null , ?string $ clientMediaType = null )
9194 {
92- $ mockStream = $ this ->getStreamMock ($ path );
93- $ mockStream
94- ->method ('getContents ' )
95- ->willReturn ('foo bar baz ! ' );
95+ $ mockStream = $ this ->getStreamMock ($ path , [
96+ 'getContents ' => 'foo bar baz ! ' ,
97+ ]);
98+
99+ $ errorToUse = $ error ?? \UPLOAD_ERR_OK ;
96100
97101 if ($ clientMediaType !== null ) {
98- return new \IngeniozIT \Http \Message \UploadedFile ($ mockStream , $ size , $ error , $ clientFilename , $ clientMediaType );
102+ return new \IngeniozIT \Http \Message \UploadedFile ($ mockStream , $ size , $ errorToUse , $ clientFilename , $ clientMediaType );
99103 }
100104
101105 if ($ clientFilename !== null ) {
102- return new \IngeniozIT \Http \Message \UploadedFile ($ mockStream , $ size , $ error , $ clientFilename );
106+ return new \IngeniozIT \Http \Message \UploadedFile ($ mockStream , $ size , $ errorToUse , $ clientFilename );
103107 }
104108
105109 if ($ error !== null ) {
106- return new \IngeniozIT \Http \Message \UploadedFile ($ mockStream , $ size , $ error );
110+ return new \IngeniozIT \Http \Message \UploadedFile ($ mockStream , $ size , $ errorToUse );
107111 }
108112
109113 if ($ size !== null ) {
@@ -225,39 +229,58 @@ public function testMoveToExistingTargetPath()
225229 * throws \RuntimeException on any error during the move operation.
226230 * @dataProvider providerFsErrorCases
227231 */
228- public function testMoveToThrowsExceptionOnFilesystemError (bool $ isCli , bool $ streamWithUri )
232+ public function testMoveToThrowsExceptionOnFilesystemError (bool $ isCli , bool $ streamWithUri, bool $ overrideFopen )
229233 {
230234 $ uploadedFile = $ this ->getUploadedFile ();
231235 $ path = $ this ->getEmptyPath ();
232236 $ mockStream = $ this ->getStreamMock ($ path , [
233237 'getContents ' => 'foo bar baz ! ' ,
234- 'getMetadata ' => 'test_uri ' ,
238+ 'getMetadata ' => $ streamWithUri ? 'test_uri ' : null ,
235239 ]);
236-
237240 $ uploadedFile = new \IngeniozIT \Http \Message \UploadedFile ($ mockStream );
238241 $ path = $ this ->getEmptyPath ();
242+
239243 self ::$ rename = false ;
240244 self ::$ move_uploaded_file = false ;
241245 self ::$ file_put_contents = false ;
242246 self ::$ fopen = false ;
243247 self ::$ php_sapi_name = $ isCli ? 'cli ' : 'not_cli ' ;
248+ if ($ overrideFopen ) {
249+ self ::$ fopen = false ;
250+ }
244251
245252 $ this ->expectException (\RuntimeException::class);
246253 $ uploadedFile ->moveTo ($ path );
247254 }
248255
256+ /**
257+ * Provider. Gives filesystem error cases based on
258+ * - cli / not cli environment
259+ * - StreamInterface object with / without uri, metadata, fopen fail
260+ * @return array
261+ */
262+ public function providerFsErrorCases (): array
263+ {
264+ return [
265+ 'cli + uri ' => [true , true , false ],
266+ 'cli + no uri ' => [true , false , false ],
267+ 'not cli + uri ' => [false , true , false ],
268+ 'not cli + no uri ' => [false , false , false ],
269+ 'cli + no uri + fopen fail ' => [false , false , true ],
270+ ];
271+ }
272+
249273 /**
250274 * Move the uploaded file to a new location.
251- * throws \RuntimeException on any error during the move operation.
252- * @dataProvider providerFsErrorCases
275+ * @dataProvider providerFsWorkingCases
253276 */
254277 public function testMoveToWorksWithAllEnvs (bool $ isCli , bool $ streamWithUri )
255278 {
256279 $ uploadedFile = $ this ->getUploadedFile ();
257280 $ path = $ this ->getEmptyPath ();
258281 $ mockStream = $ this ->getStreamMock ($ path , [
259282 'getContents ' => 'foo bar baz ! ' ,
260- 'getMetadata ' => 'test_uri ' ,
283+ 'getMetadata ' => $ streamWithUri ? 'test_uri ' : null ,
261284 ]);
262285 $ uploadedFile = new \IngeniozIT \Http \Message \UploadedFile ($ mockStream );
263286 $ path = $ this ->getEmptyPath ();
@@ -278,13 +301,14 @@ public function testMoveToWorksWithAllEnvs(bool $isCli, bool $streamWithUri)
278301 * - StreamInterface object with / without uri metadata
279302 * @return array
280303 */
281- public function providerFsErrorCases (): array
304+ public function providerFsWorkingCases (): array
282305 {
283306 return [
284307 'cli + uri ' => [true , true ],
285308 'cli + no uri ' => [true , false ],
286309 'not cli + uri ' => [false , true ],
287310 'not cli + no uri ' => [false , false ],
311+ 'cli + no uri + fopen fail ' => [false , false ],
288312 ];
289313 }
290314
0 commit comments