Skip to content

Commit

Permalink
Merge pull request #75 from ajanes93/feature/event-support
Browse files Browse the repository at this point in the history
Feature/ Event Support
  • Loading branch information
tomirons committed Sep 2, 2022
2 parents 54e7714 + 8d188ac commit ec35f26
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
13 changes: 13 additions & 0 deletions config/api-postman.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@
],
],

/*
|--------------------------------------------------------------------------
| Events
|--------------------------------------------------------------------------
|
| If you want to configure the prequest and test scripts for the collection,
| then please provide paths to the JavaScript files.
|
*/

'prerequest_script' => '', // This script will execute before every request in the collection.
'test_script' => '', // This script will execute after every request in the collection.

/*
|--------------------------------------------------------------------------
| Enable Form Data
Expand Down
23 changes: 23 additions & 0 deletions src/Commands/ExportPostmanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,31 @@ protected function initializeStructure(): void
'schema' => 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json',
],
'item' => [],
'event' => [],
];

$prerequestPath = $this->config['prerequest_script'];
$testPath = $this->config['test_script'];

if ($prerequestPath || $testPath) {
$scripts = [
'prerequest' => $prerequestPath,
'test' => $testPath,
];

foreach ($scripts as $type => $path) {
if (file_exists($path)) {
$this->structure['event'][] = [
'listen' => $type,
'script' => [
'type' => 'text/javascript',
'exec' => file_get_contents($path),
],
];
}
}
}

if ($this->token) {
$this->structure['variable'][] = [
'key' => 'token',
Expand Down
28 changes: 28 additions & 0 deletions tests/Feature/ExportPostmanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,34 @@ public function test_rules_printing_export_to_human_readable_works()
$this->assertCount(1, $fields->where('key', 'field_9')->where('description', 'The field 9 field is required., The field 9 must be a string.'));
}

public function test_event_export_works()
{
$eventScriptPath = 'tests/Fixtures/ExampleEvent.js';

config([
'api-postman.prerequest_script' => $eventScriptPath,
'api-postman.test_script' => $eventScriptPath,
]);

$this->artisan('export:postman')->assertExitCode(0);

$this->assertTrue(true);

$collection = collect(json_decode(Storage::get('postman/'.config('api-postman.filename')), true)['event']);

$events = $collection
->whereIn('listen', ['prerequest', 'test'])
->all();

$this->assertCount(2, $events);

$content = file_get_contents($eventScriptPath);

foreach ($events as $event) {
$this->assertEquals($event['script']['exec'], $content);
}
}

public function providerFormDataEnabled(): array
{
return [
Expand Down
1 change: 1 addition & 0 deletions tests/Fixtures/ExampleEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("example event");

0 comments on commit ec35f26

Please sign in to comment.