Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class PhpSlim4ServerCodegen extends PhpSlimServerCodegen {
protected String mockPackage = "";
protected String utilsDirName = "Utils";
protected String utilsPackage = "";
protected String interfacesDirName = "Interfaces";
protected String interfacesPackage = "";

public PhpSlim4ServerCodegen() {
super();
Expand All @@ -54,6 +56,7 @@ public PhpSlim4ServerCodegen() {

mockPackage = invokerPackage + "\\" + mockDirName;
utilsPackage = invokerPackage + "\\" + utilsDirName;
interfacesPackage = invokerPackage + "\\" + interfacesDirName;
outputFolder = "generated-code" + File.separator + "slim4";
embeddedTemplateDir = templateDir = "php-slim4-server";

Expand Down Expand Up @@ -92,6 +95,7 @@ public void processOpts() {
// Update mockPackage and utilsPackage
mockPackage = invokerPackage + "\\" + mockDirName;
utilsPackage = invokerPackage + "\\" + utilsDirName;
interfacesPackage = invokerPackage + "\\" + interfacesDirName;
}

// make mock src path available in mustache template
Expand All @@ -104,6 +108,11 @@ public void processOpts() {
additionalProperties.put("utilsSrcPath", "./" + toSrcPath(utilsPackage, srcBasePath));
additionalProperties.put("utilsTestPath", "./" + toSrcPath(utilsPackage, testBasePath));

// same for interfaces package
additionalProperties.put("interfacesPackage", interfacesPackage);
additionalProperties.put("interfacesSrcPath", "./" + toSrcPath(interfacesPackage, srcBasePath));
additionalProperties.put("interfacesTestPath", "./" + toSrcPath(interfacesPackage, testBasePath));

if (additionalProperties.containsKey(PSR7_IMPLEMENTATION)) {
this.setPsr7Implementation((String) additionalProperties.get(PSR7_IMPLEMENTATION));
}
Expand Down Expand Up @@ -147,6 +156,9 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("string_utils_trait_test.mustache", toSrcPath(utilsPackage, testBasePath), toTraitName("StringUtils") + "Test.php"));
supportingFiles.add(new SupportingFile("model_utils_trait.mustache", toSrcPath(utilsPackage, srcBasePath), toTraitName("ModelUtils") + ".php"));
supportingFiles.add(new SupportingFile("model_utils_trait_test.mustache", toSrcPath(utilsPackage, testBasePath), toTraitName("ModelUtils") + "Test.php"));

// model interface
supportingFiles.add(new SupportingFile("model_interface.mustache", toSrcPath(interfacesPackage, srcBasePath), toInterfaceName("Model") + ".php"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@
},
"scripts": {
"test": [
"@test-apis",
"@test-models",
"@test-mock",
"@test-utils"
"phpunit"
],
"test-apis": "phpunit --testsuite Apis",
"test-models": "phpunit --testsuite Models",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,36 @@
*/
namespace {{modelPackage}};

use {{interfacesPackage}}\{{interfaceNamePrefix}}Model{{interfaceNameSuffix}};

/**
* {{classname}}
*
* @package {{modelPackage}}
* @author OpenAPI Generator team
* @link https://github.com/openapitools/openapi-generator
*/
class {{classname}}
class {{classname}} implements {{interfaceNamePrefix}}Model{{interfaceNameSuffix}}
{
private const MODEL_SCHEMA = <<<'SCHEMA'
{{{modelJson}}}
SCHEMA;
{{#vars}}

/** @var {{dataType}} ${{name}} {{#description}}{{description}}{{/description}}*/
private ${{name}};
{{/vars}}

/**
* Returns model schema.
*
* @param bool $assoc When TRUE, returned objects will be converted into associative arrays. Default FALSE.
*
* @return array
*/
public static function getOpenApiSchema($assoc = false)
{
return json_decode(static::MODEL_SCHEMA, $assoc);
}
}
{{/model}}{{/models}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* {{interfaceNamePrefix}}Model{{interfaceNameSuffix}}
*
* PHP version 7.1
*
* @package {{invokerPackage}}
* @author OpenAPI Generator team
* @link https://github.com/openapitools/openapi-generator
*/

/**{{#apiInfo}}{{#appName}}
* {{{appName}}}
*
{{/appName}}
{{#appDescription}}
* {{{appDescription}}}
{{/appDescription}}
{{#version}}
* The version of the OpenAPI document: {{{version}}}
{{/version}}
{{#infoEmail}}
* Contact: {{{infoEmail}}}
{{/infoEmail}}
* Generated by: https://github.com/openapitools/openapi-generator.git
*/

/**
* NOTE: This class is auto generated by the openapi generator program.
* https://github.com/openapitools/openapi-generator
* Do not edit the class manually.
*/
namespace {{interfacesPackage}};

/**
* {{interfaceNamePrefix}}Model{{interfaceNameSuffix}} Class Doc Comment
*
* @package {{interfacesPackage}}
* @author OpenAPI Generator team
* @link https://github.com/openapitools/openapi-generator
*/
interface {{interfaceNamePrefix}}Model{{interfaceNameSuffix}}
{
/**
* Returns model schema.
*
* @param bool $assoc When TRUE, returned objects will be converted into associative arrays. Default FALSE.
*
* @return array
*/
public static function getOpenApiSchema($assoc = false);
}
{{/apiInfo}}
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,17 @@ class {{classname}}Test extends TestCase
{
}
{{/vars}}

/**
* Test getOpenApiSchema static method
* @covers ::getOpenApiSchema
*/
public function testGetOpenApiSchema()
{
$schemaObject = {{classname}}::getOpenApiSchema();
$schemaArr = {{classname}}::getOpenApiSchema(true);
$this->assertIsObject($schemaObject);
$this->assertIsArray($schemaArr);
}
}
{{/model}}{{/models}}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
namespace {{mockPackage}};

use {{mockPackage}}\{{interfaceNamePrefix}}OpenApiDataMocker{{interfaceNameSuffix}} as IMocker;
use {{utilsPackage}}\{{traitNamePrefix}}ModelUtils{{traitNameSuffix}};
use StdClass;
use InvalidArgumentException;

Expand All @@ -46,6 +47,8 @@ use InvalidArgumentException;
*/
final class OpenApiDataMocker implements IMocker
{
use {{traitNamePrefix}}ModelUtils{{traitNameSuffix}};

/**
* Mocks OpenApi Data.
* @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#data-types
Expand Down Expand Up @@ -74,7 +77,8 @@ final class OpenApiDataMocker implements IMocker
case IMocker::DATA_TYPE_STRING:
$minLength = $options['minLength'] ?? 0;
$maxLength = $options['maxLength'] ?? null;
return $this->mockString($dataFormat, $minLength, $maxLength);
$enum = $options['enum'] ?? null;
return $this->mockString($dataFormat, $minLength, $maxLength, $enum);
case IMocker::DATA_TYPE_BOOLEAN:
return $this->mockBoolean();
case IMocker::DATA_TYPE_ARRAY:
Expand Down Expand Up @@ -276,11 +280,13 @@ final class OpenApiDataMocker implements IMocker
$options = $this->extractSchemaProperties($items);
$dataType = $options['type'];
$dataFormat = $options['format'] ?? null;
$ref = $options['$ref'] ?? null;

// always genarate smallest possible array to avoid huge JSON responses
// always generate smallest possible array to avoid huge JSON responses
$arrSize = ($maxSize < 1) ? $maxSize : max($minSize, 1);
while (count($arr) < $arrSize) {
$arr[] = $this->mock($dataType, $dataFormat, $options);
$data = $this->mockFromRef($ref);
$arr[] = ($data) ? $data : $this->mock($dataType, $dataFormat, $options);
}
return $arr;
}
Expand Down Expand Up @@ -360,12 +366,62 @@ final class OpenApiDataMocker implements IMocker
$options = $this->extractSchemaProperties($propValue);
$dataType = $options['type'];
$dataFormat = $options['dataFormat'] ?? null;
$obj->$propName = $this->mock($dataType, $dataFormat, $options);
$ref = $options['$ref'] ?? null;
$data = $this->mockFromRef($ref);
$obj->$propName = ($data) ? $data : $this->mock($dataType, $dataFormat, $options);
}

return $obj;
}

/**
* Mocks OpenApi Data from schema.
*
* @param array|object $schema OpenAPI schema
*
* @throws \InvalidArgumentException when invalid arguments passed
*
* @return mixed
*/
public function mockFromSchema($schema)
{
$props = $this->extractSchemaProperties($schema);
if (array_key_exists('$ref', $props) && !empty($props['$ref'])) {
return $this->mockFromRef($props['$ref']);
} elseif ($props['type'] === null) {
throw new InvalidArgumentException('"schema" must be object or assoc array with "type" property');
}
return $this->mock($props['type'], $props['format'], $props);
}

/**
* Mock data by referenced schema.
* TODO: this method will return model instance, not an StdClass
*
* @param string|null $ref Ref to model, eg. #/components/schemas/User
*
* @return mixed
*/
public function mockFromRef($ref)
{
$data = null;
if (is_string($ref) && !empty($ref)) {
$refName = static::getSimpleRef($ref);
$modelName = static::toModelName($refName);
$modelClass = '{{modelPackage}}\\' . $modelName;
if (!class_exists($modelClass) || !method_exists($modelClass, 'getOpenApiSchema')) {
throw new InvalidArgumentException(sprintf(
'Model %s not found or method %s doesn\'t exist',
$modelClass,
$modelClass . '::getOpenApiSchema'
));
}
$data = $this->mockFromSchema($modelClass::getOpenApiSchema(true));
}

return $data;
}

/**
* @internal Extract OAS properties from array or object.
* @codeCoverageIgnore
Expand Down Expand Up @@ -402,6 +458,7 @@ final class OpenApiDataMocker implements IMocker
'additionalProperties',
'required',
'example',
'$ref',
] as $propName
) {
if (is_array($val) && array_key_exists($propName, $val)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,5 +237,28 @@ interface {{interfaceNamePrefix}}OpenApiDataMocker{{interfaceNameSuffix}}
$additionalProperties = null,
$required = null
);

/**
* Mocks OpenApi Data from schema.
*
* @param array|object $schema OpenAPI schema
*
* @throws \InvalidArgumentException when invalid arguments passed
*
* @return mixed
*/
public function mockFromSchema($schema);

/**
* Mock data by referenced schema.
* TODO: this method will return model instance, not an StdClass
*
* @param string|null $ref Ref to model, eg. #/components/schemas/User
*
* @throws \InvalidArgumentException when invalid arguments passed
*
* @return mixed
*/
public function mockFromRef($ref);
}
{{/apiInfo}}
Loading