Selaa lähdekoodia

Fix custom app url in Postman collection

The Postman collection currently doesn't use the APP_URL environment variable. This can be forced in the URL Laravel Facade
Enrico Werkema 6 vuotta sitten
vanhempi
commit
8ac4b33b21

+ 3 - 0
src/Postman/CollectionWriter.php

@@ -2,6 +2,7 @@
 
 namespace Mpociot\ApiDoc\Postman;
 
+use Illuminate\Support\Facades\URL;
 use Ramsey\Uuid\Uuid;
 use Illuminate\Support\Collection;
 
@@ -24,6 +25,8 @@ class CollectionWriter
 
     public function getCollection()
     {
+        URL::forceRootUrl(config('app.url'));
+
         $collection = [
             'variables' => [],
             'info' => [

+ 1 - 0
tests/Fixtures/collection_updated_url.json

@@ -0,0 +1 @@
+{"variables":[],"info":{"name":"Laravel API","_postman_id":"","description":"","schema":"https:\/\/schema.getpostman.com\/json\/collection\/v2.0.0\/collection.json"},"item":[{"name":"Group A","description":"","item":[{"name":"Example title.","request":{"url":"http:\/\/yourapp.app\/api\/test","method":"GET","body":{"mode":"formdata","formdata":[]},"description":"This will be the long description.\nIt can also be multiple lines long.","response":[]}},{"name":"http:\/\/yourapp.app\/api\/responseTag","request":{"url":"http:\/\/yourapp.app\/api\/responseTag","method":"POST","body":{"mode":"formdata","formdata":[]},"description":"","response":[]}}]}]}

+ 18 - 0
tests/GenerateDocumentationTest.php

@@ -2,6 +2,8 @@
 
 namespace Mpociot\ApiDoc\Tests;
 
+use Illuminate\Support\Facades\Config;
+use Illuminate\Support\Facades\URL;
 use ReflectionException;
 use RecursiveIteratorIterator;
 use RecursiveDirectoryIterator;
@@ -229,6 +231,22 @@ class GenerateDocumentationTest extends TestCase
         $this->assertEquals($generatedCollection, $fixtureCollection);
     }
 
+    /** @test */
+    public function generated_postman_collection_can_have_custom_url()
+    {
+        Config::set('app.url', 'http://yourapp.app');
+        RouteFacade::get('/api/test', TestController::class.'@withEndpointDescription');
+        RouteFacade::post('/api/responseTag', TestController::class.'@withResponseTag');
+
+        config(['apidoc.routes.0.match.prefixes' => ['api/*']]);
+        $this->artisan('apidoc:generate');
+
+        $generatedCollection = json_decode(file_get_contents(__DIR__.'/../public/docs/collection.json'));
+        $generatedCollection->info->_postman_id = '';
+        $fixtureCollection = json_decode(file_get_contents(__DIR__.'/Fixtures/collection_updated_url.json'));
+        $this->assertEquals($generatedCollection, $fixtureCollection);
+    }
+
     /** @test */
     public function can_append_custom_http_headers()
     {