Skip to content

Commit b21c5f0

Browse files
committed
Merge pull request #1921 from wing328/php_generate_test
[PHP] generate files for unit testing models and API files
2 parents 719056c + 61c717b commit b21c5f0

File tree

14 files changed

+1064
-0
lines changed

14 files changed

+1064
-0
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfig.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ public interface CodegenConfig {
1919

2020
Map<String, Object> additionalProperties();
2121

22+
String testPackage();
23+
2224
String apiPackage();
2325

2426
String apiFileFolder();
2527

28+
String apiTestFileFolder();
29+
2630
String fileSuffix();
2731

2832
String outputFolder();
@@ -33,6 +37,8 @@ public interface CodegenConfig {
3337

3438
String modelFileFolder();
3539

40+
String modelTestFileFolder();
41+
3642
String modelPackage();
3743

3844
String toApiName(String name);
@@ -87,6 +93,10 @@ public interface CodegenConfig {
8793

8894
Map<String, String> modelTemplateFiles();
8995

96+
Map<String, String> apiTestTemplateFiles();
97+
98+
Map<String, String> modelTestTemplateFiles();
99+
90100
Set<String> languageSpecificPrimitives();
91101

92102
void preprocessSwagger(Swagger swagger);
@@ -97,6 +107,10 @@ public interface CodegenConfig {
97107

98108
String toModelFilename(String name);
99109

110+
String toApiTestFilename(String name);
111+
112+
String toModelTestFilename(String name);
113+
100114
String toModelImport(String name);
101115

102116
String toApiImport(String name);
@@ -115,6 +129,8 @@ public interface CodegenConfig {
115129

116130
String apiFilename(String templateName, String tag);
117131

132+
String apiTestFilename(String templateName, String tag);
133+
118134
boolean shouldOverwrite(String filename);
119135

120136
boolean isSkipOverwrite();

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ public class DefaultCodegen {
6363
protected Set<String> languageSpecificPrimitives = new HashSet<String>();
6464
protected Map<String, String> importMapping = new HashMap<String, String>();
6565
protected String modelPackage = "", apiPackage = "", fileSuffix;
66+
protected String testPackage = "";
6667
protected Map<String, String> apiTemplateFiles = new HashMap<String, String>();
6768
protected Map<String, String> modelTemplateFiles = new HashMap<String, String>();
69+
protected Map<String, String> apiTestTemplateFiles = new HashMap<String, String>();
70+
protected Map<String, String> modelTestTemplateFiles = new HashMap<String, String>();
6871
protected String templateDir;
6972
protected String embeddedTemplateDir;
7073
protected Map<String, Object> additionalProperties = new HashMap<String, Object>();
@@ -170,6 +173,10 @@ public Map<String, String> importMapping() {
170173
return importMapping;
171174
}
172175

176+
public String testPackage() {
177+
return testPackage;
178+
}
179+
173180
public String modelPackage() {
174181
return modelPackage;
175182
}
@@ -194,6 +201,14 @@ public String embeddedTemplateDir() {
194201
}
195202
}
196203

204+
public Map<String, String> apiTestTemplateFiles() {
205+
return apiTestTemplateFiles;
206+
}
207+
208+
public Map<String, String> modelTestTemplateFiles() {
209+
return modelTestTemplateFiles;
210+
}
211+
197212
public Map<String, String> apiTemplateFiles() {
198213
return apiTemplateFiles;
199214
}
@@ -210,6 +225,14 @@ public String modelFileFolder() {
210225
return outputFolder + "/" + modelPackage().replace('.', '/');
211226
}
212227

228+
public String apiTestFileFolder() {
229+
return outputFolder + "/" + testPackage().replace('.', '/');
230+
}
231+
232+
public String modelTestFileFolder() {
233+
return outputFolder + "/" + testPackage().replace('.', '/');
234+
}
235+
213236
public Map<String, Object> additionalProperties() {
214237
return additionalProperties;
215238
}
@@ -260,6 +283,16 @@ public String toApiFilename(String name) {
260283
return toApiName(name);
261284
}
262285

286+
/**
287+
* Return the file name of the Api Test
288+
*
289+
* @param name the file name of the Api
290+
* @return the file name of the Api
291+
*/
292+
public String toApiTestFilename(String name) {
293+
return toApiName(name) + "Test";
294+
}
295+
263296
/**
264297
* Return the variable name in the Api
265298
*
@@ -280,6 +313,16 @@ public String toModelFilename(String name) {
280313
return initialCaps(name);
281314
}
282315

316+
/**
317+
* Return the capitalized file name of the model test
318+
*
319+
* @param name the model name
320+
* @return the file name of the model
321+
*/
322+
public String toModelTestFilename(String name) {
323+
return initialCaps(name) + "Test";
324+
}
325+
283326
/**
284327
* Return the operation ID (method name)
285328
*
@@ -2028,6 +2071,19 @@ public String apiFilename(String templateName, String tag) {
20282071
return apiFileFolder() + '/' + toApiFilename(tag) + suffix;
20292072
}
20302073

2074+
/**
2075+
* Return the full path and API test file
2076+
*
2077+
* @param templateName template name
2078+
* @param tag tag
2079+
*
2080+
* @return the API test file name with full path
2081+
*/
2082+
public String apiTestFilename(String templateName, String tag) {
2083+
String suffix = apiTestTemplateFiles().get(templateName);
2084+
return apiTestFileFolder() + '/' + toApiTestFilename(tag) + suffix;
2085+
}
2086+
20312087
public boolean shouldOverwrite(String filename) {
20322088
return !(skipOverwrite && new File(filename).exists());
20332089
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,28 @@ public Reader getTemplate(String name) {
211211
writeToFile(filename, tmpl.execute(models));
212212
files.add(new File(filename));
213213
}
214+
215+
// to generate model test files
216+
for (String templateName : config.modelTestTemplateFiles().keySet()) {
217+
String suffix = config.modelTestTemplateFiles().get(templateName);
218+
String filename = config.modelTestFileFolder() + File.separator + config.toModelTestFilename(name) + suffix;
219+
if (!config.shouldOverwrite(filename)) {
220+
continue;
221+
}
222+
String templateFile = getFullTemplateFile(config, templateName);
223+
String template = readTemplate(templateFile);
224+
Template tmpl = Mustache.compiler()
225+
.withLoader(new Mustache.TemplateLoader() {
226+
@Override
227+
public Reader getTemplate(String name) {
228+
return getTemplateReader(getFullTemplateFile(config, name + ".mustache"));
229+
}
230+
})
231+
.defaultValue("")
232+
.compile(template);
233+
writeToFile(filename, tmpl.execute(models));
234+
files.add(new File(filename));
235+
}
214236
} catch (Exception e) {
215237
throw new RuntimeException("Could not generate model '" + name + "'", e);
216238
}
@@ -288,6 +310,30 @@ public Reader getTemplate(String name) {
288310
writeToFile(filename, tmpl.execute(operation));
289311
files.add(new File(filename));
290312
}
313+
314+
// to generate api test files
315+
for (String templateName : config.apiTestTemplateFiles().keySet()) {
316+
String filename = config.apiTestFilename(templateName, tag);
317+
if (!config.shouldOverwrite(filename) && new File(filename).exists()) {
318+
continue;
319+
}
320+
321+
String templateFile = getFullTemplateFile(config, templateName);
322+
String template = readTemplate(templateFile);
323+
Template tmpl = Mustache.compiler()
324+
.withLoader(new Mustache.TemplateLoader() {
325+
@Override
326+
public Reader getTemplate(String name) {
327+
return getTemplateReader(getFullTemplateFile(config, name + ".mustache"));
328+
}
329+
})
330+
.defaultValue("")
331+
.compile(template);
332+
333+
writeToFile(filename, tmpl.execute(operation));
334+
files.add(new File(filename));
335+
}
336+
291337
} catch (Exception e) {
292338
throw new RuntimeException("Could not generate api file for '" + tag + "'", e);
293339
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ public PhpClientCodegen() {
4141
outputFolder = "generated-code" + File.separator + "php";
4242
modelTemplateFiles.put("model.mustache", ".php");
4343
apiTemplateFiles.put("api.mustache", ".php");
44+
modelTestTemplateFiles.put("model_test.mustache", ".php");
45+
apiTestTemplateFiles.put("api_test.mustache", ".php");
4446
embeddedTemplateDir = templateDir = "php";
4547
apiPackage = invokerPackage + "\\Api";
4648
modelPackage = invokerPackage + "\\Model";
49+
testPackage = invokerPackage + "\\Tests";
4750

4851
reservedWords = new HashSet<String>(
4952
Arrays.asList(
@@ -236,6 +239,16 @@ public String modelFileFolder() {
236239
return (outputFolder + "/" + toPackagePath(modelPackage, srcBasePath));
237240
}
238241

242+
@Override
243+
public String apiTestFileFolder() {
244+
return (outputFolder + "/" + toPackagePath(testPackage, srcBasePath));
245+
}
246+
247+
@Override
248+
public String modelTestFileFolder() {
249+
return (outputFolder + "/" + toPackagePath(testPackage, srcBasePath));
250+
}
251+
239252
@Override
240253
public String getTypeDeclaration(Property p) {
241254
if (p instanceof ArrayProperty) {
@@ -364,6 +377,12 @@ public String toModelFilename(String name) {
364377
return toModelName(name);
365378
}
366379

380+
@Override
381+
public String toModelTestFilename(String name) {
382+
// should be the same as the model name
383+
return toModelName(name) + "Test";
384+
}
385+
367386
@Override
368387
public String toOperationId(String operationId) {
369388
// throw exception if method name is empty
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* {{classname}}Test
4+
* PHP version 5
5+
*
6+
* @category Class
7+
* @package {{invokerPackage}}
8+
* @author http://github.com/swagger-api/swagger-codegen
9+
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache Licene v2
10+
* @link https://github.com/swagger-api/swagger-codegen
11+
*/
12+
/**
13+
* Copyright 2016 SmartBear Software
14+
*
15+
* Licensed under the Apache License, Version 2.0 (the "License");
16+
* you may not use this file except in compliance with the License.
17+
* You may obtain a copy of the License at
18+
*
19+
* http://www.apache.org/licenses/LICENSE-2.0
20+
*
21+
* Unless required by applicable law or agreed to in writing, software
22+
* distributed under the License is distributed on an "AS IS" BASIS,
23+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24+
* See the License for the specific language governing permissions and
25+
* limitations under the License.
26+
*/
27+
28+
/**
29+
* NOTE: This class is auto generated by the swagger code generator program.
30+
* https://github.com/swagger-api/swagger-codegen
31+
* Do not edit the class manually.
32+
*/
33+
34+
namespace {{apiPackage}};
35+
36+
use \{{invokerPackage}}\Configuration;
37+
use \{{invokerPackage}}\ApiClient;
38+
use \{{invokerPackage}}\ApiException;
39+
use \{{invokerPackage}}\ObjectSerializer;
40+
41+
/**
42+
* {{classname}}Test Class Doc Comment
43+
*
44+
* @category Class
45+
* @package {{invokerPackage}}
46+
* @author http://github.com/swagger-api/swagger-codegen
47+
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache Licene v2
48+
* @link https://github.com/swagger-api/swagger-codegen
49+
*/
50+
{{#operations}}class {{classname}}Test extends \PHPUnit_Framework_TestCase
51+
{
52+
53+
/**
54+
* Setup before running each test case
55+
*/
56+
public static function setUpBeforeClass() {
57+
58+
}
59+
60+
/**
61+
* Clean up after running each test case
62+
*/
63+
public static function tearDownAfterClass() {
64+
65+
}
66+
67+
{{#operation}}
68+
/**
69+
* Test case for {{{operationId}}}
70+
*
71+
* {{{summary}}}
72+
*
73+
*/
74+
public function test_{{operationId}}() {
75+
76+
}
77+
{{/operation}}
78+
}
79+
{{/operations}}

0 commit comments

Comments
 (0)