Skip to content

added continue-on-errors option for use in specific cases such as end… #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ The following usage will generate routes with the basic auth specified.
php artisan export:postman --basic="username:password123"
```

The following usage will continue until the end, even if it has error or incompatible endpoints.

```bash
php artisan export:postman --continue-on-errors
```

If both auths are specified, bearer will be favored.

## Examples
Expand Down
12 changes: 11 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
backupGlobals="false"
colors="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
>
<coverage/>
<testsuites>
<testsuite name="Unit">
Expand Down
32 changes: 29 additions & 3 deletions src/Commands/ExportPostmanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
Expand All @@ -27,7 +28,8 @@ class ExportPostmanCommand extends Command
/** @var string */
protected $signature = 'export:postman
{--bearer= : The bearer token to use on your endpoints}
{--basic= : The basic auth to use on your endpoints}';
{--basic= : The basic auth to use on your endpoints}
{--continue-on-errors : Continues even if an error occurs in any route, middleware or method (boolean)}';

/** @var string */
protected $description = 'Automatically generate a Postman collection for your API routes';
Expand Down Expand Up @@ -62,6 +64,9 @@ class ExportPostmanCommand extends Command
'basic',
];

/** @var bool */
private $stopOnErrors = true;

/** @var \Illuminate\Validation\Validator */
private $validator;

Expand All @@ -77,12 +82,14 @@ public function handle(): void
{
$this->setFilename();
$this->setAuthToken();
$this->setOptions();
$this->initializeStructure();
$this->initializePhpDocParser();

foreach ($this->router->getRoutes() as $route) {
$methods = array_filter($route->methods(), fn ($value) => $value !== 'HEAD');
$middlewares = $route->gatherMiddleware();

$middlewares = $this->handleCallable(fn () => $route->gatherMiddleware());

foreach ($methods as $method) {
$includedMiddleware = false;
Expand All @@ -103,7 +110,7 @@ public function handle(): void

$routeAction = $route->getAction();

$reflectionMethod = $this->getReflectionMethod($routeAction);
$reflectionMethod = $this->handleCallable(fn () => $this->getReflectionMethod($routeAction));

if (! $reflectionMethod) {
continue;
Expand Down Expand Up @@ -223,6 +230,18 @@ public function handle(): void
$this->info('Postman Collection Exported: '.storage_path('app/'.$exportName));
}

protected function handleCallable($callable)
{
try {
return $callable();
} catch (\Throwable $th) {
if ($this->stopOnErrors) {
throw $th;
}
Log::error($th->getMessage()."\n[stacktrace]\n".$th->getTraceAsString()."\n");
}
}

protected function getReflectionMethod(array $routeAction): ?object
{
// Hydrates the closure if it is an instance of Opis\Closure\SerializableClosure
Expand Down Expand Up @@ -489,6 +508,13 @@ protected function setAuthToken()
}
}

protected function setOptions()
{
if ($this->option('continue-on-errors') === true) {
$this->stopOnErrors = false;
}
}

protected function isStructured()
{
return $this->config['structured'];
Expand Down
26 changes: 26 additions & 0 deletions tests/Feature/ExportPostmanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,32 @@ public function test_basic_export_works(bool $formDataEnabled)
}
}

public function test_continue_on_errors_works()
{
$router = $this->app['router'];

$router->middleware('api')->group(function ($router) {
$router->get('endpoint/with/error', [NonExistentController::class, 'index']);
$router->get('endpoint/without/error', function () {
return 'index';
});
});

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

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

$collectionRoute = Arr::first($collection['item'], function ($item) {
return $item['name'] == 'endpoint/without/error';
});
$this->assertNotNull($collectionRoute);

$collectionRoute = Arr::first($collection['item'], function ($item) {
return $item['name'] == 'endpoint/with/error';
});
$this->assertNull($collectionRoute);
}

/**
* @dataProvider providerFormDataEnabled
*/
Expand Down