Skip to content

Commit 925ae68

Browse files
ybelenkoackintosh
authored andcommitted
[Slim] Generation of UnitTest stubs (#566)
* [Slim] Add PHPUnit4 to Composer * [Slim] Configure PHPUnit * [Slim] Add Model and Api test templates * [Slim] Refresh Slim samples
1 parent d863c3e commit 925ae68

62 files changed

Lines changed: 5877 additions & 9 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpSlimServerCodegen.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ public PhpSlimServerCodegen() {
5353
modelPackage = invokerPackage + "\\" + modelDirName;
5454
outputFolder = "generated-code" + File.separator + "slim";
5555

56-
// no test files
57-
apiTestTemplateFiles.clear();
56+
modelTestTemplateFiles.put("model_test.mustache", ".php");
5857
// no doc files
5958
modelDocTemplateFiles.clear();
6059
apiDocTemplateFiles.clear();
@@ -118,6 +117,7 @@ public void processOpts() {
118117
supportingFiles.add(new SupportingFile(".gitignore", getPackagePath(), ".gitignore"));
119118
supportingFiles.add(new SupportingFile("AbstractApiController.mustache", toSrcPath(invokerPackage, srcBasePath), "AbstractApiController.php"));
120119
supportingFiles.add(new SupportingFile("SlimRouter.mustache", toSrcPath(invokerPackage, srcBasePath), "SlimRouter.php"));
120+
supportingFiles.add(new SupportingFile("phpunit.xml.mustache", getPackagePath(), "phpunit.xml.dist"));
121121
}
122122

123123
@Override
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* {{classname}}Test
4+
* PHP version 5
5+
*
6+
* @category Class
7+
* @package {{apiPackage}}
8+
* @author OpenAPI Generator team
9+
* @link https://github.com/openapitools/openapi-generator
10+
*/
11+
12+
/**
13+
{{#appName}}
14+
* {{{appName}}}
15+
*
16+
{{/appName}}
17+
{{#appDescription}}
18+
* {{{appDescription}}}
19+
{{/appDescription}}
20+
{{#version}}
21+
* OpenAPI spec version: {{{version}}}
22+
{{/version}}
23+
{{#infoEmail}}
24+
* Contact: {{{infoEmail}}}
25+
{{/infoEmail}}
26+
* Generated by: https://github.com/openapitools/openapi-generator.git
27+
*/
28+
29+
/**
30+
* NOTE: This class is auto generated by the openapi generator program.
31+
* https://github.com/openapitools/openapi-generator
32+
* Please update the test case below to test the endpoint.
33+
*/
34+
namespace {{apiPackage}};
35+
36+
use {{apiPackage}}\{{classname}};
37+
38+
/**
39+
* {{classname}}Test Class Doc Comment
40+
*
41+
* @category Class
42+
* @description {{#description}}{{description}}{{/description}}{{^description}}{{classname}}{{/description}}
43+
* @package {{apiPackage}}
44+
* @author OpenAPI Generator team
45+
* @link https://github.com/openapitools/openapi-generator
46+
* @coversDefaultClass \{{modelPackage}}\{{classname}}
47+
*/
48+
{{#operations}}class {{classname}}Test extends \PHPUnit_Framework_TestCase {
49+
50+
/**
51+
* Setup before running any test cases
52+
*/
53+
public static function setUpBeforeClass() {
54+
55+
}
56+
57+
/**
58+
* Setup before running each test case
59+
*/
60+
public function setUp() {
61+
62+
}
63+
64+
/**
65+
* Clean up after running each test case
66+
*/
67+
public function tearDown() {
68+
69+
}
70+
71+
/**
72+
* Clean up after running all test cases
73+
*/
74+
public static function tearDownAfterClass() {
75+
76+
}
77+
{{#operation}}
78+
79+
/**
80+
* Test case for {{{operationId}}}
81+
*
82+
* {{{summary}}}.
83+
* @covers ::{{{operationId}}}
84+
*/
85+
public function test{{operationIdCamelCase}}() {
86+
87+
}
88+
{{/operation}}
89+
}
90+
{{/operations}}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
{
22
"minimum-stability": "RC",
33
"require": {
4+
"php": ">=5.5",
45
"slim/slim": "3.*"
56
},
7+
"require-dev": {
8+
"phpunit/phpunit": "^4.8"
9+
},
610
"autoload": {
711
"psr-4": { "{{escapedInvokerPackage}}\\": "{{srcBasePath}}/" }
12+
},
13+
"autoload-dev": {
14+
"psr-4": { "{{escapedInvokerPackage}}\\": "{{testBasePath}}/" }
15+
},
16+
"scripts": {
17+
"test": [
18+
"@test-apis",
19+
"@test-models"
20+
],
21+
"test-apis": "phpunit --testsuite Apis",
22+
"test-models": "phpunit --testsuite Models"
823
}
924
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
{{#models}}
3+
{{#model}}
4+
/**
5+
* {{classname}}Test
6+
* PHP version 5
7+
*
8+
* @category Class
9+
* @package {{modelPackage}}
10+
* @author OpenAPI Generator team
11+
* @link https://github.com/openapitools/openapi-generator
12+
*/
13+
14+
/**
15+
{{#appName}}
16+
* {{{appName}}}
17+
*
18+
{{/appName}}
19+
{{#appDescription}}
20+
* {{{appDescription}}}
21+
{{/appDescription}}
22+
{{#version}}
23+
* OpenAPI spec version: {{{version}}}
24+
{{/version}}
25+
{{#infoEmail}}
26+
* Contact: {{{infoEmail}}}
27+
{{/infoEmail}}
28+
* Generated by: https://github.com/openapitools/openapi-generator.git
29+
*/
30+
31+
/**
32+
* NOTE: This class is auto generated by the openapi generator program.
33+
* https://github.com/openapitools/openapi-generator
34+
* Please update the test case below to test the model.
35+
*/
36+
namespace {{modelPackage}};
37+
38+
use {{modelPackage}}\{{classname}};
39+
40+
/**
41+
* {{classname}}Test Class Doc Comment
42+
*
43+
* @category Class
44+
* @description {{#description}}{{description}}{{/description}}{{^description}}{{classname}}{{/description}}
45+
* @package {{modelPackage}}
46+
* @author OpenAPI Generator team
47+
* @link https://github.com/openapitools/openapi-generator
48+
* @coversDefaultClass \{{modelPackage}}\{{classname}}
49+
*/
50+
class {{classname}}Test extends \PHPUnit_Framework_TestCase {
51+
52+
/**
53+
* Setup before running any test cases
54+
*/
55+
public static function setUpBeforeClass() {
56+
57+
}
58+
59+
/**
60+
* Setup before running each test case
61+
*/
62+
public function setUp() {
63+
64+
}
65+
66+
/**
67+
* Clean up after running each test case
68+
*/
69+
public function tearDown() {
70+
71+
}
72+
73+
/**
74+
* Clean up after running all test cases
75+
*/
76+
public static function tearDownAfterClass() {
77+
78+
}
79+
80+
/**
81+
* Test "{{classname}}"
82+
*/
83+
public function test{{classname}}() {
84+
$test{{classname}} = new {{classname}}();
85+
}
86+
{{#vars}}
87+
88+
/**
89+
* Test attribute "{{name}}"
90+
*/
91+
public function testProperty{{nameInCamelCase}}() {
92+
93+
}
94+
{{/vars}}
95+
}
96+
{{/model}}{{/models}}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/4.8/phpunit.xsd"
5+
bootstrap="./vendor/autoload.php"
6+
colors="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
stopOnFailure="false">
11+
>
12+
<testsuites>
13+
<testsuite name="Apis">
14+
<directory>{{apiTestPath}}</directory>
15+
</testsuite>
16+
<testsuite name="Models">
17+
<directory>{{modelTestPath}}</directory>
18+
</testsuite>
19+
</testsuites>
20+
<filter>
21+
<whitelist processUncoveredFilesFromWhitelist="true">
22+
<directory suffix=".php">{{apiSrcPath}}</directory>
23+
<directory suffix=".php">{{modelSrcPath}}</directory>
24+
</whitelist>
25+
</filter>
26+
</phpunit>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.1.0-SNAPSHOT
1+
3.1.1-SNAPSHOT
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
{
22
"minimum-stability": "RC",
33
"require": {
4+
"php": ">=5.5",
45
"slim/slim": "3.*"
56
},
7+
"require-dev": {
8+
"phpunit/phpunit": "^4.8"
9+
},
610
"autoload": {
711
"psr-4": { "OpenAPIServer\\": "lib/" }
12+
},
13+
"autoload-dev": {
14+
"psr-4": { "OpenAPIServer\\": "test/" }
15+
},
16+
"scripts": {
17+
"test": [
18+
"@test-apis",
19+
"@test-models"
20+
],
21+
"test-apis": "phpunit --testsuite Apis",
22+
"test-models": "phpunit --testsuite Models"
823
}
924
}

samples/server/petstore-security-test/php-slim/lib/SlimRouter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class SlimRouter {
5757
public function __construct($container = []) {
5858
$app = new App($container);
5959

60-
$app->PUT('/fake', FakeApi::class . ':testCodeInjectEndRnNR');
60+
$app->PUT('/ ' \" =end -- \\r\\n \\n \\r/v2 ' \" =end -- \\r\\n \\n \\r/fake', FakeApi::class . ':testCodeInjectEndRnNR');
6161

6262
$this->slimApp = $app;
6363
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/4.8/phpunit.xsd"
5+
bootstrap="./vendor/autoload.php"
6+
colors="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
stopOnFailure="false">
11+
>
12+
<testsuites>
13+
<testsuite name="Apis">
14+
<directory>.\test\Api</directory>
15+
</testsuite>
16+
<testsuite name="Models">
17+
<directory>.\test\Model</directory>
18+
</testsuite>
19+
</testsuites>
20+
<filter>
21+
<whitelist processUncoveredFilesFromWhitelist="true">
22+
<directory suffix=".php">.\lib\Api</directory>
23+
<directory suffix=".php">.\lib\Model</directory>
24+
</whitelist>
25+
</filter>
26+
</phpunit>

0 commit comments

Comments
 (0)