[php-slim4] Partial generation (#11069)

* Update tests folder

I forgot to update tests folder in latest PR. Fixing my mistake.

* Implement part generation
This commit is contained in:
Yuriy Belenko 2021-12-17 11:11:06 +03:00 committed by GitHub
parent 7dbcac3b6c
commit d65bf8d1a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 46 additions and 75 deletions

View File

@ -158,6 +158,10 @@ public class PhpSlim4ServerCodegen extends AbstractPhpCodegen {
public void processOpts() {
super.processOpts();
Boolean generateModels = additionalProperties.containsKey(CodegenConstants.GENERATE_MODELS) && Boolean.TRUE.equals(additionalProperties.get(CodegenConstants.GENERATE_MODELS));
Boolean generateApiTests = additionalProperties.containsKey(CodegenConstants.GENERATE_API_TESTS) && Boolean.TRUE.equals(additionalProperties.get(CodegenConstants.GENERATE_API_TESTS));
Boolean generateModelTests = additionalProperties.containsKey(CodegenConstants.GENERATE_MODEL_TESTS) && Boolean.TRUE.equals(additionalProperties.get(CodegenConstants.GENERATE_MODEL_TESTS));
if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) {
// Update the invokerPackage for the default authPackage
authPackage = invokerPackage + "\\" + authDirName;
@ -210,14 +214,26 @@ public class PhpSlim4ServerCodegen extends AbstractPhpCodegen {
supportingFiles.add(new SupportingFile("index.mustache", "public", "index.php"));
supportingFiles.add(new SupportingFile(".htaccess", "public", ".htaccess"));
supportingFiles.add(new SupportingFile("SlimRouter.mustache", toSrcPath(invokerPackage, srcBasePath), "SlimRouter.php"));
supportingFiles.add(new SupportingFile("phpunit.xml.mustache", "", "phpunit.xml.dist"));
// don't generate phpunit config when tests generation disabled
if (Boolean.TRUE.equals(generateApiTests) || Boolean.TRUE.equals(generateModelTests)) {
supportingFiles.add(new SupportingFile("phpunit.xml.mustache", "", "phpunit.xml.dist"));
additionalProperties.put("generateTests", Boolean.TRUE);
}
supportingFiles.add(new SupportingFile("phpcs.xml.mustache", "", "phpcs.xml.dist"));
supportingFiles.add(new SupportingFile("htaccess_deny_all", "config", ".htaccess"));
supportingFiles.add(new SupportingFile("config_example.mustache", "config" + File.separator + "dev", "example.inc.php"));
supportingFiles.add(new SupportingFile("config_example.mustache", "config" + File.separator + "prod", "example.inc.php"));
supportingFiles.add(new SupportingFile("base_model.mustache", toSrcPath(invokerPackage, srcBasePath), "BaseModel.php"));
supportingFiles.add(new SupportingFile("base_model_test.mustache", toSrcPath(invokerPackage, testBasePath), "BaseModelTest.php"));
if (Boolean.TRUE.equals(generateModels)) {
supportingFiles.add(new SupportingFile("base_model.mustache", toSrcPath(invokerPackage, srcBasePath), "BaseModel.php"));
}
if (Boolean.TRUE.equals(generateModelTests)) {
supportingFiles.add(new SupportingFile("base_model_test.mustache", toSrcPath(invokerPackage, testBasePath), "BaseModelTest.php"));
}
}
@Override

View File

@ -46,6 +46,7 @@ $ php -S localhost:8888 -t php-slim-server/public
> It may also be useful for testing purposes or for application demonstrations that are run in controlled environments.
> It is not intended to be a full-featured web server. It should not be used on a public network.
{{#generateTests}}
## Tests
### PHPUnit
@ -59,9 +60,14 @@ How to write tests read at [2. Writing Tests for PHPUnit - PHPUnit 8.5 Manual](h
Command | Target
---- | ----
`$ composer test` | All tests
{{#generateApiTests}}
`$ composer test-apis` | Apis tests
{{/generateApiTests}}
{{#generateModelTests}}
`$ composer test-models` | Models tests
{{/generateModelTests}}
{{/generateTests}}
#### Config
Package contains fully functional config `./phpunit.xml.dist` file. Create `./phpunit.xml` in root folder to override it.

View File

@ -29,7 +29,9 @@
{{/isZendDiactoros}}
},
"require-dev": {
{{#generateTests}}
"phpunit/phpunit": "^8.0 || ^9.0",
{{/generateTests}}
"overtrue/phplint": "^2.0.2",
"squizlabs/php_codesniffer": "^3.5"
},
@ -43,11 +45,17 @@
"psr-4": { "{{escapedInvokerPackage}}\\": "{{testBasePath}}/" }
},
"scripts": {
{{#generateTests}}
"test": [
"phpunit"
],
{{#generateApiTests}}
"test-apis": "phpunit --testsuite Apis",
{{/generateApiTests}}
{{#generateModelTests}}
"test-models": "phpunit --testsuite Models",
{{/generateModelTests}}
{{/generateTests}}
"phpcs": "phpcs",
"phplint": "phplint ./ --exclude=vendor"
}

View File

@ -8,13 +8,17 @@
</include>
</coverage>
<testsuites>
{{#generateApiTests}}
<testsuite name="Apis">
<directory>{{apiTestPath}}</directory>
</testsuite>
{{/generateApiTests}}
{{#generateModelTests}}
<testsuite name="Models">
<file>./{{testBasePath}}/BaseModelTest.php</file>
<directory>{{modelTestPath}}</directory>
</testsuite>
{{/generateModelTests}}
</testsuites>
<php>
<ini name="error_reporting" value="E_ALL"/>

View File

@ -1,63 +0,0 @@
<?php
/**
* OpenAPI Petstore
* PHP version 7.4
*
* @package OpenAPIServer
* @author OpenAPI Generator team
* @link https://github.com/openapitools/openapi-generator
*/
/**
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
* The version of the OpenAPI document: 1.0.0
* 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 OpenAPIServer\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
/**
* JsonBodyParserMiddleware Class Doc Comment
*
* @package OpenAPIServer\Middleware
* @author OpenAPI Generator team
* @link https://github.com/openapitools/openapi-generator
*/
final class JsonBodyParserMiddleware implements MiddlewareInterface
{
/**
* Parse incoming JSON input into a native PHP format
* Copied from Slim4 guide
* @ref https://www.slimframework.com/docs/v4/objects/request.html#the-request-body
*
* @param ServerRequestInterface $request HTTP request
* @param RequestHandlerInterface $handler Request handler
*
* @return ResponseInterface HTTP response
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$contentType = $request->getHeaderLine('Content-Type');
if (strstr($contentType, 'application/json')) {
$contents = json_decode(file_get_contents('php://input'), true);
if (json_last_error() === JSON_ERROR_NONE) {
$request = $request->withParsedBody($contents);
}
}
return $handler->handle($request);
}
}

View File

@ -2,7 +2,7 @@
/**
* OpenAPI Petstore
* PHP version 7.2
* PHP version 7.4
*
* @package OpenAPIServer
* @author OpenAPI Generator team

View File

@ -2,7 +2,7 @@
/**
* OpenAPI Petstore
* PHP version 7.2
* PHP version 7.4
*
* @package OpenAPIServer
* @author OpenAPI Generator team

View File

@ -2,7 +2,7 @@
/**
* OpenAPI Petstore
* PHP version 7.2
* PHP version 7.4
*
* @package OpenAPIServer
* @author OpenAPI Generator team

View File

@ -2,7 +2,7 @@
/**
* OpenAPI Petstore
* PHP version 7.2
* PHP version 7.4
*
* @package OpenAPIServer
* @author OpenAPI Generator team

View File

@ -2,7 +2,7 @@
/**
* OpenAPI Petstore
* PHP version 7.2
* PHP version 7.4
*
* @package OpenAPIServer
* @author OpenAPI Generator team

View File

@ -2,7 +2,7 @@
/**
* OpenAPI Petstore
* PHP version 7.2
* PHP version 7.4
*
* @package OpenAPIServer
* @author OpenAPI Generator team

View File

@ -2,7 +2,7 @@
/**
* OpenAPI Petstore
* PHP version 7.2
* PHP version 7.4
*
* @package OpenAPIServer
* @author OpenAPI Generator team

View File

@ -2,7 +2,7 @@
/**
* OpenAPI Petstore
* PHP version 7.2
* PHP version 7.4
*
* @package OpenAPIServer
* @author OpenAPI Generator team

View File

@ -2,7 +2,7 @@
/**
* OpenAPI Petstore
* PHP version 7.2
* PHP version 7.4
*
* @package OpenAPIServer
* @author OpenAPI Generator team