Переглянути джерело

Use Storage Facade to load the file, using the default driver (config/filesystems.php)

Gabriel Peixoto 6 роки тому
батько
коміт
68fb2ee96a

+ 2 - 1
src/Tools/ResponseStrategies/ResponseFileStrategy.php

@@ -3,6 +3,7 @@
 namespace Mpociot\ApiDoc\Tools\ResponseStrategies;
 
 use Illuminate\Routing\Route;
+use Illuminate\Support\Facades\Storage;
 use Mpociot\Reflection\DocBlock\Tag;
 
 /**
@@ -32,7 +33,7 @@ class ResponseFileStrategy
         }
         $responseFileTag = array_first($responseFileTags);
 
-        $json = json_decode(file_get_contents(storage_path($responseFileTag->getContent()), true), true);
+        $json = json_decode(Storage::get($responseFileTag->getContent()), true);
 
         return response()->json($json);
     }

+ 1 - 1
tests/Fixtures/TestController.php

@@ -165,7 +165,7 @@ class TestController extends Controller
     }
 
     /**
-     * @responseFile /test.json
+     * @responseFile response_test.json
      */
     public function responseFileTag()
     {

+ 1 - 0
tests/Fixtures/response_test.json

@@ -0,0 +1 @@
+{"id":5,"name":"Jessica Jones","gender":"female"}

+ 38 - 10
tests/Unit/GeneratorTestCase.php

@@ -2,6 +2,9 @@
 
 namespace Mpociot\ApiDoc\Tests\Unit;
 
+use Illuminate\Contracts\Filesystem\FileNotFoundException;
+use Illuminate\Support\Facades\Config;
+use Illuminate\Support\Facades\Storage;
 use Orchestra\Testbench\TestCase;
 use Mpociot\ApiDoc\Tools\Generator;
 use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider;
@@ -268,14 +271,9 @@ abstract class GeneratorTestCase extends TestCase
     /** @test */
     public function can_parse_response_file_tag()
     {
-        // Create a file
-        $fp = fopen(storage_path('test.json'), 'w');
-        fwrite($fp, json_encode([
-            'id' => 5,
-            'name' => 'Jessica Jones',
-            'gender' => 'female',
-        ]));
-        fclose($fp);
+        // copy file to storage
+        $fixtureFileJson = file_get_contents(__DIR__.'/../Fixtures/response_test.json');
+        Storage::put('response_test.json', $fixtureFileJson);
 
         $route = $this->createRoute('GET', '/responseFileTag', 'responseFileTag');
         $parsed = $this->generator->processRoute($route);
@@ -284,10 +282,40 @@ abstract class GeneratorTestCase extends TestCase
         $this->assertTrue($parsed['showresponse']);
         $this->assertSame(
             $parsed['response'],
-            '{"id":5,"name":"Jessica Jones","gender":"female"}'
+            $fixtureFileJson
         );
 
-        unlink(storage_path('test.json'));
+        Storage::delete('response_test.json');
+    }
+
+    /** @test */
+    public function can_parse_response_file_tag_using_another_filesystem_driver()
+    {
+        // Mocking
+        Storage::fake('s3');
+
+        // copy file to storage
+        $fixtureFileJson = file_get_contents(__DIR__.'/../Fixtures/response_test.json');
+        Storage::disk('s3')->put('response_test.json', $fixtureFileJson);
+
+        $route = $this->createRoute('GET', '/responseFileTag', 'responseFileTag');
+
+        // Exception, because the file is not on Local storage
+        $this->expectException(FileNotFoundException::class);
+        $this->generator->processRoute($route);
+
+        // Now, set the default config to s3
+        Config::set('filesystems.default', 's3');
+        $parsed = $this->generator->processRoute($route);
+        $this->assertTrue(is_array($parsed));
+        $this->assertArrayHasKey('showresponse', $parsed);
+        $this->assertTrue($parsed['showresponse']);
+        $this->assertSame(
+            $parsed['response'],
+            $fixtureFileJson
+        );
+
+        Storage::disk('s3')->delete('response_test.json');
     }
 
     /** @test */