|
17 | 17 |
|
18 | 18 | package org.openapitools.codegen.php;
|
19 | 19 |
|
| 20 | +import io.swagger.parser.OpenAPIParser; |
20 | 21 | import io.swagger.v3.oas.models.OpenAPI;
|
21 |
| -import org.openapitools.codegen.CodegenConstants; |
22 |
| -import org.openapitools.codegen.CodegenModel; |
| 22 | +import io.swagger.v3.parser.core.models.ParseOptions; |
| 23 | +import org.openapitools.codegen.*; |
| 24 | +import org.openapitools.codegen.java.assertions.JavaFileAssert; |
| 25 | +import org.openapitools.codegen.languages.JavaMicroprofileServerCodegen; |
23 | 26 | import org.openapitools.codegen.languages.PhpClientCodegen;
|
24 |
| -import org.openapitools.codegen.TestUtils; |
25 | 27 | import org.testng.Assert;
|
| 28 | +import org.testng.annotations.BeforeMethod; |
26 | 29 | import org.testng.annotations.Test;
|
27 | 30 |
|
| 31 | +import java.io.File; |
| 32 | +import java.nio.file.Files; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Map; |
| 35 | +import java.util.function.Function; |
| 36 | +import java.util.stream.Collectors; |
| 37 | + |
28 | 38 | public class PhpClientCodegenTest {
|
29 | 39 |
|
| 40 | + protected PhpClientCodegen codegen; |
| 41 | + |
| 42 | + @BeforeMethod |
| 43 | + public void before() { |
| 44 | + codegen = new PhpClientCodegen(); |
| 45 | + } |
| 46 | + |
30 | 47 | @Test
|
31 | 48 | public void testInitialConfigValues() throws Exception {
|
32 | 49 | final PhpClientCodegen codegen = new PhpClientCodegen();
|
@@ -90,4 +107,69 @@ public void modelTest() {
|
90 | 107 | Assert.assertEquals(simpleName.classname, "DollarModel");
|
91 | 108 | Assert.assertEquals(simpleName.classVarName, "dollar_model");
|
92 | 109 | }
|
| 110 | + |
| 111 | + @Test |
| 112 | + public void testEnumUnknownDefaultCaseDeserializationEnabled() throws Exception { |
| 113 | + File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); |
| 114 | + output.deleteOnExit(); |
| 115 | + |
| 116 | + OpenAPI openAPI = new OpenAPIParser() |
| 117 | + .readLocation("src/test/resources/bugs/issue_20593.yaml", null, new ParseOptions()).getOpenAPI(); |
| 118 | + |
| 119 | + codegen.setOutputDir(output.getAbsolutePath()); |
| 120 | + codegen.additionalProperties().put(CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, "true"); |
| 121 | + |
| 122 | + ClientOptInput input = new ClientOptInput() |
| 123 | + .openAPI(openAPI) |
| 124 | + .config(codegen); |
| 125 | + |
| 126 | + DefaultGenerator generator = new DefaultGenerator(); |
| 127 | + Map<String, File> files = generator.opts(input).generate().stream() |
| 128 | + .collect(Collectors.toMap(File::getName, Function.identity())); |
| 129 | + |
| 130 | + List<String> modelContent = Files |
| 131 | + .readAllLines(files.get("Pet.php").toPath()) |
| 132 | + .stream() |
| 133 | + .map(String::trim) |
| 134 | + .collect(Collectors.toList()); |
| 135 | + |
| 136 | + for (String l : modelContent) { |
| 137 | + System.out.println(l); |
| 138 | + } |
| 139 | + |
| 140 | + Assert.assertListContains(modelContent, a -> a.equals("$color = self::COLOR_UNKNOWN_DEFAULT_OPEN_API;"), ""); |
| 141 | + Assert.assertListNotContains(modelContent, a -> a.equals("\"Invalid value '%s' for 'color', must be one of '%s'\","), ""); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void testEnumUnknownDefaultCaseDeserializationDisabled() throws Exception { |
| 146 | + File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); |
| 147 | + output.deleteOnExit(); |
| 148 | + |
| 149 | + OpenAPI openAPI = new OpenAPIParser() |
| 150 | + .readLocation("src/test/resources/bugs/issue_20593.yaml", null, new ParseOptions()).getOpenAPI(); |
| 151 | + |
| 152 | + codegen.setOutputDir(output.getAbsolutePath()); |
| 153 | + |
| 154 | + ClientOptInput input = new ClientOptInput() |
| 155 | + .openAPI(openAPI) |
| 156 | + .config(codegen); |
| 157 | + |
| 158 | + DefaultGenerator generator = new DefaultGenerator(); |
| 159 | + Map<String, File> files = generator.opts(input).generate().stream() |
| 160 | + .collect(Collectors.toMap(File::getName, Function.identity())); |
| 161 | + |
| 162 | + List<String> modelContent = Files |
| 163 | + .readAllLines(files.get("Pet.php").toPath()) |
| 164 | + .stream() |
| 165 | + .map(String::trim) |
| 166 | + .collect(Collectors.toList()); |
| 167 | + |
| 168 | + for (String l : modelContent) { |
| 169 | + System.out.println(l); |
| 170 | + } |
| 171 | + |
| 172 | + Assert.assertListNotContains(modelContent, a -> a.equals("$color = self::COLOR_UNKNOWN_DEFAULT_OPEN_API;"), ""); |
| 173 | + Assert.assertListContains(modelContent, a -> a.equalsIgnoreCase("\"Invalid value '%s' for 'color', must be one of '%s'\","), ""); |
| 174 | + } |
93 | 175 | }
|
0 commit comments