Browse Source

Update based on feedback.

I'm still not able to reproduce the problem reported.
Mark Story 3 years ago
parent
commit
4ba2965799

+ 1 - 5
src/TestSuite/Constraint/Response/FileSentAs.php

@@ -35,16 +35,12 @@ class FileSentAs extends ResponseBase
      */
     public function matches($other): bool
     {
-        /** @psalm-suppress PossiblyNullReference */
-        $result = $this->response->getFile()->getPathName() === $other;
         $file = $this->response->getFile();
         if (!$file) {
             return false;
         }
 
-        $result = $file->getPathName() === $other;
-
-        return $result;
+        return $file->getPathName() === $other;
     }
 
     /**

+ 4 - 1
src/TestSuite/IntegrationTestTrait.php

@@ -1322,7 +1322,10 @@ trait IntegrationTestTrait
         $this->assertThat(null, new FileSent($this->_response), $verboseMessage);
         $this->assertThat($expected, new FileSentAs($this->_response), $verboseMessage);
 
-        $this->_response->getBody()->close();
+        $body = $this->_response->getBody();
+        if ($body) {
+            $body->close();
+        }
     }
 
     /**

+ 9 - 12
tests/TestCase/TestSuite/IntegrationTestTraitTest.php

@@ -1361,19 +1361,16 @@ class IntegrationTestTraitTest extends TestCase
      */
     public function testSendUnlinked(): void
     {
-        $filename = TMP . 'cake_test_send_unlink.php';
-        file_put_contents(
-            $filename,
-            file_get_contents(
-                TEST_APP . 'TestApp' . DS . 'Controller' . DS . 'PostsController.php'
-            )
-        );
+        $file = microtime(true) . 'txt';
+        $path = TMP . $file;
+        file_put_contents($path, 'testing unlink');
 
-        $this->get('/posts/file?file=' . $filename);
-        $this->assertFileResponse($filename);
-        $this->assertFileExists($filename);
-        system("rm -rf {$filename}");
-        $this->assertFileDoesNotExist($filename);
+        $this->get("/posts/file?file={$file}");
+        $this->assertResponseOk();
+        $this->assertFileResponse($path);
+        $this->assertFileExists($path);
+        system("rm -rf {$path}");
+        $this->assertFileDoesNotExist($path);
     }
 
     /**

+ 3 - 1
tests/test_app/TestApp/Controller/PostsController.php

@@ -140,7 +140,9 @@ class PostsController extends AppController
     {
         $filename = $this->request->getQuery('file');
         if ($filename) {
-            return $this->response->withFile($filename);
+            $path = TMP . $filename;
+            return $this->response->withFile($path, ['download' => true])
+                ->withHeader('Content-Disposition', "attachment;filename=*UTF-8''{$filename}");
         }
 
         return $this->response->withFile(__FILE__);