Skip to content

Commit

Permalink
Merge pull request #3548 from wing328/php_enum_test_cases
Browse files Browse the repository at this point in the history
[PHP] add enum test cases for PHP generator (java)
  • Loading branch information
wing328 authored Aug 8, 2016
2 parents c4f3cb1 + b547d56 commit 328eb5b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,11 @@ public String toEnumDefaultValue(String value, String datatype) {

@Override
public String toEnumVarName(String name, String datatype) {
// for symbol, e.g. $, #
if (getSymbolName(name) != null) {
return (getSymbolName(name)).toUpperCase();
}

// number
if ("int".equals(datatype) || "double".equals(datatype) || "float".equals(datatype)) {
String varName = new String(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@
import io.swagger.models.ArrayModel;
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.DateTimeProperty;
import io.swagger.models.properties.LongProperty;
import io.swagger.models.properties.MapProperty;
import io.swagger.models.properties.RefProperty;
import io.swagger.models.properties.StringProperty;
import io.swagger.models.Swagger;
import io.swagger.parser.SwaggerParser;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.google.common.collect.Sets;
import org.testng.Assert;
Expand Down Expand Up @@ -268,4 +276,65 @@ public void modelNameTest(String name, String expectedName) {
Assert.assertEquals(cm.name, name);
Assert.assertEquals(cm.classname, expectedName);
}

@Test(description = "test enum array model")
public void enumArrayMdoelTest() {
final Swagger model = new SwaggerParser().read("src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml");
final DefaultCodegen codegen = new PhpClientCodegen();
final Model definition = model.getDefinitions().get("EnumArrays");

Property property = definition.getProperties().get("array_enum");
CodegenProperty prope = codegen.fromProperty("array_enum", property);
codegen.updateCodegenPropertyEnum(prope);
Assert.assertEquals(prope.datatypeWithEnum, "ARRAY_ENUM[]");
Assert.assertEquals(prope.enumName, "ARRAY_ENUM");
Assert.assertTrue(prope.isEnum);
Assert.assertEquals(prope.allowableValues.get("values"), Arrays.asList("fish", "crab"));

HashMap<String, String> fish= new HashMap<String, String>();
fish.put("name", "FISH");
fish.put("value", "\'fish\'");
HashMap<String, String> crab= new HashMap<String, String>();
crab.put("name", "CRAB");
crab.put("value", "\'crab\'");
Assert.assertEquals(prope.allowableValues.get("enumVars"), Arrays.asList(fish, crab));

// assert inner items
Assert.assertEquals(prope.datatypeWithEnum, "ARRAY_ENUM[]");
Assert.assertEquals(prope.enumName, "ARRAY_ENUM");
Assert.assertTrue(prope.items.isEnum);
Assert.assertEquals(prope.items.allowableValues.get("values"), Arrays.asList("fish", "crab"));
Assert.assertEquals(prope.items.allowableValues.get("enumVars"), Arrays.asList(fish, crab));

}

@Test(description = "test enum model for values (numeric, string, etc)")
public void enumMdoelValueTest() {
final Swagger model = new SwaggerParser().read("src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml");
final DefaultCodegen codegen = new PhpClientCodegen();
final Model definition = model.getDefinitions().get("Enum_Test");

Property property = definition.getProperties().get("enum_integer");
CodegenProperty prope = codegen.fromProperty("enum_integer", property);
codegen.updateCodegenPropertyEnum(prope);
Assert.assertEquals(prope.datatypeWithEnum, "ENUM_INTEGER");
Assert.assertEquals(prope.enumName, "ENUM_INTEGER");
Assert.assertTrue(prope.isEnum);
Assert.assertNull(prope.isContainer);
Assert.assertNull(prope.items);
Assert.assertEquals(prope.allowableValues.get("values"), Arrays.asList(1, -1));

HashMap<String, String> one = new HashMap<String, String>();
one.put("name", "1");
one.put("value", "1");
HashMap<String, String> minusOne = new HashMap<String, String>();
minusOne.put("name", "MINUS_1");
minusOne.put("value", "-1");
Assert.assertEquals(prope.allowableValues.get("enumVars"), Arrays.asList(one, minusOne));

}




}

0 comments on commit 328eb5b

Please sign in to comment.