123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- namespace Knuckles\Scribe\Tests\GenerateDocumentation;
- use Illuminate\Support\Facades\Route as RouteFacade;
- use Knuckles\Scribe\Scribe;
- use Knuckles\Scribe\Tests\BaseLaravelTest;
- use Knuckles\Scribe\Tests\Fixtures\TestController;
- use Knuckles\Scribe\Tests\Fixtures\TestGroupController;
- use Knuckles\Scribe\Tests\Fixtures\TestIgnoreThisController;
- use Knuckles\Scribe\Tests\Fixtures\TestPartialResourceController;
- use Knuckles\Scribe\Tests\Fixtures\TestResourceController;
- use Knuckles\Scribe\Tests\Fixtures\TestUser;
- use Knuckles\Scribe\Tests\TestHelpers;
- use Knuckles\Scribe\Tools\Utils;
- class BehavioursTest extends BaseLaravelTest
- {
- use TestHelpers;
- protected function setUp(): void
- {
- parent::setUp();
- config(['scribe.database_connections_to_transact' => []]);
- config(['scribe.routes.0.match.prefixes' => ['api/*']]);
- // Skip these ones for faster tests
- config(['scribe.openapi.enabled' => false]);
- config(['scribe.postman.enabled' => false]);
- $factory = app(\Illuminate\Database\Eloquent\Factory::class);
- $factory->define(TestUser::class, function () {
- return [
- 'id' => 4,
- 'first_name' => 'Tested',
- 'last_name' => 'Again',
- 'email' => 'a@b.com',
- ];
- });
- }
- public function tearDown(): void
- {
- Utils::deleteDirectoryAndContents('public/docs');
- Utils::deleteDirectoryAndContents('.scribe');
- }
- /** @test */
- public function can_process_traditional_laravel_route_syntax_and_callable_tuple_syntax()
- {
- RouteFacade::get('/api/test', [TestController::class, 'withEndpointDescription']);
- RouteFacade::get('/api/array/test', [TestController::class, 'withEndpointDescription']);
- $this->generateAndExpectConsoleOutput(
- 'Processed route: [GET] api/test',
- 'Processed route: [GET] api/array/test'
- );
- }
- /** @test */
- public function processes_head_routes_as_head_not_get()
- {
- RouteFacade::addRoute('HEAD', '/api/test', [TestController::class, 'withEndpointDescription']);
- $this->generateAndExpectConsoleOutput('Processed route: [HEAD] api/test');
- }
- /**
- * @test
- * @see https://github.com/knuckleswtf/scribe/issues/53
- */
- public function can_process_closure_routes()
- {
- RouteFacade::get('/api/closure', function () {
- return 'hi';
- });
- $this->generateAndExpectConsoleOutput('Processed route: [GET] api/closure');
- }
- /**
- * @group dingo
- * @test
- */
- public function can_process_routes_on_dingo()
- {
- $api = app(\Dingo\Api\Routing\Router::class);
- $api->version('v1', function ($api) {
- $api->get('/closure', function () {
- return 'foo';
- });
- $api->get('/test', [TestController::class, 'withEndpointDescription']);
- });
- config(['scribe.routes.0.match.prefixes' => ['*']]);
- config(['scribe.routes.0.match.versions' => ['v1']]);
- $this->generateAndExpectConsoleOutput(
- 'Processed route: [GET] closure',
- 'Processed route: [GET] test'
- );
- }
- /** @test */
- public function calls_afterGenerating_hook()
- {
- $paths = [];
- Scribe::afterGenerating(function (array $outputPaths) use (&$paths) {
- $paths = $outputPaths;
- });
- RouteFacade::get('/api/test', [TestController::class, 'withEndpointDescription']);
- $this->generate();
- $this->assertEquals([
- 'html' => realpath('public/docs/index.html'),
- 'blade' => null,
- 'postman' => realpath('public/docs/collection.json') ?: null,
- 'openapi' => realpath('public/docs/openapi.yaml') ?: null,
- 'assets' => [
- 'js' => realpath('public/docs/js'),
- 'css' => realpath('public/docs/css'),
- 'images' => realpath('public/docs/images'),
- ],
- ], $paths);
- Scribe::afterGenerating(fn() => null);
- }
- /** @test */
- public function skips_methods_and_classes_with_hidefromapidocumentation_tag()
- {
- RouteFacade::get('/api/skip', [TestController::class, 'skip']);
- RouteFacade::get('/api/skipClass', TestIgnoreThisController::class . '@dummy');
- RouteFacade::get('/api/test', [TestController::class, 'withEndpointDescription']);
- $this->generateAndExpectConsoleOutput(
- 'Skipping route: [GET] api/skip',
- 'Skipping route: [GET] api/skipClass',
- 'Processed route: [GET] api/test'
- );
- }
- /** @test */
- public function warns_of_nonexistent_response_files()
- {
- RouteFacade::get('/api/non-existent', [TestController::class, 'withNonExistentResponseFile']);
- $this->generateAndExpectConsoleOutput('@responseFile i-do-not-exist.json does not exist');
- }
- /** @test */
- public function can_parse_resource_routes()
- {
- RouteFacade::resource('/api/users', TestResourceController::class)
- ->only(['index', 'store']);
- $output = $this->generate();
- $this->assertStringContainsString('Processed route: [GET] api/users', $output);
- $this->assertStringContainsString('Processed route: [POST] api/users', $output);
- $this->assertStringNotContainsString('Processed route: [PUT,PATCH] api/users/{user}', $output);
- $this->assertStringNotContainsString('Processed route: [DELETE] api/users/{user}', $output);
- }
- /** @test */
- public function supports_partial_resource_controller()
- {
- RouteFacade::resource('/api/users', TestPartialResourceController::class);
- $this->generateAndExpectConsoleOutput(
- 'Processed route: [GET] api/users',
- 'Processed route: [PUT,PATCH] api/users/{user}'
- );
- }
- /** @test */
- public function can_customise_static_output_path()
- {
- RouteFacade::get('/api/action1', TestGroupController::class . '@action1');
- config(['scribe.static.output_path' => 'static/docs']);
- $this->assertFileDoesNotExist('static/docs/index.html');
- $this->generate();
- $this->assertFileExists('static/docs/index.html');
- Utils::deleteDirectoryAndContents('static/');
- }
- protected function generateAndExpectConsoleOutput(string ...$expectedOutput): void
- {
- $output = $this->generate();
- foreach ($expectedOutput as $expected) {
- $this->assertStringContainsString($expected, $output);
- }
- }
- }
|