Skip to content

Commit ec35f26

Browse files
authored
Merge pull request #75 from ajanes93/feature/event-support
Feature/ Event Support
2 parents 54e7714 + 8d188ac commit ec35f26

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

config/api-postman.php

+13
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@
7171
],
7272
],
7373

74+
/*
75+
|--------------------------------------------------------------------------
76+
| Events
77+
|--------------------------------------------------------------------------
78+
|
79+
| If you want to configure the prequest and test scripts for the collection,
80+
| then please provide paths to the JavaScript files.
81+
|
82+
*/
83+
84+
'prerequest_script' => '', // This script will execute before every request in the collection.
85+
'test_script' => '', // This script will execute after every request in the collection.
86+
7487
/*
7588
|--------------------------------------------------------------------------
7689
| Enable Form Data

src/Commands/ExportPostmanCommand.php

+23
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,31 @@ protected function initializeStructure(): void
375375
'schema' => 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json',
376376
],
377377
'item' => [],
378+
'event' => [],
378379
];
379380

381+
$prerequestPath = $this->config['prerequest_script'];
382+
$testPath = $this->config['test_script'];
383+
384+
if ($prerequestPath || $testPath) {
385+
$scripts = [
386+
'prerequest' => $prerequestPath,
387+
'test' => $testPath,
388+
];
389+
390+
foreach ($scripts as $type => $path) {
391+
if (file_exists($path)) {
392+
$this->structure['event'][] = [
393+
'listen' => $type,
394+
'script' => [
395+
'type' => 'text/javascript',
396+
'exec' => file_get_contents($path),
397+
],
398+
];
399+
}
400+
}
401+
}
402+
380403
if ($this->token) {
381404
$this->structure['variable'][] = [
382405
'key' => 'token',

tests/Feature/ExportPostmanTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,34 @@ public function test_rules_printing_export_to_human_readable_works()
203203
$this->assertCount(1, $fields->where('key', 'field_9')->where('description', 'The field 9 field is required., The field 9 must be a string.'));
204204
}
205205

206+
public function test_event_export_works()
207+
{
208+
$eventScriptPath = 'tests/Fixtures/ExampleEvent.js';
209+
210+
config([
211+
'api-postman.prerequest_script' => $eventScriptPath,
212+
'api-postman.test_script' => $eventScriptPath,
213+
]);
214+
215+
$this->artisan('export:postman')->assertExitCode(0);
216+
217+
$this->assertTrue(true);
218+
219+
$collection = collect(json_decode(Storage::get('postman/'.config('api-postman.filename')), true)['event']);
220+
221+
$events = $collection
222+
->whereIn('listen', ['prerequest', 'test'])
223+
->all();
224+
225+
$this->assertCount(2, $events);
226+
227+
$content = file_get_contents($eventScriptPath);
228+
229+
foreach ($events as $event) {
230+
$this->assertEquals($event['script']['exec'], $content);
231+
}
232+
}
233+
206234
public function providerFormDataEnabled(): array
207235
{
208236
return [

tests/Fixtures/ExampleEvent.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("example event");

0 commit comments

Comments
 (0)