瀏覽代碼

added tests for modifying response file by adding a json beside file storage path in responseFile tag

franko4don 6 年之前
父節點
當前提交
a6333b0d3a

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

@@ -47,7 +47,7 @@ class ResponseFileStrategy
             preg_match('/^(\d{3})?\s?([\S]*[\s]*?)(\{.*\})?$/', $responseFileTag->getContent(), $result);
             $status = $result[1] ?: 200;
             $content = $result[2] ? file_get_contents(storage_path(trim($result[2])), true) : '{}';
-            $json = isset($result[3]) ? preg_replace("/'/", "\"", $result[3]) : "{}";
+            $json = !empty($result[3]) ? str_replace("'", "\"", $result[3]) : "{}";
             $merged = array_merge(json_decode($content, true), json_decode($json, true));
             return new JsonResponse($merged, (int) $status);
         }, $responseFileTags);

+ 8 - 0
tests/Fixtures/TestController.php

@@ -207,4 +207,12 @@ class TestController extends Controller
     {
         return '';
     }
+
+    /**
+     * @responseFile response_test.json {"message" : "Serendipity"}
+     */
+    public function responseFileTagAndCustomJson()
+    {
+        return '';
+    }
 }

+ 25 - 0
tests/Unit/GeneratorTestCase.php

@@ -354,6 +354,31 @@ abstract class GeneratorTestCase extends TestCase
         unlink(storage_path('response_test.json'));
     }
 
+    /** @test */
+    public function can_replace_key_value_pair_in_response_file()
+    {
+        // copy file to storage
+        $filePath = __DIR__.'/../Fixtures/response_test.json';
+        $fixtureFileJson = file_get_contents($filePath);
+        copy($filePath, storage_path('response_test.json'));
+
+        $route = $this->createRoute('GET', '/responseFileTagAndCustomJson', 'responseFileTagAndCustomJson');
+        $parsed = $this->generator->processRoute($route);
+        $response = array_first($parsed['response']);
+
+        $this->assertTrue(is_array($parsed));
+        $this->assertArrayHasKey('showresponse', $parsed);
+        $this->assertTrue($parsed['showresponse']);
+        $this->assertTrue(is_array($response));
+        $this->assertEquals(200, $response['status']);
+        $this->assertNotSame(
+            $response['content'],
+            $fixtureFileJson
+        );
+
+        unlink(storage_path('response_test.json'));
+    }
+
     /** @test */
     public function can_parse_multiple_response_file_tags_with_status_codes()
     {