浏览代码

Fix sample body with array of object (#720)

* Fix sample body with array of object

* Fix indent

* Rename methods
Stéphane Monnot 1 年之前
父节点
当前提交
16a879d2d3
共有 2 个文件被更改,包括 80 次插入0 次删除
  1. 4 0
      src/Tools/WritingUtils.php
  2. 76 0
      tests/Unit/WritingUtilsTest.php

+ 4 - 0
src/Tools/WritingUtils.php

@@ -218,6 +218,10 @@ class WritingUtils
 
         return array_map(function ($param) {
             if (!empty($param['__fields'])) {
+                if ($param['type'] === 'object[]') {
+                    return [self::getSampleBody($param['__fields'])];
+                }
+
                 return self::getSampleBody($param['__fields']);
             }
 

+ 76 - 0
tests/Unit/WritingUtilsTest.php

@@ -78,6 +78,25 @@ class WritingUtilsTest extends BaseLaravelTest
         $this->assertEquals($expected, $queryParams);
     }
 
+    /** @test */
+    public function get_sample_body_with_array_fields()
+    {
+        $sampleBody = WritingUtils::getSampleBody($this->bodyParamsWithArrayFields());
+
+        $expected = [
+            'name' => 'Experience Form',
+            'fields' => [
+                [
+                    'name' => 'experience',
+                    'label' => 'Experience',
+                    'type' => 'textarea',
+                    'order' => 1,
+                ],
+            ],
+        ];
+        $this->assertEquals($expected, $sampleBody);
+    }
+
     private function queryParams(): array
     {
         return [
@@ -99,6 +118,63 @@ class WritingUtilsTest extends BaseLaravelTest
         ];
     }
 
+    private function bodyParamsWithArrayFields(): array
+    {
+        return [
+            'name' => [
+                'name' => 'name',
+                'description' => 'Form\'s name',
+                'required' => true,
+                'example' => 'Experience Form',
+                'type' => 'string',
+                'custom' => [],
+                '__fields' => [],
+            ],
+            'fields' => [
+                'name' => 'fields',
+                'description' => 'Form\'s fields',
+                'required' => false,
+                'example' => [[]],
+                'type' => 'object[]',
+                'custom' => [],
+                '__fields' => [
+                    'name' => [
+                        'name' => 'fields[].name',
+                        'description' => 'Field\'s name',
+                        'required' => true,
+                        'example' => 'experience',
+                        'type' => 'string',
+                        'custom' => [],
+                    ],
+                    'label' => [
+                        'name' => 'fields[].label',
+                        'description' => 'Field\'s label',
+                        'required' => true,
+                        'example' => 'Experience',
+                        'type' => 'string',
+                        'custom' => [],
+                    ],
+                    'type' => [
+                        'name' => 'fields[].type',
+                        'description' => 'Field\'s type',
+                        'required' => true,
+                        'example' => 'textarea',
+                        'type' => 'string',
+                        'custom' => [],
+                    ],
+                    'order' => [
+                        'name' => 'fields[].order',
+                        'description' => 'Field\'s order',
+                        'required' => true,
+                        'example' => 1,
+                        'type' => 'number',
+                        'custom' => [],
+                    ],
+                ],
+            ],
+        ];
+    }
+
     protected function assertStringsEqualNormalizingNewlines(string $expected, string $actual)
     {
         $this->assertEquals(str_replace("\r", "", $expected), str_replace("\r", "", $actual));