From adbd69776d9c5198fbcdae7c7917252ad50cec07 Mon Sep 17 00:00:00 2001 From: Thai Nguyen Hung Date: Wed, 3 Apr 2024 13:13:00 +0700 Subject: [PATCH 1/2] feat: api only flag --- config/log-viewer.php | 2 ++ src/LogViewerServiceProvider.php | 4 ++++ tests/Feature/RoutesTest.php | 18 ++++++++++++++++++ tests/Pest.php | 1 + 4 files changed, 25 insertions(+) diff --git a/config/log-viewer.php b/config/log-viewer.php index a02a68b2..41e71bc1 100644 --- a/config/log-viewer.php +++ b/config/log-viewer.php @@ -12,6 +12,8 @@ 'enabled' => env('LOG_VIEWER_ENABLED', true), + 'api_only' => env('LOG_VIEWER_API_ONLY', false), + 'require_auth_in_production' => true, /* diff --git a/src/LogViewerServiceProvider.php b/src/LogViewerServiceProvider.php index 803e16b2..8598655e 100644 --- a/src/LogViewerServiceProvider.php +++ b/src/LogViewerServiceProvider.php @@ -95,6 +95,10 @@ protected function registerRoutes() $this->loadRoutesFrom(self::basePath('/routes/api.php')); }); + if (config('log-viewer.api_only', false)) { + return; + } + Route::group([ 'domain' => config('log-viewer.route_domain', null), 'prefix' => config('log-viewer.route_path'), diff --git a/tests/Feature/RoutesTest.php b/tests/Feature/RoutesTest.php index ba47abfa..2b0507d2 100644 --- a/tests/Feature/RoutesTest.php +++ b/tests/Feature/RoutesTest.php @@ -1,5 +1,7 @@ set('log-viewer.route_path', 'new-log-route'); @@ -25,6 +27,22 @@ expect(route('log-viewer.index'))->toBe('http://localhost'); }); +test('only use api', function () { + config()->set('log-viewer.api_only', true); + + reloadRoutes(); + + route('log-viewer.index'); +})->throws(RouteNotFoundException::class); + +test('only both api and web', function () { + config()->set('log-viewer.api_only', false); + + reloadRoutes(); + + expect(route('log-viewer.index'))->toBe('http://localhost'); +}); + /* |-------------------------------------------------------------------------- | HELPERS diff --git a/tests/Pest.php b/tests/Pest.php index c21ea6fc..67e52441 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -11,6 +11,7 @@ uses(TestCase::class)->in(__DIR__); uses()->afterEach(fn () => clearGeneratedLogFiles())->in('Feature', 'Unit'); uses()->beforeEach(fn () => Artisan::call('log-viewer:publish'))->in('Feature'); +uses()->beforeEach(fn () => Artisan::call('config:cache'))->in('Feature'); uses()->beforeEach(function () { // let's not include any of the default mac logs or similar config(['log-viewer.include_files' => ['*.log', '**/*.log']]); From 413c487d6c6eea2e42f81670b62c70d4b90d886d Mon Sep 17 00:00:00 2001 From: Thai Nguyen Hung Date: Wed, 10 Apr 2024 09:10:28 +0700 Subject: [PATCH 2/2] feat: change way to load base test case ignore some test cases need register service provider laterly --- tests/DeferPackageProviderTestCase.php | 15 +++++++++++++++ tests/Feature/OnlyApiRoutesTest.php | 13 +++++++++++++ tests/Feature/RoutesTest.php | 18 ------------------ tests/Pest.php | 23 +++++++++++++++++++---- 4 files changed, 47 insertions(+), 22 deletions(-) create mode 100644 tests/DeferPackageProviderTestCase.php create mode 100644 tests/Feature/OnlyApiRoutesTest.php diff --git a/tests/DeferPackageProviderTestCase.php b/tests/DeferPackageProviderTestCase.php new file mode 100644 index 00000000..c9f11d3b --- /dev/null +++ b/tests/DeferPackageProviderTestCase.php @@ -0,0 +1,15 @@ + true]); + $this->app->register(LogViewerServiceProvider::class); + route('log-viewer.index'); +})->throws(RouteNotFoundException::class); diff --git a/tests/Feature/RoutesTest.php b/tests/Feature/RoutesTest.php index 2b0507d2..ba47abfa 100644 --- a/tests/Feature/RoutesTest.php +++ b/tests/Feature/RoutesTest.php @@ -1,7 +1,5 @@ set('log-viewer.route_path', 'new-log-route'); @@ -27,22 +25,6 @@ expect(route('log-viewer.index'))->toBe('http://localhost'); }); -test('only use api', function () { - config()->set('log-viewer.api_only', true); - - reloadRoutes(); - - route('log-viewer.index'); -})->throws(RouteNotFoundException::class); - -test('only both api and web', function () { - config()->set('log-viewer.api_only', false); - - reloadRoutes(); - - expect(route('log-viewer.index'))->toBe('http://localhost'); -}); - /* |-------------------------------------------------------------------------- | HELPERS diff --git a/tests/Pest.php b/tests/Pest.php index 67e52441..303887bc 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -7,14 +7,29 @@ use Opcodes\LogViewer\LogIndex; use Opcodes\LogViewer\Logs\LogType; use Opcodes\LogViewer\Tests\TestCase; +use Symfony\Component\Finder\Finder; -uses(TestCase::class)->in(__DIR__); +$finder = new Finder(); + +$useTestCases = []; +foreach ($finder->files()->in(__DIR__)->notName('OnlyApiRoutesTest.php')->name('*.php') as $file) { + $useTestCases[] = $file->getRealPath(); +} + +$callingPublishTestCases = []; +foreach ($finder->files()->in(__DIR__ . '/Feature')->notName('OnlyApiRoutesTest.php')->name('*.php') as $file) { + $callingPublishTestCases[] = $file->getRealPath(); +} + +uses(TestCase::class)->in(...$useTestCases); uses()->afterEach(fn () => clearGeneratedLogFiles())->in('Feature', 'Unit'); -uses()->beforeEach(fn () => Artisan::call('log-viewer:publish'))->in('Feature'); -uses()->beforeEach(fn () => Artisan::call('config:cache'))->in('Feature'); +uses()->beforeEach(fn () => Artisan::call('log-viewer:publish'))->in(...$callingPublishTestCases); uses()->beforeEach(function () { // let's not include any of the default mac logs or similar - config(['log-viewer.include_files' => ['*.log', '**/*.log']]); + config([ + 'log-viewer.api_only' => false, + 'log-viewer.include_files' => ['*.log', '**/*.log'] + ]); })->in('Unit', 'Feature'); /*