From 2a4ff4b067b1741918fb558bdfb9b6913a1bc018 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Monnot?= Date: Sat, 2 Sep 2023 22:03:24 +0900 Subject: [PATCH 1/3] Fix sample body with array of object --- src/Tools/WritingUtils.php | 10 +++-- tests/Unit/WritingUtilsTest.php | 76 +++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 3 deletions(-) diff --git a/src/Tools/WritingUtils.php b/src/Tools/WritingUtils.php index 4a37d56c..cbe70a0e 100644 --- a/src/Tools/WritingUtils.php +++ b/src/Tools/WritingUtils.php @@ -110,7 +110,7 @@ public static function printQueryParamsAsKeyValue( $output = isset($braces[0]) ? "{$braces[0]}\n" : ''; foreach ($cleanQueryParams as $parameter => $value) { $output .= self::printSingleQueryParamAsKeyValue($value, $spacesIndentation, $startLinesWith, $quote, - $parameter, $delimiter, $endLinesWith); + $parameter, $delimiter, $endLinesWith); } $closing = isset($braces[1]) ? str_repeat(" ", $closingBraceIndentation) . "{$braces[1]}" : ''; @@ -150,11 +150,11 @@ protected static function printSingleQueryParamAsKeyValue( $parameterString = sprintf('%s[%s]', $parameter, $item); if (is_array($itemValue)) { $output .= static::printSingleQueryParamAsKeyValue($itemValue, $spacesIndentation, $startLinesWith, - $quote, $parameterString, $delimiter, $endLinesWith); + $quote, $parameterString, $delimiter, $endLinesWith); } else { $output .= str_repeat(" ", $spacesIndentation); $output .= sprintf("%s%s%s%s%s %s%s%s%s\n", $startLinesWith, $quote, $parameterString, $quote, - $delimiter, $quote, $itemValue, $quote, $endLinesWith); + $delimiter, $quote, $itemValue, $quote, $endLinesWith); } } } @@ -218,6 +218,10 @@ public static function getSampleBody(array $nestedBodyParameters) return array_map(function ($param) { if (!empty($param['__fields'])) { + if ($param['type'] === 'object[]') { + return [self::getSampleBody($param['__fields'])]; + } + return self::getSampleBody($param['__fields']); } diff --git a/tests/Unit/WritingUtilsTest.php b/tests/Unit/WritingUtilsTest.php index 6cb139ef..9b9b068c 100644 --- a/tests/Unit/WritingUtilsTest.php +++ b/tests/Unit/WritingUtilsTest.php @@ -78,6 +78,25 @@ public function print_query_params_as_string_bash() $this->assertEquals($expected, $queryParams); } + /** @test */ + public function get_sample_body() + { + $sampleBody = WritingUtils::getSampleBody($this->bodyParams()); + + $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 @@ private function queryParams(): array ]; } + private function bodyParams(): 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)); From f5a8afdacb5201b1eebc2081afa59f7b6092589e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Monnot?= Date: Sat, 2 Sep 2023 22:12:57 +0900 Subject: [PATCH 2/3] Fix indent --- src/Tools/WritingUtils.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Tools/WritingUtils.php b/src/Tools/WritingUtils.php index cbe70a0e..43efe255 100644 --- a/src/Tools/WritingUtils.php +++ b/src/Tools/WritingUtils.php @@ -110,7 +110,7 @@ public static function printQueryParamsAsKeyValue( $output = isset($braces[0]) ? "{$braces[0]}\n" : ''; foreach ($cleanQueryParams as $parameter => $value) { $output .= self::printSingleQueryParamAsKeyValue($value, $spacesIndentation, $startLinesWith, $quote, - $parameter, $delimiter, $endLinesWith); + $parameter, $delimiter, $endLinesWith); } $closing = isset($braces[1]) ? str_repeat(" ", $closingBraceIndentation) . "{$braces[1]}" : ''; @@ -150,11 +150,11 @@ protected static function printSingleQueryParamAsKeyValue( $parameterString = sprintf('%s[%s]', $parameter, $item); if (is_array($itemValue)) { $output .= static::printSingleQueryParamAsKeyValue($itemValue, $spacesIndentation, $startLinesWith, - $quote, $parameterString, $delimiter, $endLinesWith); + $quote, $parameterString, $delimiter, $endLinesWith); } else { $output .= str_repeat(" ", $spacesIndentation); $output .= sprintf("%s%s%s%s%s %s%s%s%s\n", $startLinesWith, $quote, $parameterString, $quote, - $delimiter, $quote, $itemValue, $quote, $endLinesWith); + $delimiter, $quote, $itemValue, $quote, $endLinesWith); } } } From 35515b8b4c970d1b3d5277866e2fd3d115b3141f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Monnot?= Date: Sun, 3 Sep 2023 17:40:06 +0900 Subject: [PATCH 3/3] Rename methods --- tests/Unit/WritingUtilsTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Unit/WritingUtilsTest.php b/tests/Unit/WritingUtilsTest.php index 9b9b068c..91046f2a 100644 --- a/tests/Unit/WritingUtilsTest.php +++ b/tests/Unit/WritingUtilsTest.php @@ -79,9 +79,9 @@ public function print_query_params_as_string_bash() } /** @test */ - public function get_sample_body() + public function get_sample_body_with_array_fields() { - $sampleBody = WritingUtils::getSampleBody($this->bodyParams()); + $sampleBody = WritingUtils::getSampleBody($this->bodyParamsWithArrayFields()); $expected = [ 'name' => 'Experience Form', @@ -118,7 +118,7 @@ private function queryParams(): array ]; } - private function bodyParams(): array + private function bodyParamsWithArrayFields(): array { return [ 'name' => [