瀏覽代碼

Test: get response from stored json file

Gabriel Peixoto 6 年之前
父節點
當前提交
b0744fbc44
共有 3 個文件被更改,包括 35 次插入1 次删除
  1. 8 0
      tests/Fixtures/TestController.php
  2. 1 1
      tests/GenerateDocumentationTest.php
  3. 26 0
      tests/Unit/GeneratorTestCase.php

+ 8 - 0
tests/Fixtures/TestController.php

@@ -163,4 +163,12 @@ class TestController extends Controller
     {
         return '';
     }
+
+    /**
+     * @responseFile /test.json
+     */
+    public function responseFileTag()
+    {
+        return '';
+    }
 }

+ 1 - 1
tests/GenerateDocumentationTest.php

@@ -220,7 +220,7 @@ class GenerateDocumentationTest extends TestCase
         config(['apidoc.routes.0.match.prefixes' => ['api/*']]);
         $this->artisan('apidoc:generate');
 
-        $generatedCollection = json_decode(file_get_contents(__DIR__.'/../public/docs/collection.json'));
+        $generatedCollection = json_decode(file_get_contents(__DIR__.'/../../public/docs/collection.json'));
         $generatedCollection->info->_postman_id = '';
         $fixtureCollection = json_decode(file_get_contents(__DIR__.'/Fixtures/collection.json'));
         $this->assertEquals($generatedCollection, $fixtureCollection);

+ 26 - 0
tests/Unit/GeneratorTestCase.php

@@ -265,6 +265,32 @@ abstract class GeneratorTestCase extends TestCase
         ], json_decode($parsed['response'], true));
     }
 
+    /** @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);
+
+        $route = $this->createRoute('GET', '/responseFileTag', 'responseFileTag');
+        $parsed = $this->generator->processRoute($route);
+        $this->assertTrue(is_array($parsed));
+        $this->assertArrayHasKey('showresponse', $parsed);
+        $this->assertTrue($parsed['showresponse']);
+        $this->assertSame(
+            $parsed['response'],
+            '{"id":5,"name":"Jessica Jones","gender":"female"}'
+        );
+
+        unlink(storage_path('test.json'));
+    }
+
+
     /** @test */
     public function uses_configured_settings_when_calling_route()
     {