From dd775d1f89e766e71f3dfbddc790ec28c406fed9 Mon Sep 17 00:00:00 2001 From: rupadana Date: Thu, 8 Feb 2024 13:17:40 +0000 Subject: [PATCH 1/7] Update CHANGELOG --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e5232d..a2c154d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ All notable changes to `api-service` will be documented in this file. +## 3.0.7 - 2024-02-08 + +### What's Changed + +* adding configuration to enable/disable policy by @rupadana in https://github.com/rupadana/filament-api-service/pull/19 + +**Full Changelog**: https://github.com/rupadana/filament-api-service/compare/3.0.6...3.0.7 + ## 3.0.6 - 2024-01-29 **Full Changelog**: https://github.com/rupadana/filament-api-service/compare/3.0.5...3.0.6 From 4ba82c7067b372000a1abe8b37f4870422be1861 Mon Sep 17 00:00:00 2001 From: I Wayan Rupadana Date: Fri, 9 Feb 2024 21:23:38 +0800 Subject: [PATCH 2/7] public api --- routes/api.php | 1 - src/Commands/MakeApiServiceCommand.php | 2 +- src/Http/Handlers.php | 19 ++++++++++++++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/routes/api.php b/routes/api.php index a299907..801c224 100644 --- a/routes/api.php +++ b/routes/api.php @@ -5,7 +5,6 @@ Route::prefix('api') ->name('api.') - ->middleware('auth:sanctum') ->group(function () { $panels = Filament::getPanels(); diff --git a/src/Commands/MakeApiServiceCommand.php b/src/Commands/MakeApiServiceCommand.php index 880495b..f241eb8 100644 --- a/src/Commands/MakeApiServiceCommand.php +++ b/src/Commands/MakeApiServiceCommand.php @@ -143,7 +143,7 @@ public function handle(): int ]); $this->components->info("Successfully created API for {$resource}!"); - $this->components->info("It automatically registered to '/api-service' route group"); + $this->components->info("It automatically registered to '/api' route group"); return static::SUCCESS; } diff --git a/src/Http/Handlers.php b/src/Http/Handlers.php index 87b10f3..9ad9c82 100644 --- a/src/Http/Handlers.php +++ b/src/Http/Handlers.php @@ -18,6 +18,8 @@ class Handlers protected static string $keyName = 'id'; + protected static bool $public = false; + const POST = 'post'; const GET = 'get'; @@ -40,7 +42,22 @@ public static function route(Router $router) $router ->$method(static::$uri, [static::class, 'handler']) ->name(static::getKebabClassName()) - ->middleware(static::getMiddlewareAliasName().':'.static::stringifyAbility()); + ->middleware(static::getRouteMiddleware()); + } + + public static function isPublic() : bool + { + return static::$public; + } + + public static function getRouteMiddleware() : array + { + if(static::isPublic()) return []; + + return [ + 'auth:sanctum', + static::getMiddlewareAliasName() . ':' . static::stringifyAbility() + ]; } protected static function getMiddlewareAliasName() From 1ff470fea5df5a3861b27818d7f5691d0bac9652 Mon Sep 17 00:00:00 2001 From: rupadana Date: Fri, 9 Feb 2024 13:24:09 +0000 Subject: [PATCH 3/7] Fix styling --- src/Http/Handlers.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Http/Handlers.php b/src/Http/Handlers.php index 9ad9c82..7b460f4 100644 --- a/src/Http/Handlers.php +++ b/src/Http/Handlers.php @@ -45,18 +45,20 @@ public static function route(Router $router) ->middleware(static::getRouteMiddleware()); } - public static function isPublic() : bool + public static function isPublic(): bool { return static::$public; } - public static function getRouteMiddleware() : array + public static function getRouteMiddleware(): array { - if(static::isPublic()) return []; + if (static::isPublic()) { + return []; + } return [ 'auth:sanctum', - static::getMiddlewareAliasName() . ':' . static::stringifyAbility() + static::getMiddlewareAliasName().':'.static::stringifyAbility(), ]; } From 028e6fa0d7ee7c2ad00bf92f7fc473eb1d517e2b Mon Sep 17 00:00:00 2001 From: I Wayan Rupadana Date: Fri, 9 Feb 2024 21:34:16 +0800 Subject: [PATCH 4/7] update readme --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 3996827..328539b 100644 --- a/README.md +++ b/README.md @@ -174,6 +174,16 @@ To Generate Token, you just need create it from admin panel. It will be Token Re ![Image](https://res.cloudinary.com/rupadana/image/upload/v1704958748/Screenshot_2024-01-11_at_15.37.55_ncpg8n.png) +## Public API + +Set API to public by overriding this property on your API Handler. Assume we have a `PaginationHandler` + +```php +class PaginationHandler extends Handlers { + public static bool $public = true; +} +``` + ## TODO - [ ] Test Plugin for Tenancy purpose From 4930d96cc0604f61af24b508da3809fe6f0ead2d Mon Sep 17 00:00:00 2001 From: rupadana Date: Fri, 9 Feb 2024 13:39:27 +0000 Subject: [PATCH 5/7] Update CHANGELOG --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a2c154d..69760f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ All notable changes to `api-service` will be documented in this file. +## 3.0.8 - 2024-02-09 + +### What's Changed + +* Public api by @rupadana in https://github.com/rupadana/filament-api-service/pull/20 + +**Full Changelog**: https://github.com/rupadana/filament-api-service/compare/3.0.7...3.0.8 + ## 3.0.7 - 2024-02-08 ### What's Changed From f1b4f3167ad6cbe187cb521b29933cb2ec1115e0 Mon Sep 17 00:00:00 2001 From: I Wayan Rupadana Date: Sat, 2 Mar 2024 18:23:57 +0800 Subject: [PATCH 6/7] feat: Allow non-admin users to generate API key to access their records --- .phpunit.cache/test-results | 1 - composer.json | 1 + pint.json | 84 +++++++++++++++++++ resources/lang/en/api-service.php | 2 +- routes/api.php | 2 +- src/ApiService.php | 1 - src/ApiServicePlugin.php | 4 +- src/ApiServiceServiceProvider.php | 3 +- src/Commands/MakeApiHandlerCommand.php | 2 - src/Commands/MakeApiServiceCommand.php | 2 - src/Commands/MakeApiTransformerCommand.php | 2 - src/Http/Handlers.php | 16 +--- src/Models/Token.php | 1 - src/Policies/TokenPolicy.php | 12 +-- src/Resources/TokenResource.php | 22 ++++- .../TokenResource/Pages/CreateToken.php | 20 +++-- src/Transformers/DefaultTransformer.php | 2 +- tests/ApiServiceTest.php | 18 ++-- .../Migrations/01_create_products_table.php | 3 +- ...01_create_personal_access_tokens_table.php | 3 +- tests/Fixtures/Models/Product.php | 4 - tests/Fixtures/Resources/ProductResource.php | 5 -- .../Api/Handlers/CreateHandler.php | 1 - .../Api/Handlers/DeleteHandler.php | 1 - .../Api/Handlers/DetailHandler.php | 1 - .../Api/Handlers/PaginationHandler.php | 1 - .../Api/Handlers/UpdateHandler.php | 1 - .../ProductResource/Api/ProductApiService.php | 1 - .../Api/ProductTransformer.php | 2 +- tests/TestCase.php | 2 +- 30 files changed, 144 insertions(+), 76 deletions(-) delete mode 100644 .phpunit.cache/test-results create mode 100644 pint.json diff --git a/.phpunit.cache/test-results b/.phpunit.cache/test-results deleted file mode 100644 index 9491d5a..0000000 --- a/.phpunit.cache/test-results +++ /dev/null @@ -1 +0,0 @@ -{"version":"pest_2.32.2","defects":[],"times":{"P\\Tests\\ApiServiceTest::__pest_evaluable_it_can_return_a_list_of_products_with_selected_filters":0.088,"P\\Tests\\ApiServiceTest::__pest_evaluable_it_can_return_a_list_of_products_with_selected_fields":0.083,"P\\Tests\\ApiServiceTest::__pest_evaluable_it_throws_when_selecting_a_field_that_is_not_allowed":0.17,"P\\Tests\\ApiServiceTest::__pest_evaluable_it_can_make_routes_for_a_product_resource":0.006,"P\\Tests\\ApiServiceTest::__pest_evaluable_it_can_return_a_list_of_products_with_a_custom_transformer":0.081,"P\\Tests\\ApiServiceTest::__pest_evaluable_it_throws_when_filtering_by_a_field_that_is_not_allowed":0.124,"P\\Tests\\ApiServiceTest::__pest_evaluable_it_can_return_a_list_of_products_with_selected_sorts":0.086,"P\\Tests\\ApiServiceTest::__pest_evaluable_it_throws_when_sorting_by_a_field_that_is_not_allowed":0.124,"P\\Tests\\ApiServiceTest::__pest_evaluable_it_can_return_a_list_of_products_with_allowed_attributes":0.082,"P\\Tests\\ArchTest::__pest_evaluable_it_will_not_use_debugging_functions":0.134}} \ No newline at end of file diff --git a/composer.json b/composer.json index 94cd327..6cf6ded 100644 --- a/composer.json +++ b/composer.json @@ -32,6 +32,7 @@ "spatie/laravel-query-builder": "^5.3" }, "require-dev": { + "laravel/pint": "^1.0", "nunomaduro/collision": "^7.9", "orchestra/testbench": "^8.0", "pestphp/pest": "^2.0", diff --git a/pint.json b/pint.json new file mode 100644 index 0000000..a32453d --- /dev/null +++ b/pint.json @@ -0,0 +1,84 @@ +{ + "preset": "laravel", + "rules": { + "no_unused_imports": true, + "ordered_imports": true, + "phpdoc_align": true, + "phpdoc_order": true, + "single_line_comment_style": true, + "single_quote": true, + "combine_consecutive_issets": true, + "array_syntax": true, + "blank_line_after_namespace": true, + "blank_line_after_opening_tag": true, + "attribute_empty_parentheses": true, + "blank_line_before_statement": true, + "braces_position": true, + "blank_lines_before_namespace": { + "min_line_breaks": 1 + }, + "cast_spaces": { + "space": "single" + }, + "class_attributes_separation": { + "elements": { + "const": "only_if_meta", + "property": "only_if_meta" + } + }, + "class_definition": { + "single_line": true + }, + "class_reference_name_casing": true, + "clean_namespace": true, + "concat_space": { + "spacing": "one" + }, + "constant_case": true, + "control_structure_braces": true, + "control_structure_continuation_position": true, + "elseif": true, + "empty_loop_body": true, + "empty_loop_condition": true, + "encoding": true, + "explicit_indirect_variable": true, + "full_opening_tag": true, + "function_declaration": true, + "general_phpdoc_annotation_remove": { + "annotations": [ + "author", + "package", + "subpackage" + ], + "case_sensitive": false + }, + "global_namespace_import": true, + "heredoc_indentation": true, + "increment_style": { + "style": "post" + }, + "indentation_type": true, + "lambda_not_used_import": true, + "linebreak_after_opening_tag": true, + "lowercase_cast": true, + "lowercase_keywords": true, + "lowercase_static_reference": true, + "magic_constant_casing": true, + "magic_method_casing": true, + "method_chaining_indentation": true, + "multiline_comment_opening_closing": true, + "multiline_whitespace_before_semicolons": true, + "native_type_declaration_casing": true, + "no_blank_lines_after_class_opening": true, + "no_blank_lines_after_phpdoc": true, + "no_break_comment": true, + "no_empty_comment": true, + "no_empty_statement": true, + "no_multiline_whitespace_around_double_arrow": true, + "no_multiple_statements_per_line": true, + "no_short_bool_cast": true, + "no_singleline_whitespace_before_semicolons": true, + "no_space_around_double_colon": true, + "no_trailing_whitespace": true + } +} \ No newline at end of file diff --git a/resources/lang/en/api-service.php b/resources/lang/en/api-service.php index 8dfc1c8..cb63102 100644 --- a/resources/lang/en/api-service.php +++ b/resources/lang/en/api-service.php @@ -2,5 +2,5 @@ // translations for Rupadana/ApiService return [ - // + ]; diff --git a/routes/api.php b/routes/api.php index 801c224..12b117c 100644 --- a/routes/api.php +++ b/routes/api.php @@ -12,7 +12,7 @@ try { Route::prefix($panel->getId()) - ->name($panel->getId().'.') + ->name($panel->getId() . '.') ->group(function () use ($panel) { $apiServicePlugin = $panel->getPlugin('api-service'); $apiServicePlugin->route($panel); diff --git a/src/ApiService.php b/src/ApiService.php index a5a1e53..97174fc 100644 --- a/src/ApiService.php +++ b/src/ApiService.php @@ -11,7 +11,6 @@ class ApiService * Filament Resource */ protected static ?string $resource = null; - protected static ?string $groupRouteName = null; /** diff --git a/src/ApiServicePlugin.php b/src/ApiServicePlugin.php index dac91f1..102098d 100644 --- a/src/ApiServicePlugin.php +++ b/src/ApiServicePlugin.php @@ -38,7 +38,7 @@ public static function getAbilities(Panel $panel): array $resourceName = str($resource)->beforeLast('Resource')->explode('\\')->last(); - $apiServiceClass = $resource.'\\Api\\'.$resourceName.'ApiService'; + $apiServiceClass = $resource . '\\Api\\' . $resourceName . 'ApiService'; $handlers = app($apiServiceClass)->handlers(); @@ -63,7 +63,7 @@ public function route(Panel $panel): void try { $resourceName = str($resource)->beforeLast('Resource')->explode('\\')->last(); - $apiServiceClass = $resource.'\\Api\\'.$resourceName.'ApiService'; + $apiServiceClass = $resource . '\\Api\\' . $resourceName . 'ApiService'; app($apiServiceClass)->registerRoutes(); } catch (Exception $e) { diff --git a/src/ApiServiceServiceProvider.php b/src/ApiServiceServiceProvider.php index 56e6a36..b75f05e 100644 --- a/src/ApiServiceServiceProvider.php +++ b/src/ApiServiceServiceProvider.php @@ -17,7 +17,6 @@ class ApiServiceServiceProvider extends PackageServiceProvider { public static string $name = 'api-service'; - public static string $viewNamespace = 'api-service'; public function configurePackage(Package $package): void @@ -91,7 +90,7 @@ protected function getAssetPackageName(): ?string protected function getAssets(): array { return [ - // + ]; } diff --git a/src/Commands/MakeApiHandlerCommand.php b/src/Commands/MakeApiHandlerCommand.php index d0bec54..70270fd 100644 --- a/src/Commands/MakeApiHandlerCommand.php +++ b/src/Commands/MakeApiHandlerCommand.php @@ -14,9 +14,7 @@ class MakeApiHandlerCommand extends Command { use CanManipulateFiles; - protected $description = 'Create a new API Handler for supporting filamentphp Resource'; - protected $signature = 'make:filament-api-handler {resource?} {handler?} {--panel=}'; public function handle(): int diff --git a/src/Commands/MakeApiServiceCommand.php b/src/Commands/MakeApiServiceCommand.php index f241eb8..19aa150 100644 --- a/src/Commands/MakeApiServiceCommand.php +++ b/src/Commands/MakeApiServiceCommand.php @@ -14,9 +14,7 @@ class MakeApiServiceCommand extends Command { use CanManipulateFiles; - protected $description = 'Create a new API Service for supporting filamentphp Resource'; - protected $signature = 'make:filament-api-service {resource?} {--panel=}'; public function handle(): int diff --git a/src/Commands/MakeApiTransformerCommand.php b/src/Commands/MakeApiTransformerCommand.php index abe4c62..b9dd6fb 100644 --- a/src/Commands/MakeApiTransformerCommand.php +++ b/src/Commands/MakeApiTransformerCommand.php @@ -14,9 +14,7 @@ class MakeApiTransformerCommand extends Command { use CanManipulateFiles; - protected $description = 'Create a Transformer for your API response'; - protected $signature = 'make:filament-api-transformer {resource?} {--panel=}'; public function handle(): int diff --git a/src/Http/Handlers.php b/src/Http/Handlers.php index 7b460f4..711ac49 100644 --- a/src/Http/Handlers.php +++ b/src/Http/Handlers.php @@ -9,25 +9,15 @@ class Handlers { use HttpResponse; - public static ?string $uri = '/'; - public static string $method = 'get'; - public static ?string $resource = null; - protected static string $keyName = 'id'; - protected static bool $public = false; - const POST = 'post'; - const GET = 'get'; - const DELETE = 'delete'; - const PATCH = 'patch'; - const PUT = 'put'; public static function getMethod() @@ -40,7 +30,7 @@ public static function route(Router $router) $method = static::getMethod(); $router - ->$method(static::$uri, [static::class, 'handler']) + ->{$method}(static::$uri, [static::class, 'handler']) ->name(static::getKebabClassName()) ->middleware(static::getRouteMiddleware()); } @@ -58,7 +48,7 @@ public static function getRouteMiddleware(): array return [ 'auth:sanctum', - static::getMiddlewareAliasName().':'.static::stringifyAbility(), + static::getMiddlewareAliasName() . ':' . static::stringifyAbility(), ]; } @@ -80,7 +70,7 @@ public static function stringifyAbility() public static function getAbility(): array { return [ - str(str(static::getModel())->explode('\\')->last())->kebab().':'.static::getKebabClassName(), + str(str(static::getModel())->explode('\\')->last())->kebab() . ':' . static::getKebabClassName(), ]; } diff --git a/src/Models/Token.php b/src/Models/Token.php index 389c151..4085001 100644 --- a/src/Models/Token.php +++ b/src/Models/Token.php @@ -8,6 +8,5 @@ class Token extends PersonalAccessToken { use HasFactory; - protected $table = 'personal_access_tokens'; } diff --git a/src/Policies/TokenPolicy.php b/src/Policies/TokenPolicy.php index ee7c6bc..9560ccd 100644 --- a/src/Policies/TokenPolicy.php +++ b/src/Policies/TokenPolicy.php @@ -36,7 +36,7 @@ public function view(User $user, Token $token): bool return true; } - return $user->can('view_token'); + return $user->can('view_token') && $token->tokenable_id === $user->id; } /** @@ -60,7 +60,7 @@ public function update(User $user, Token $token): bool return true; } - return $user->can('update_token'); + return $user->can('update_token') && $token->tokenable_id === $user->id; } /** @@ -73,7 +73,7 @@ public function delete(User $user, Token $token): bool return true; } - return $user->can('delete_token'); + return $user->can('delete_token') && $token->tokenable_id === $user->id; } /** @@ -97,7 +97,7 @@ public function forceDelete(User $user, Token $token): bool return true; } - return $user->can('force_delete_token'); + return $user->can('force_delete_token') && $token->tokenable_id === $user->id; } /** @@ -121,7 +121,7 @@ public function restore(User $user, Token $token): bool return true; } - return $user->can('restore_token'); + return $user->can('restore_token') && $token->tokenable_id === $user->id; } /** @@ -145,7 +145,7 @@ public function replicate(User $user, Token $token): bool return true; } - return $user->can('replicate_token'); + return $user->can('replicate_token') && $token->tokenable_id === $user->id; } /** diff --git a/src/Resources/TokenResource.php b/src/Resources/TokenResource.php index 55fdc76..9c59b61 100644 --- a/src/Resources/TokenResource.php +++ b/src/Resources/TokenResource.php @@ -15,6 +15,7 @@ use Filament\Tables\Actions\DeleteAction; use Filament\Tables\Columns\TextColumn; use Filament\Tables\Table; +use Illuminate\Database\Eloquent\Builder; use Rupadana\ApiService\ApiServicePlugin; use Rupadana\ApiService\Models\Token; use Rupadana\ApiService\Resources\TokenResource\Pages; @@ -22,7 +23,6 @@ class TokenResource extends Resource { protected static ?string $model = Token::class; - protected static ?string $navigationIcon = 'heroicon-o-key'; public static function form(Form $form): Form @@ -36,6 +36,11 @@ public static function form(Form $form): Form Select::make('tokenable_id') ->options(User::all()->pluck('name', 'id')) ->label('User') + ->hidden(function () { + $user = auth()->user(); + + return ! $user->hasRole('super_admin'); + }) ->required(), ]), @@ -87,7 +92,7 @@ public static function table(Table $table): Table ->toggleable(isToggledHiddenByDefault: true), ]) ->filters([ - // + ]) ->actions([ DeleteAction::make(), @@ -96,13 +101,22 @@ public static function table(Table $table): Table Tables\Actions\BulkActionGroup::make([ Tables\Actions\DeleteBulkAction::make(), ]), - ]); + ]) + ->modifyQueryUsing(function (Builder $query) { + $authenticatedUser = auth()->user(); + + if (method_exists($authenticatedUser, 'hasRole') && $authenticatedUser->hasRole('super_admin')) { + return $query; + } + + return $query->where('tokenable_id', $authenticatedUser->id); + }); } public static function getRelations(): array { return [ - // + ]; } diff --git a/src/Resources/TokenResource/Pages/CreateToken.php b/src/Resources/TokenResource/Pages/CreateToken.php index a8755e7..c0a139f 100644 --- a/src/Resources/TokenResource/Pages/CreateToken.php +++ b/src/Resources/TokenResource/Pages/CreateToken.php @@ -12,26 +12,32 @@ class CreateToken extends CreateRecord { protected static string $resource = TokenResource::class; + protected $newToken; protected function handleRecordCreation(array $data): Model { + if (! isset($data['tokenable_id'])) { + $data['tokenable_id'] = auth()->user()->id; + } $user = User::find($data['tokenable_id']); - $newToken = $user->createToken($data['name'], $data['ability']); + $this->newToken = $user->createToken($data['name'], $data['ability']); + + return $user; + } - Notification::make() + protected function getCreatedNotification(): ?Notification + { + return Notification::make() ->title('Token created, save it!') - ->body($newToken->plainTextToken) + ->body($this->newToken->plainTextToken) ->persistent() ->actions([ Action::make('close') ->close(), ]) - ->success() - ->send(); - - return $user; + ->success(); } protected function sendCreatedNotificationAndRedirect(bool $shouldCreateAnotherInsteadOfRedirecting = true): void diff --git a/src/Transformers/DefaultTransformer.php b/src/Transformers/DefaultTransformer.php index 9187f17..e941f4c 100644 --- a/src/Transformers/DefaultTransformer.php +++ b/src/Transformers/DefaultTransformer.php @@ -9,7 +9,7 @@ class DefaultTransformer extends JsonResource /** * Transform the resource into an array. * - * @param \Illuminate\Http\Request $request + * @param \Illuminate\Http\Request $request * @return array */ public function toArray($request) diff --git a/tests/ApiServiceTest.php b/tests/ApiServiceTest.php index ff21a01..93a7166 100644 --- a/tests/ApiServiceTest.php +++ b/tests/ApiServiceTest.php @@ -8,7 +8,7 @@ it('can make routes for a product resource', function () { $routes = collect(app('router')->getRoutes())->map(function (RoutingRoute $route) { - return implode('|', $route->methods()).' '.$route->uri(); + return implode('|', $route->methods()) . ' ' . $route->uri(); }); // The route name is customized to `our-products` in the `ProductApiService` class @@ -27,7 +27,7 @@ $token = $user->createToken('testing')->plainTextToken; $response = $this->get('/api/admin/our-products', [ - 'Authorization' => 'Bearer '.$token, + 'Authorization' => 'Bearer ' . $token, ]) ->assertStatus(200); @@ -57,7 +57,7 @@ $token = $user->createToken('testing')->plainTextToken; $response = $this->get('/api/admin/our-products?fields[products]=name,price', [ - 'Authorization' => 'Bearer '.$token, + 'Authorization' => 'Bearer ' . $token, ]) ->assertStatus(200); @@ -75,7 +75,7 @@ $token = $user->createToken('testing')->plainTextToken; $this->get('/api/admin/our-products?fields[products]=name,slug,price', [ - 'Authorization' => 'Bearer '.$token, + 'Authorization' => 'Bearer ' . $token, ]) ->assertStatus(400) ->assertJsonFragment([ @@ -91,7 +91,7 @@ $token = $user->createToken('testing')->plainTextToken; $response = $this->get('/api/admin/our-products?sort=-price', [ - 'Authorization' => 'Bearer '.$token, + 'Authorization' => 'Bearer ' . $token, ]) ->assertStatus(200); @@ -119,7 +119,7 @@ $token = $user->createToken('testing')->plainTextToken; $this->get('/api/admin/our-products?sort=-slug', [ - 'Authorization' => 'Bearer '.$token, + 'Authorization' => 'Bearer ' . $token, ]) ->assertStatus(400) ->assertJsonFragment([ @@ -135,7 +135,7 @@ $token = $user->createToken('testing')->plainTextToken; $response = $this->get('/api/admin/our-products?filter[name]=T-Shirt&filter[price]=500', [ - 'Authorization' => 'Bearer '.$token, + 'Authorization' => 'Bearer ' . $token, ]) ->assertStatus(200); @@ -153,7 +153,7 @@ $token = $user->createToken('testing')->plainTextToken; $this->get('/api/admin/our-products?filter[name]=T-Shirt&filter[slug]=t-shirt', [ - 'Authorization' => 'Bearer '.$token, + 'Authorization' => 'Bearer ' . $token, ]) ->assertStatus(400) ->assertJsonFragment([ @@ -169,7 +169,7 @@ $token = $user->createToken('testing')->plainTextToken; $response = $this->get('/api/admin/our-products', [ - 'Authorization' => 'Bearer '.$token, + 'Authorization' => 'Bearer ' . $token, ]) ->assertStatus(200); diff --git a/tests/Fixtures/Database/Migrations/01_create_products_table.php b/tests/Fixtures/Database/Migrations/01_create_products_table.php index 88d847a..7d148b9 100644 --- a/tests/Fixtures/Database/Migrations/01_create_products_table.php +++ b/tests/Fixtures/Database/Migrations/01_create_products_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class() extends Migration -{ +return new class() extends Migration { public function up() { Schema::create('products', function (Blueprint $table) { diff --git a/tests/Fixtures/Database/Migrations/2019_12_14_000001_create_personal_access_tokens_table.php b/tests/Fixtures/Database/Migrations/2019_12_14_000001_create_personal_access_tokens_table.php index 6c81fd2..a399be0 100644 --- a/tests/Fixtures/Database/Migrations/2019_12_14_000001_create_personal_access_tokens_table.php +++ b/tests/Fixtures/Database/Migrations/2019_12_14_000001_create_personal_access_tokens_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. * diff --git a/tests/Fixtures/Models/Product.php b/tests/Fixtures/Models/Product.php index 289b2d5..3594753 100644 --- a/tests/Fixtures/Models/Product.php +++ b/tests/Fixtures/Models/Product.php @@ -13,21 +13,17 @@ class Product extends Model 'price', 'created_at', ]; - public static array $allowedSorts = [ 'name', 'price', 'created_at', ]; - public static array $allowedFilters = [ 'name', 'price', 'created_at', ]; - protected $guarded = []; - protected $hidden = [ 'slug', ]; diff --git a/tests/Fixtures/Resources/ProductResource.php b/tests/Fixtures/Resources/ProductResource.php index 6e2c74a..031290e 100644 --- a/tests/Fixtures/Resources/ProductResource.php +++ b/tests/Fixtures/Resources/ProductResource.php @@ -13,15 +13,10 @@ class ProductResource extends Resource { protected static ?string $model = Product::class; - protected static bool $isDiscovered = false; - protected static ?string $navigationIcon = 'heroicon-o-archive-box'; - protected static ?string $navigationLabel = 'Product'; - protected static ?string $modelLabel = 'Product'; - protected static ?string $pluralModelLabel = 'Products'; public static function getApiTransformer() diff --git a/tests/Fixtures/Resources/ProductResource/Api/Handlers/CreateHandler.php b/tests/Fixtures/Resources/ProductResource/Api/Handlers/CreateHandler.php index 6734acd..2f6b1bc 100644 --- a/tests/Fixtures/Resources/ProductResource/Api/Handlers/CreateHandler.php +++ b/tests/Fixtures/Resources/ProductResource/Api/Handlers/CreateHandler.php @@ -9,7 +9,6 @@ class CreateHandler extends Handlers { public static ?string $uri = '/'; - public static ?string $resource = ProductResource::class; public static function getMethod() diff --git a/tests/Fixtures/Resources/ProductResource/Api/Handlers/DeleteHandler.php b/tests/Fixtures/Resources/ProductResource/Api/Handlers/DeleteHandler.php index 5caaf71..253c2cf 100644 --- a/tests/Fixtures/Resources/ProductResource/Api/Handlers/DeleteHandler.php +++ b/tests/Fixtures/Resources/ProductResource/Api/Handlers/DeleteHandler.php @@ -9,7 +9,6 @@ class DeleteHandler extends Handlers { public static ?string $uri = '/{id}'; - public static ?string $resource = ProductResource::class; public static function getMethod() diff --git a/tests/Fixtures/Resources/ProductResource/Api/Handlers/DetailHandler.php b/tests/Fixtures/Resources/ProductResource/Api/Handlers/DetailHandler.php index 4f6936a..db0a02c 100644 --- a/tests/Fixtures/Resources/ProductResource/Api/Handlers/DetailHandler.php +++ b/tests/Fixtures/Resources/ProductResource/Api/Handlers/DetailHandler.php @@ -9,7 +9,6 @@ class DetailHandler extends Handlers { public static ?string $uri = '/{id}'; - public static ?string $resource = ProductResource::class; public function handler($id) diff --git a/tests/Fixtures/Resources/ProductResource/Api/Handlers/PaginationHandler.php b/tests/Fixtures/Resources/ProductResource/Api/Handlers/PaginationHandler.php index 4c93cce..14ce88b 100644 --- a/tests/Fixtures/Resources/ProductResource/Api/Handlers/PaginationHandler.php +++ b/tests/Fixtures/Resources/ProductResource/Api/Handlers/PaginationHandler.php @@ -9,7 +9,6 @@ class PaginationHandler extends Handlers { public static ?string $uri = '/'; - public static ?string $resource = ProductResource::class; public function handler() diff --git a/tests/Fixtures/Resources/ProductResource/Api/Handlers/UpdateHandler.php b/tests/Fixtures/Resources/ProductResource/Api/Handlers/UpdateHandler.php index 0be1e99..ac68f16 100644 --- a/tests/Fixtures/Resources/ProductResource/Api/Handlers/UpdateHandler.php +++ b/tests/Fixtures/Resources/ProductResource/Api/Handlers/UpdateHandler.php @@ -9,7 +9,6 @@ class UpdateHandler extends Handlers { public static ?string $uri = '/{id}'; - public static ?string $resource = ProductResource::class; public static function getMethod() diff --git a/tests/Fixtures/Resources/ProductResource/Api/ProductApiService.php b/tests/Fixtures/Resources/ProductResource/Api/ProductApiService.php index 93691dc..75ef537 100644 --- a/tests/Fixtures/Resources/ProductResource/Api/ProductApiService.php +++ b/tests/Fixtures/Resources/ProductResource/Api/ProductApiService.php @@ -7,7 +7,6 @@ class ProductApiService extends ApiService { protected static ?string $resource = \Rupadana\ApiService\Tests\Fixtures\Resources\ProductResource::class; - protected static ?string $groupRouteName = 'our-products'; // customize route name public static function handlers(): array diff --git a/tests/Fixtures/Resources/ProductResource/Api/ProductTransformer.php b/tests/Fixtures/Resources/ProductResource/Api/ProductTransformer.php index 1f2908a..cec4f63 100644 --- a/tests/Fixtures/Resources/ProductResource/Api/ProductTransformer.php +++ b/tests/Fixtures/Resources/ProductResource/Api/ProductTransformer.php @@ -9,7 +9,7 @@ class ProductTransformer extends JsonResource /** * Transform the resource into an array. * - * @param \Illuminate\Http\Request $request + * @param \Illuminate\Http\Request $request * @return array */ public function toArray($request) diff --git a/tests/TestCase.php b/tests/TestCase.php index ba065ce..094ffb2 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -74,7 +74,7 @@ protected function defineDatabaseMigrations() $this->loadLaravelMigrations(); // Migrations for test fixtures - $this->loadMigrationsFrom(realpath(__DIR__.'/Fixtures/Database/Migrations')); + $this->loadMigrationsFrom(realpath(__DIR__ . '/Fixtures/Database/Migrations')); } protected function defineRoutes($router) From 2b9170c343308ca683f86aa4d97c4267e8ea6f48 Mon Sep 17 00:00:00 2001 From: rupadana Date: Sat, 2 Mar 2024 10:27:08 +0000 Subject: [PATCH 7/7] Fix styling --- src/Http/Handlers.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Http/Handlers.php b/src/Http/Handlers.php index 45b8c7e..711ac49 100644 --- a/src/Http/Handlers.php +++ b/src/Http/Handlers.php @@ -14,7 +14,6 @@ class Handlers public static ?string $resource = null; protected static string $keyName = 'id'; protected static bool $public = false; - const POST = 'post'; const GET = 'get'; const DELETE = 'delete'; @@ -49,7 +48,7 @@ public static function getRouteMiddleware(): array return [ 'auth:sanctum', - static::getMiddlewareAliasName() . ':' . static::stringifyAbility() + static::getMiddlewareAliasName() . ':' . static::stringifyAbility(), ]; }