Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Aug 12, 2022
1 parent 895ae58 commit 136ce71
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Knuckles\Scribe\Extracting\Shared\ResponseFieldTools;
use Knuckles\Scribe\Extracting\Strategies\GetFieldsFromTagStrategy;
use Mpociot\Reflection\DocBlock;

class GetFromResponseFieldTag extends GetFieldsFromTagStrategy
{
Expand Down Expand Up @@ -42,21 +43,20 @@ protected function parseTag(string $tagContent): array
return $data;
}

/**
* Get api resource tag.
*
* @param Tag[] $tags
*
* @return Tag|null
*/
public function getApiResourceTag(array $tags): ?Tag
public function getFromTags(array $tagsOnMethod, array $tagsOnClass = []): array
{
$apiResourceTags = array_values(
array_filter($tags, function ($tag) {
return ($tag instanceof Tag) && in_array(strtolower($tag->getName()), ['apiresource', 'apiresourcecollection']);
array_filter($tagsOnMethod, function ($tag) {
return in_array(strtolower($tag->getName()), ['apiresource', 'apiresourcecollection']);
})
);

return empty($apiResourceTags) ? null : $apiResourceTags[0];
if ($apiResourceTags && !empty($className = $apiResourceTags[0]->getContent())) {
$method = u::getReflectedRouteMethod([$className, 'toArray']);
$docBlock = new DocBlock($method->getDocComment() ?: '');
$tagsOnApiResource = $docBlock->getTags();
}

return parent::getFromTags(array_merge($tagsOnMethod, $tagsOnApiResource ?? []), $tagsOnClass);
}
}
31 changes: 20 additions & 11 deletions src/Writing/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct(DocumentationConfig $config = null, $docsName = 'scr

$this->laravelAssetsPath = $this->config->get('laravel.assets_directory')
? '/' . $this->config->get('laravel.assets_directory')
: '/vendor/scribe';
: "/vendor/$this->docsName";
}

/**
Expand Down Expand Up @@ -87,12 +87,11 @@ protected function writePostmanCollection(array $groups): void
file_put_contents($collectionPath, $collection);
} else {
Storage::disk('local')->put("{$this->docsName}/collection.json", $collection);
$collectionPath = "storage/app/{$this->docsName}/collection.json";
$collectionPath = Storage::disk('local')->path("$this->docsName/collection.json");
}

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

Expand All @@ -107,12 +106,11 @@ protected function writeOpenAPISpec(array $parsedRoutes): void
file_put_contents($specPath, $spec);
} else {
Storage::disk('local')->put("{$this->docsName}/openapi.yaml", $spec);
$specPath = "storage/app/{$this->docsName}/openapi.yaml";
$specPath = Storage::disk('local')->path("$this->docsName/openapi.yaml");
}

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

Expand Down Expand Up @@ -208,10 +206,10 @@ public function writeHtmlDocs(array $groupedEndpoints): void
$assetsOutputPath = $outputPath;
} else {
$outputPath = rtrim($this->laravelTypeOutputPath, '/') . '/';
c::success("Wrote Blade docs to: $outputPath");
c::success("Wrote Blade docs to: ". $this->makePathFriendly($outputPath));
$this->generatedFiles['blade'] = realpath("{$outputPath}index.blade.php");
$assetsOutputPath = app()->get('path.public') . $this->laravelAssetsPath . '/';
c::success("Wrote Laravel assets to: " . realpath($assetsOutputPath));
c::success("Wrote Laravel assets to: " . $this->makePathFriendly($assetsOutputPath));
}
$this->generatedFiles['assets']['js'] = realpath("{$assetsOutputPath}js");
$this->generatedFiles['assets']['css'] = realpath("{$assetsOutputPath}css");
Expand All @@ -233,4 +231,15 @@ protected function getLaravelTypeOutputPath(): ?string
return config('view.paths.0', function_exists('base_path') ? base_path("resources/views") : "resources/views")."/$this->docsName";
}

/**
* Turn a path from (possibly) C:\projects\myapp\resources\views
* or /projects/myapp/resources/views to resources/views ie:
* - make it relative to PWD
* - normalise all slashes to forward slashes
*/
protected function makePathFriendly(string $path): string
{
return str_replace("\\", "/", str_replace(getcwd() . DIRECTORY_SEPARATOR, "", $path));
}

}
18 changes: 13 additions & 5 deletions tests/GenerateDocumentation/BehavioursTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,20 @@ public function can_customise_static_output_path()
Utils::deleteDirectoryAndContents('static/');
}

protected function generateAndExpectConsoleOutput(string ...$expectedOutput): void
/** @test */
public function checks_for_upgrades_after_run_unless_disabled()
{
$output = $this->generate();
file_put_contents("config/scribe_test.php", str_replace("'logo' => false,", "", file_get_contents("config/scribe.php")));
config(["scribe_test" => require "config/scribe_test.php"]);

$output = $this->artisan('scribe:generate', ['--config' => 'scribe_test']);
$this->assertStringContainsString("Checking for any pending upgrades to your config file...", $output);
$this->assertStringContainsString("`logo` will be added", $output);

$output = $this->artisan('scribe:generate', ['--config' => 'scribe_test', '--no-upgrade-check' => true]);
$this->assertStringNotContainsString("Checking for any pending upgrades to your config file...", $output);

foreach ($expectedOutput as $expected) {
$this->assertStringContainsString($expected, $output);
}
unlink("config/scribe_test.php");
Utils::deleteDirectoryAndContents(".scribe_test");
}
}
31 changes: 27 additions & 4 deletions tests/GenerateDocumentation/OutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ public function generates_laravel_type_output()
config(['scribe.postman.enabled' => true]);
config(['scribe.openapi.enabled' => true]);

$this->generate();
$this->generateAndExpectConsoleOutput(
"Wrote Blade docs to: vendor/orchestra/testbench-core/laravel/resources/views/scribe",
"Wrote Laravel assets to: vendor/orchestra/testbench-core/laravel/public/vendor/scribe",
"Wrote Postman collection to: vendor/orchestra/testbench-core/laravel/storage/app/scribe/collection.json",
"Wrote OpenAPI specification to: vendor/orchestra/testbench-core/laravel/storage/app/scribe/openapi.yaml",
);

$this->assertFileExists($this->postmanOutputPath(true));
$this->assertFileExists($this->openapiOutputPath(true));
Expand Down Expand Up @@ -142,7 +147,19 @@ public function supports_multi_docs_in_laravel_type_output()
config(['scribe_admin.postman.enabled' => true]);
config(['scribe_admin.openapi.enabled' => true]);

$this->generate(["--config" => "scribe_admin"]);
$output = $this->generate(["--config" => "scribe_admin"]);
$this->assertStringContainsString(
"Wrote Blade docs to: vendor/orchestra/testbench-core/laravel/resources/views/scribe_admin", $output
);
$this->assertStringContainsString(
"Wrote Laravel assets to: vendor/orchestra/testbench-core/laravel/public/vendor/scribe_admin", $output
);
$this->assertStringContainsString(
"Wrote Postman collection to: vendor/orchestra/testbench-core/laravel/storage/app/scribe_admin/collection.json", $output
);
$this->assertStringContainsString(
"Wrote OpenAPI specification to: vendor/orchestra/testbench-core/laravel/storage/app/scribe_admin/openapi.yaml", $output
);

$paths = collect([
Storage::disk('local')->path('scribe_admin/collection.json'),
Expand Down Expand Up @@ -177,7 +194,10 @@ public function generated_postman_collection_file_is_correct()
]);
config(['scribe.postman.enabled' => true]);

$this->generate();
$this->generateAndExpectConsoleOutput(
"Wrote HTML docs and assets to: public/docs/",
"Wrote Postman collection to: public/docs/collection.json"
);

$generatedCollection = json_decode(file_get_contents($this->postmanOutputPath()), true);
// The Postman ID varies from call to call; erase it to make the test data reproducible.
Expand Down Expand Up @@ -207,7 +227,10 @@ public function generated_openapi_spec_file_is_correct()
],
]);

$this->generate();
$this->generateAndExpectConsoleOutput(
"Wrote HTML docs and assets to: public/docs/",
"Wrote OpenAPI specification to: public/docs/openapi.yaml"
);

$generatedSpec = Yaml::parseFile($this->openapiOutputPath());
$fixtureSpec = Yaml::parseFile(__DIR__ . '/../Fixtures/openapi.yaml');
Expand Down
9 changes: 9 additions & 0 deletions tests/TestHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ protected function generate(array $flags = []): mixed
);
}

protected function generateAndExpectConsoleOutput(string ...$expectedOutput): void
{
$output = $this->generate();

foreach ($expectedOutput as $expected) {
$this->assertStringContainsString($expected, $output);
}
}

protected function assertFileContainsString(string $filePath, string $string)
{
$this->assertFileExists($filePath);
Expand Down

0 comments on commit 136ce71

Please sign in to comment.