Skip to content

Commit 2b9fc9a

Browse files
committed
add includeBoilerplate option
add includeBoilerplate option
1 parent 992549b commit 2b9fc9a

File tree

7 files changed

+157
-51
lines changed

7 files changed

+157
-51
lines changed

codegens/php-guzzle/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@ typings/
3939
.env
4040

4141
out/
42+
43+
codesnippet.php

codegens/php-guzzle/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Convert function takes three parameters
1717
* `indentCount` - The number of indentation characters to add per code level
1818
* `trimRequestBody` - Whether or not request body fields should be trimmed
1919
* `asyncType` - String denoting call types. eg: 'async', 'sync'
20+
* `includeBoilerplate` - Include class definition and import statements in snippet
2021

2122
* `callback` - callback function with first parameter as error and second parameter as string for code snippet
2223

codegens/php-guzzle/lib/options.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ const options = [
4646
availableOptions: ['async', 'sync'],
4747
default: 'async',
4848
description: 'Set if the requests will be asynchronous or synchronous'
49+
},
50+
{
51+
name: 'Include boilerplate',
52+
id: 'includeBoilerplate',
53+
type: 'boolean',
54+
default: false,
55+
description: 'Include class definition and import statements in snippet'
4956
}
5057
];
5158

codegens/php-guzzle/lib/phpGuzzle.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,21 @@ function validateIsFunction (validateFunction) {
133133
* Returns the snippet header
134134
*
135135
* @module convert
136-
*
136+
* @param {string} includeBoilerplate - wheter to include the boilerplate
137137
* @returns {string} the snippet headers (uses)
138138
*/
139-
function getSnippetHeader () {
140-
return '<?php\n' +
141-
'$composerHome = substr(shell_exec(\'composer config home -g\'), 0, -1).\'/vendor/autoload.php\';\n' +
142-
'require $composerHome; // your path to autoload.php \n' +
143-
'use Psr\\Http\\Message\\ResponseInterface;\n' +
144-
'use GuzzleHttp\\Exception\\RequestException;\n' +
145-
'use GuzzleHttp\\Client;\n' +
146-
'use GuzzleHttp\\Psr7\\Utils;\n' +
147-
'use GuzzleHttp\\Psr7\\Request;\n';
139+
function getSnippetBoilerplate (includeBoilerplate) {
140+
if (includeBoilerplate) {
141+
return '<?php\n' +
142+
'$composerHome = substr(shell_exec(\'composer config home -g\'), 0, -1).\'/vendor/autoload.php\';\n' +
143+
'require $composerHome; // your path to autoload.php \n' +
144+
'use Psr\\Http\\Message\\ResponseInterface;\n' +
145+
'use GuzzleHttp\\Exception\\RequestException;\n' +
146+
'use GuzzleHttp\\Client;\n' +
147+
'use GuzzleHttp\\Psr7\\Utils;\n' +
148+
'use GuzzleHttp\\Psr7\\Request;\n';
149+
}
150+
return '';
148151
}
149152

150153
/**
@@ -288,6 +291,20 @@ function includeRequestOptions (snippetBody) {
288291
return snippetBody.startsWith('$options');
289292
}
290293

294+
/**
295+
* Gets the defined indentation from options
296+
*
297+
* @param {object} options - process options
298+
* @returns {String} - indentation characters
299+
*/
300+
function getIncludeBoilerplate (options) {
301+
if (options && options.includeBoilerplate !== undefined && options.includeBoilerplate !== null) {
302+
return options.includeBoilerplate;
303+
}
304+
return false;
305+
}
306+
307+
291308
/**
292309
* Used to convert the postman sdk-request object in PHP-Guzzle request snippet
293310
*
@@ -313,9 +330,10 @@ function convert (request, options, callback) {
313330

314331
const method = getRequestMethod(request),
315332
indentation = getIndentation(options),
333+
includeBoilerplate = getIncludeBoilerplate(options),
316334
url = getRequestURL(request),
317335
snippetHeaders = getSnippetHeaders(getRequestHeaders(request), indentation),
318-
snippetHeader = getSnippetHeader(),
336+
snippetHeader = getSnippetBoilerplate(includeBoilerplate),
319337
snippetClient = getSnippetClient(options);
320338
snippetbody = parseBody(request.body, indentation, getBodyTrim(options), request.headers.get('Content-Type'));
321339
hasBody = includeBody(request, snippetbody);
@@ -345,13 +363,15 @@ module.exports = {
345363
convert,
346364
getHeaders: getRequestHeaders,
347365
getSnippetHeaders,
366+
getSnippetBoilerplate,
348367
getURL: getRequestURL,
349368
getMethod: getRequestMethod,
350369
getIndentation,
351370
getSnippetClient,
352371
getSnippetFooter,
353372
getSnippetRequestObject,
354-
groupHeadersSameKey
373+
groupHeadersSameKey,
374+
getIncludeBoilerplate
355375
};
356376

357377

codegens/php-guzzle/test/newman/newman.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ describe('PHP-Guzzle Converter', function () {
1313
options = {
1414
indentType: 'Space',
1515
indentCount: 4,
16-
asyncType: 'sync'
16+
asyncType: 'sync',
17+
includeBoilerplate: true
1718
};
1819
runNewmanTest(convert, options, testConfig);
1920
});
@@ -26,7 +27,8 @@ describe('PHP-Guzzle Converter', function () {
2627
options = {
2728
indentType: 'Space',
2829
indentCount: 4,
29-
asyncType: 'async'
30+
asyncType: 'async',
31+
includeBoilerplate: true
3032
};
3133
runNewmanTest(convert, options, testConfig);
3234
});

codegens/php-guzzle/test/unit/phpGuzzle.test.js

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ var expect = require('chai').expect,
1212
convert,
1313
getSnippetFooter,
1414
getSnippetRequestObject,
15-
groupHeadersSameKey
15+
groupHeadersSameKey,
16+
getSnippetBoilerplate,
17+
getIncludeBoilerplate
1618
} = require('../../lib/phpGuzzle'),
1719
collectionsPath = './fixtures';
1820

@@ -21,7 +23,7 @@ describe('convert function', function () {
2123
it('should convert a simple get request', function (done) {
2224
const collection = new sdk.Collection(JSON.parse(
2325
fs.readFileSync(path.resolve(__dirname, collectionsPath, './sample_collection.json').toString())));
24-
convert(collection.items.members[0].request, {}, function (err, snippet) {
26+
convert(collection.items.members[0].request, {includeBoilerplate: true}, function (err, snippet) {
2527
if (err) {
2628
console.error(err);
2729
}
@@ -34,6 +36,19 @@ describe('convert function', function () {
3436
expect(function () { convert({}, {}); })
3537
.to.throw('Php-Guzzle~convert: Callback is not a function');
3638
});
39+
40+
it('should convert a simple get request without boilerplate', function (done) {
41+
const collection = new sdk.Collection(JSON.parse(
42+
fs.readFileSync(path.resolve(__dirname, collectionsPath, './sample_collection.json').toString())));
43+
convert(collection.items.members[0].request, {includeBoilerplate: false}, function (err, snippet) {
44+
if (err) {
45+
console.error(err);
46+
}
47+
expect(snippet).to.not.be.empty;
48+
expect(snippet).to.not.include('use');
49+
});
50+
done();
51+
});
3752
});
3853

3954
describe('getHeaders function', function () {
@@ -250,3 +265,59 @@ describe('groupHeadersSameKey method', function () {
250265
expect(result[0].key).to.equal('key1');
251266
});
252267
});
268+
269+
describe('getSnippetBoilerplate method', function () {
270+
it('should the boilerplate with include option in true"', function () {
271+
const expected = '<?php\n' +
272+
'$composerHome = substr(shell_exec(\'composer config home -g\'), 0, -1).\'/vendor/autoload.php\';\n' +
273+
'require $composerHome; // your path to autoload.php \n' +
274+
'use Psr\\Http\\Message\\ResponseInterface;\n' +
275+
'use GuzzleHttp\\Exception\\RequestException;\n' +
276+
'use GuzzleHttp\\Client;\n' +
277+
'use GuzzleHttp\\Psr7\\Utils;\n' +
278+
'use GuzzleHttp\\Psr7\\Request;\n',
279+
result = getSnippetBoilerplate(true);
280+
expect(result).to.equal(expected);
281+
});
282+
283+
it('should return empty string for include option in false', function () {
284+
const expected = '',
285+
result = getSnippetBoilerplate(false);
286+
expect(result).to.equal(expected);
287+
});
288+
});
289+
290+
describe('getIncludeBoilerplate method', function () {
291+
it('should return false with empty options', function () {
292+
const result = getIncludeBoilerplate({});
293+
expect(result).to.be.false;
294+
});
295+
it('should return false with undefined options', function () {
296+
const result = getIncludeBoilerplate();
297+
expect(result).to.be.false;
298+
});
299+
it('should return false with null options', function () {
300+
const result = getIncludeBoilerplate(null);
301+
expect(result).to.be.false;
302+
});
303+
it('should return false with options and include option not present', function () {
304+
const result = getIncludeBoilerplate({asyncType: 'sync'});
305+
expect(result).to.be.false;
306+
});
307+
it('should return false with options and include option present with value of false', function () {
308+
const result = getIncludeBoilerplate({includeBoilerplate: false});
309+
expect(result).to.be.false;
310+
});
311+
it('should return false with options and include option present with value of false', function () {
312+
const result = getIncludeBoilerplate({includeBoilerplate: true});
313+
expect(result).to.be.true;
314+
});
315+
it('should return false with options and include option present with value of false', function () {
316+
const result = getIncludeBoilerplate({includeBoilerplate: undefined});
317+
expect(result).to.be.false;
318+
});
319+
it('should return false with options and include option present with value of false', function () {
320+
const result = getIncludeBoilerplate({includeBoilerplate: null});
321+
expect(result).to.be.false;
322+
});
323+
});
Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,47 @@
11
{
2-
"info": {
3-
"_postman_id": "7da8f1cd-aeb0-420f-a989-d47db46b44b0",
4-
"name": "Multipart/form-data FILE",
5-
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
6-
},
7-
"item": [
8-
{
9-
"name": "Single/multiple file upload via form-data",
10-
"request": {
11-
"method": "POST",
12-
"header": [],
13-
"body": {
14-
"mode": "formdata",
15-
"formdata": [
16-
{
2+
"info": {
3+
"_postman_id": "7da8f1cd-aeb0-420f-a989-d47db46b44b0",
4+
"name": "Multipart/form-data FILE",
5+
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
6+
},
7+
"item": [
8+
{
9+
"name": "Single/multiple file upload via form-data",
10+
"request": {
11+
"method": "POST",
12+
"header": [],
13+
"body": {
14+
"mode": "formdata",
15+
"formdata": [
16+
{
1717
"key": "single file",
1818
"value": "",
1919
"type": "file",
20-
"src": "<file path>"
21-
},
22-
{
20+
"src": "/Users/luis.tejeda/Documents/Source/GitHub/postmanlabs/postman-code-generators/dummyFile1.txt"
21+
},
22+
{
2323
"key": "multiple files",
2424
"value": "",
2525
"type": "file",
26-
"src": ["<file path 1>","<file path 2>"]
26+
"src": [
27+
"/Users/luis.tejeda/Documents/Source/GitHub/postmanlabs/postman-code-generators/dummyFile2.txt",
28+
"/Users/luis.tejeda/Documents/Source/GitHub/postmanlabs/postman-code-generators/dummyFile3.txt"
29+
]
2730
}
28-
]
29-
},
30-
"url": {
31-
"raw": "https://postman-echo.com/post",
32-
"protocol": "https",
33-
"host": [
34-
"postman-echo",
35-
"com"
36-
],
37-
"path": [
38-
"post"
39-
]
40-
}
41-
}
31+
]
32+
},
33+
"url": {
34+
"raw": "https://postman-echo.com/post",
35+
"protocol": "https",
36+
"host": [
37+
"postman-echo",
38+
"com"
39+
],
40+
"path": [
41+
"post"
42+
]
43+
}
44+
}
4245
},
4346
{
4447
"name": "Binary file upload via form-data",
@@ -52,7 +55,7 @@
5255
"key": "binary file",
5356
"value": "",
5457
"type": "file",
55-
"src": "<file path>"
58+
"src": "/Users/luis.tejeda/Documents/Source/GitHub/postmanlabs/postman-code-generators/dummyBinaryFile"
5659
}
5760
]
5861
},
@@ -69,5 +72,5 @@
6972
}
7073
}
7174
}
72-
]
75+
]
7376
}

0 commit comments

Comments
 (0)