Skip to content

Commit

Permalink
Refactor tests/add tests for laravel type
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Jun 25, 2022
1 parent e352039 commit d582cc5
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 700 deletions.
3 changes: 2 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
</coverage>
<testsuites>
<testsuite name="Full Test">
<file>tests/GenerateDocumentationTest.php</file>
<file>tests/GenerateDocumentation/OutputTest.php</file>
<file>tests/GenerateDocumentation/BehavioursTest.php</file>
</testsuite>
<testsuite name="Strategies">
<directory>tests/Strategies</directory>
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/GenerateDocumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ protected function upgradeConfigFileIfNeeded(): void
}
}
} catch (\Throwable $e) {
$this->warn("Check failed wih error:");
$this->warn("Check failed with error:");
e::dumpExceptionIfVerbose($e);
$this->warn("This did not affect your docs. Please report this issue in the project repo: https://github.com/knuckleswtf/scribe");
}
Expand Down
23 changes: 14 additions & 9 deletions src/Extracting/Strategies/Responses/UseResponseFileTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,7 @@ public function getFileResponses(array $tags): ?array
$status = $attributes['status'] ?: ($status ?: 200);
$description = $attributes['scenario'] ? "$status, {$attributes['scenario']}" : "$status";

if (!file_exists($filePath)) {
// Try Laravel storage folder
if (!file_exists(storage_path($filePath))) {
throw new \InvalidArgumentException("@responseFile {$filePath} does not exist");
}

$filePath = storage_path($filePath);
}
$content = file_get_contents($filePath, true);
$content = $this->getFileContents($filePath);
if ($json) {
$json = str_replace("'", '"', $json);
$content = json_encode(array_merge(json_decode($content, true), json_decode($json, true)));
Expand All @@ -76,4 +68,17 @@ public function getFileResponses(array $tags): ?array

return $responses;
}

protected function getFileContents($filePath): string|false
{
if (!file_exists($filePath)) {
// Try Laravel storage folder
if (!file_exists(storage_path($filePath))) {
throw new \InvalidArgumentException("@responseFile {$filePath} does not exist");
}

$filePath = storage_path($filePath);
}
return file_get_contents($filePath, true);
}
}
24 changes: 24 additions & 0 deletions src/Scribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Knuckles\Camel\Extraction\ExtractedEndpointData;
use Knuckles\Scribe\Tools\Globals;
use Symfony\Component\HttpFoundation\Request;
use Illuminate\Support\Collection;

class Scribe
{
Expand Down Expand Up @@ -56,4 +57,27 @@ public static function instantiateFormRequestUsing(callable $callable)
{
Globals::$__instantiateFormRequestUsing = $callable;
}

/**
* Customise how Scribe orders your endpoint groups.
* Your callback will be given
*
* @param callable(string,\Illuminate\Routing\Route,\ReflectionFunctionAbstract): mixed $callable
*/
public static function orderGroupsUsing(callable $callable)
{
Globals::$__orderGroupsUsing = $callable;
}

/**
* Customise how Scribe orders endpoints within a group.
* Your callback will be given a Laravel Collection of ExtractedEndpointData objects,
* and should return the sorted collection.
*
* @param callable(Collection<ExtractedEndpointData>): Collection<ExtractedEndpointData> $callable
*/
public static function orderEndpointsInGroupUsing(callable $callable)
{
Globals::$__orderEndpointsInGroupUsing = $callable;
}
}
30 changes: 17 additions & 13 deletions src/Tools/ErrorHandlingUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,26 @@ class ErrorHandlingUtils
{
public static function dumpExceptionIfVerbose(\Throwable $e, $completelySilent = false): void
{
if ($completelySilent) {
return;
}

if (Globals::$shouldBeVerbose) {
self::dumpException($e);
} else if (!$completelySilent) {
[$firstFrame, $secondFrame] = $e->getTrace();

try {
['file' => $file, 'line' => $line] = $firstFrame;
} catch (\Exception $_) {
['file' => $file, 'line' => $line] = $secondFrame;
}
$exceptionType = get_class($e);
$message = $e->getMessage();
$message = "$exceptionType in $file at line $line: $message";
ConsoleOutputUtils::error($message);
ConsoleOutputUtils::error('Run this again with the --verbose flag to see the full stack trace.');
return;
}
[$firstFrame, $secondFrame] = $e->getTrace();

try {
['file' => $file, 'line' => $line] = $firstFrame;
} catch (\Exception $_) {
['file' => $file, 'line' => $line] = $secondFrame;
}
$exceptionType = get_class($e);
$message = $e->getMessage();
$message = "$exceptionType in $file at line $line: $message";
ConsoleOutputUtils::error($message);
ConsoleOutputUtils::error('Run this again with the --verbose flag to see the full stack trace.');

}

Expand Down
8 changes: 8 additions & 0 deletions src/Tools/Globals.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ class Globals

public static bool $shouldBeVerbose = false;

/*
* Hooks, used by users to configure Scribe's behaviour.
*/

public static $__beforeResponseCall;

public static $__afterGenerating;

public static $__instantiateFormRequestUsing;

public static $__orderEndpointsInGroupUsing;

public static $__orderGroupsUsing;
}
8 changes: 5 additions & 3 deletions src/Writing/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ protected function writePostmanCollection(array $groups): void
}

c::success("Wrote Postman collection to: {$collectionPath}");
$this->generatedFiles['postman'] = realpath($collectionPath);
$this->generatedFiles['postman'] = realpath($collectionPath)
?: Storage::disk('local')->path('scribe/collection.json');
}
}

Expand All @@ -102,7 +103,8 @@ protected function writeOpenAPISpec(array $parsedRoutes): void
}

c::success("Wrote OpenAPI specification to: {$specPath}");
$this->generatedFiles['openapi'] = realpath($specPath);
$this->generatedFiles['openapi'] = realpath($specPath)
?: Storage::disk('local')->path('scribe/openapi.yaml');
}
}

Expand Down Expand Up @@ -200,7 +202,7 @@ public function writeHtmlDocs(array $groupedEndpoints): void
$outputPath = rtrim($this->laravelTypeOutputPath, '/') . '/';
c::success("Wrote Blade docs to: $outputPath");
$this->generatedFiles['blade'] = realpath("{$outputPath}index.blade.php");
$assetsOutputPath = app()->get('path.public') . $this->laravelAssetsPath;
$assetsOutputPath = app()->get('path.public') . $this->laravelAssetsPath . '/';
c::success("Wrote Laravel assets to: " . realpath($assetsOutputPath));
}
$this->generatedFiles['assets']['js'] = realpath("{$assetsOutputPath}js");
Expand Down
Loading

0 comments on commit d582cc5

Please sign in to comment.