diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index cdc7504..d9d6048 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -2,9 +2,15 @@ name: Tests on: push: - branches: [ main, develop, 3.x ] + branches: + - main + - develop + - 3.x pull_request: - branches: [ main, develop, 3.x ] + branches: + - main + - develop + - 3.x jobs: build: @@ -14,12 +20,12 @@ jobs: strategy: fail-fast: true matrix: - php: [8.1, 8.2] - laravel: [10] + php: [ 8.2, 8.3, 8.4 ] + laravel: [ 11, 12 ] steps: - name: Checkout Code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -34,7 +40,7 @@ jobs: run: composer require "laravel/framework:^${{ matrix.laravel }}" --no-update - name: Install dependencies - uses: nick-fields/retry@v2 + uses: nick-fields/retry@v3 with: timeout_minutes: 5 max_attempts: 5 diff --git a/.gitignore b/.gitignore index 62b9c43..9448440 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ /dummy vendor/ composer.lock -.phpunit.result.cache -.phpunit.cache +.phpunit.cache/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 38dfa04..5b87944 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,75 @@ All notable changes to this project will be documented in this file. This projec ## Unreleased +## [5.1.0] - 2025-02-24 + +### Added + +- Package now supports Laravel 12. + +## [5.0.2] - 2025-12-03 + +### Fixed + +- [#302](https://github.com/laravel-json-api/laravel/pull/302) Ensure auth response is used when deleting a resource + that does not have a resource response class. + +## [5.0.1] - 2025-12-02 + +### Fixed + +- [#301](https://github.com/laravel-json-api/laravel/pull/301) Do not override response status when authorization + exception is thrown. + +## [5.0.0] - 2025-12-01 + +### Changed + +- [#298](https://github.com/laravel-json-api/laravel/pull/298) + and [#70](https://github.com/laravel-json-api/laravel/issues/70) The authorizer implementation now allows methods to + return either `bool` or an Illuminate Auth `Response`. +- **BREAKING** The return type for the `authorizeResource()` method on both resource and query request classes has + changed to `bool|Response` (where response is the Illuminate Auth response). If you are manually calling this method + and relying on the return value being a boolean, this change is breaking. However, the vast majority of applications + should be able to upgrade without any changes. + +## [4.1.1] - 2024-11-30 + +### Fixed + +- Remove deprecation notices in PHP 8.4. + +## [4.1.0] - 2024-06-26 + +### Fixed + +- [core#17](https://github.com/laravel-json-api/core/pull/17) Fix incorrect `self` link in related resource responses, + and remove `related` link that should not exist. This has been incorrect for some time, but is definitely what + the [spec defines here.](https://jsonapi.org/format/1.0/#document-top-level) +- [eloquent#36](https://github.com/laravel-json-api/eloquent/pull/36) Support Eloquent dynamic relationships. + +## [4.0.0] - 2024-03-14 + +### Changed + +- Package is now licensed under the MIT License. +- **BREAKING** Package now requires Laravel 11. +- Minimum PHP version is now `8.2`. + +## [3.4.0] - 2024-03-03 + +### Added + +- [#272](https://github.com/laravel-json-api/laravel/pull/272) Added a model property type-hint to the resource stub and + allowed it to be replaced via a model option on the command. + +## [3.3.0] - 2024-02-14 + +### Added + +- [#265](https://github.com/laravel-json-api/laravel/issues/265) Allow registration of middleware per action on both + resource routes and relationship routes. + ## [3.2.0] - 2023-11-08 ### Added diff --git a/LICENSE b/LICENSE index 7218413..c587f9b 100644 --- a/LICENSE +++ b/LICENSE @@ -1,202 +1,21 @@ - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright 2020 Cloud Creativity Limited. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. +The MIT License (MIT) + +Copyright (c) 2024 Cloud Creativity Ltd + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index 2808bcd..977e3d9 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ See our website, [laraveljsonapi.io](https://laraveljsonapi.io) ### Tutorial New to JSON:API and/or Laravel JSON:API? Then -the [Laravel JSON:API tutorial](https://laraveljsonapi.io/docs/2.0/tutorial/) +the [Laravel JSON:API tutorial](https://laraveljsonapi.io/4.x/tutorial/) is a great way to learn! Follow the tutorial to build a blog application with a JSON:API compliant API. @@ -129,4 +129,4 @@ To view an example Laravel application that uses this package, see the ## License -Laravel JSON:API is open-sourced software licensed under the [Apache 2.0 License](./LICENSE). +Laravel JSON:API is open-sourced software licensed under the [MIT License](./LICENSE). diff --git a/composer.json b/composer.json index 4be1273..a91b5fb 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "support": { "issues": "https://github.com/laravel-json-api/laravel/issues" }, - "license": "Apache-2.0", + "license": "MIT", "authors": [ { "name": "Cloud Creativity Ltd", @@ -23,20 +23,20 @@ } ], "require": { - "php": "^8.1", + "php": "^8.2", "ext-json": "*", - "laravel-json-api/core": "^3.3", - "laravel-json-api/eloquent": "^3.1", - "laravel-json-api/encoder-neomerx": "^3.1", - "laravel-json-api/exceptions": "^2.1", - "laravel-json-api/spec": "^2.0", - "laravel-json-api/validation": "^3.0", - "laravel/framework": "^10.0" + "laravel-json-api/core": "^5.2", + "laravel-json-api/eloquent": "^4.5", + "laravel-json-api/encoder-neomerx": "^4.2", + "laravel-json-api/exceptions": "^3.2", + "laravel-json-api/spec": "^3.2", + "laravel-json-api/validation": "^4.3", + "laravel/framework": "^11.0|^12.0" }, "require-dev": { - "laravel-json-api/testing": "^2.1", - "orchestra/testbench": "^8.0", - "phpunit/phpunit": "^10.0" + "laravel-json-api/testing": "^3.1", + "orchestra/testbench": "^9.0|^10.0", + "phpunit/phpunit": "^10.5|^11.0" }, "autoload": { "psr-4": { @@ -53,7 +53,7 @@ }, "extra": { "branch-alias": { - "dev-develop": "3.x-dev" + "dev-develop": "5.x-dev" }, "laravel": { "aliases": { diff --git a/phpunit.xml b/phpunit.xml index 539875b..f2ba358 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,9 +1,20 @@ - + diff --git a/src/Console/Concerns/ReplacesModel.php b/src/Console/Concerns/ReplacesModel.php new file mode 100644 index 0000000..b1c47c1 --- /dev/null +++ b/src/Console/Concerns/ReplacesModel.php @@ -0,0 +1,50 @@ +qualifyModel($model); + } + + $model = class_basename($model); + + $replace = [ + '{{ namespacedModel }}' => $namespacedModel, + '{{namespacedModel}}' => $namespacedModel, + '{{ model }}' => $model, + '{{model}}' => $model, + ]; + + return str_replace( + array_keys($replace), array_values($replace), $stub + ); + } +} diff --git a/src/Console/Concerns/ResolvesStub.php b/src/Console/Concerns/ResolvesStub.php index fc63283..45094fa 100644 --- a/src/Console/Concerns/ResolvesStub.php +++ b/src/Console/Concerns/ResolvesStub.php @@ -1,18 +1,10 @@ resolveStubPath('resource.stub'); } + /** + * @inheritDoc + */ + protected function buildClass($name) + { + $stub = parent::buildClass($name); + + $model = $this->option('model') ?: $this->guessModel(); + + return $this->replaceModel($stub, $model); + } + /** * @inheritDoc */ @@ -59,8 +65,8 @@ protected function getOptions() { return [ ['force', null, InputOption::VALUE_NONE, 'Create the class even if the resource already exists'], + ['model', 'm', InputOption::VALUE_REQUIRED, 'The model that the resource applies to.'], ['server', 's', InputOption::VALUE_REQUIRED, 'The JSON:API server the resource exists in.'], ]; } - } diff --git a/src/Console/MakeSchema.php b/src/Console/MakeSchema.php index 6e5e62b..764a4cc 100644 --- a/src/Console/MakeSchema.php +++ b/src/Console/MakeSchema.php @@ -1,25 +1,17 @@ replaceSchema(parent::buildClass($name)); $model = $this->option('model') ?: $this->guessModel(); @@ -77,24 +70,11 @@ protected function buildClass($name) * @param string $model * @return string */ - protected function replaceModel(string $stub, string $model): string + protected function replaceSchema(string $stub): string { - $model = str_replace('/', '\\', $model); - - if (Str::startsWith($model, '\\')) { - $namespacedModel = trim($model, '\\'); - } else { - $namespacedModel = $this->qualifyModel($model); - } - - $model = class_basename($model); $schema = $this->option('proxy') ? 'ProxySchema' : 'Schema'; $replace = [ - '{{ namespacedModel }}' => $namespacedModel, - '{{namespacedModel}}' => $namespacedModel, - '{{ model }}' => $model, - '{{model}}' => $model, '{{ schema }}' => $schema, '{{schema}}' => $schema, ]; @@ -117,5 +97,4 @@ protected function getOptions() ['server', 's', InputOption::VALUE_REQUIRED, 'The JSON:API server the schema exists in.'], ]; } - } diff --git a/src/Console/MakeServer.php b/src/Console/MakeServer.php index 2e6f74e..2a27fee 100644 --- a/src/Console/MakeServer.php +++ b/src/Console/MakeServer.php @@ -1,18 +1,10 @@ authorizer()->destroy( + $result = $route->authorizer()->destroy( $request = \request(), $model, ); - throw_if(false === $check && Auth::guest(), new AuthenticationException()); - throw_if(false === $check, new AuthorizationException()); + if ($result instanceof AuthResponse) { + try { + $result->authorize(); + } catch (AuthorizationException $ex) { + if (!$ex->hasStatus()) { + throw_if(Auth::guest(), new AuthenticationException()); + } + throw $ex; + } + } + + throw_if(false === $result && Auth::guest(), new AuthenticationException()); + throw_if(false === $result, new AuthorizationException()); } $response = null; diff --git a/src/Http/Controllers/Actions/DetachRelationship.php b/src/Http/Controllers/Actions/DetachRelationship.php index da05398..4febd0a 100644 --- a/src/Http/Controllers/Actions/DetachRelationship.php +++ b/src/Http/Controllers/Actions/DetachRelationship.php @@ -1,18 +1,10 @@ container->call([$this, 'authorize']))) { - return $passes; + try { + /** + * If the developer has implemented the `authorize` method, we + * will return the result if it is a boolean. This allows + * the developer to return a null value to indicate they want + * the default authorization to run. + */ + if (method_exists($this, 'authorize')) { + $result = $this->container->call([$this, 'authorize']); + if ($result !== null) { + return $result instanceof Response ? $result->authorize() : $result; + } } - } - /** - * If the developer has not authorized the request themselves, - * we run our default authorization as long as authorization is - * enabled for both the server and the schema (checked via the - * `mustAuthorize()` method). - */ - if (method_exists($this, 'authorizeResource')) { - return $this->container->call([$this, 'authorizeResource']); - } + /** + * If the developer has not authorized the request themselves, + * we run our default authorization as long as authorization is + * enabled for both the server and the schema (checked via the + * `mustAuthorize()` method). + */ + if (method_exists($this, 'authorizeResource')) { + $result = $this->container->call([$this, 'authorizeResource']); + return $result instanceof Response ? $result->authorize() : $result; + } + } catch (AuthorizationException $ex) { + if (!$ex->hasStatus()) { + $this->failIfUnauthenticated(); + } + throw $ex; + } return true; } - /** - * @inheritDoc - */ - protected function failedAuthorization() + protected function failIfUnauthenticated() { - /** @var Guard $auth */ + /** @var Guard $auth */ $auth = $this->container->make(Guard::class); if ($auth->guest()) { throw new AuthenticationException(); } + } + + /** + * @inheritDoc + */ + protected function failedAuthorization() + { + $this->failIfUnauthenticated(); parent::failedAuthorization(); } diff --git a/src/Http/Requests/RequestResolver.php b/src/Http/Requests/RequestResolver.php index 0f04330..731576c 100644 --- a/src/Http/Requests/RequestResolver.php +++ b/src/Http/Requests/RequestResolver.php @@ -1,18 +1,10 @@ isViewingAny()) { return $authorizer->index( diff --git a/src/Http/Requests/ResourceRequest.php b/src/Http/Requests/ResourceRequest.php index 53ec5f1..7b0ef36 100644 --- a/src/Http/Requests/ResourceRequest.php +++ b/src/Http/Requests/ResourceRequest.php @@ -1,24 +1,17 @@ isCreating()) { return $authorizer->store( diff --git a/src/LaravelJsonApi.php b/src/LaravelJsonApi.php index 586d871..64e047e 100644 --- a/src/LaravelJsonApi.php +++ b/src/LaravelJsonApi.php @@ -1,18 +1,10 @@ router = $router; $this->resource = $resource; @@ -114,7 +106,7 @@ public function withId(): self * @param string|null $method * @return ActionProxy */ - public function get(string $uri, string $method = null): ActionProxy + public function get(string $uri, ?string $method = null): ActionProxy { return $this->register('get', $uri, $method); } @@ -126,7 +118,7 @@ public function get(string $uri, string $method = null): ActionProxy * @param string|null $method * @return ActionProxy */ - public function post(string $uri, string $method = null): ActionProxy + public function post(string $uri, ?string $method = null): ActionProxy { return $this->register('post', $uri, $method); } @@ -138,7 +130,7 @@ public function post(string $uri, string $method = null): ActionProxy * @param string|null $method * @return ActionProxy */ - public function patch(string $uri, string $method = null): ActionProxy + public function patch(string $uri, ?string $method = null): ActionProxy { return $this->register('patch', $uri, $method); } @@ -150,7 +142,7 @@ public function patch(string $uri, string $method = null): ActionProxy * @param string|null $method * @return ActionProxy */ - public function put(string $uri, string $method = null): ActionProxy + public function put(string $uri, ?string $method = null): ActionProxy { return $this->register('put', $uri, $method); } @@ -162,7 +154,7 @@ public function put(string $uri, string $method = null): ActionProxy * @param string|null $method * @return ActionProxy */ - public function delete(string $uri, string $method = null): ActionProxy + public function delete(string $uri, ?string $method = null): ActionProxy { return $this->register('delete', $uri, $method); } @@ -174,7 +166,7 @@ public function delete(string $uri, string $method = null): ActionProxy * @param string|null $method * @return ActionProxy */ - public function options(string $uri, string $method = null): ActionProxy + public function options(string $uri, ?string $method = null): ActionProxy { return $this->register('options', $uri, $method); } @@ -185,7 +177,7 @@ public function options(string $uri, string $method = null): ActionProxy * @param string|null $action * @return ActionProxy */ - public function register(string $method, string $uri, string $action = null): ActionProxy + public function register(string $method, string $uri, ?string $action = null): ActionProxy { $action = $action ?: $this->guessControllerAction($uri); $parameter = $this->getParameter(); diff --git a/src/Routing/PendingRelationshipRegistration.php b/src/Routing/PendingRelationshipRegistration.php index d0a5934..8bffb48 100644 --- a/src/Routing/PendingRelationshipRegistration.php +++ b/src/Routing/PendingRelationshipRegistration.php @@ -1,18 +1,10 @@ options['middleware'] = $middleware; + if (count($middleware) === 1) { + $middleware = Arr::wrap($middleware[0]); + } + + if (array_is_list($middleware)) { + $this->options['middleware'] = $middleware; + return $this; + } + + $this->options['middleware'] = Arr::wrap($middleware['*'] ?? null); + + foreach ($this->map as $alias => $action) { + if (isset($middleware[$alias])) { + $middleware[$action] = $middleware[$alias]; + unset($middleware[$alias]); + } + } + + $this->options['action_middleware'] = $middleware; return $this; } diff --git a/src/Routing/PendingResourceRegistration.php b/src/Routing/PendingResourceRegistration.php index d07c8f3..627bd0f 100644 --- a/src/Routing/PendingResourceRegistration.php +++ b/src/Routing/PendingResourceRegistration.php @@ -1,18 +1,10 @@ options['middleware'] = $middleware; + if (count($middleware) === 1) { + $middleware = Arr::wrap($middleware[0]); + } + + if (array_is_list($middleware)) { + $this->options['middleware'] = $middleware; + return $this; + } + + $this->options['middleware'] = Arr::wrap($middleware['*'] ?? null); + $this->options['action_middleware'] = $middleware; return $this; } @@ -196,7 +199,7 @@ public function middleware(string ...$middleware): self * @param string ...$middleware * @return $this */ - public function withoutMiddleware(string ...$middleware) + public function withoutMiddleware(string ...$middleware): self { $this->options['excluded_middleware'] = array_merge( (array) ($this->options['excluded_middleware'] ?? []), @@ -226,7 +229,7 @@ public function relationships(Closure $callback): self * @param Closure|null $callback * @return $this */ - public function actions($prefixOrCallback, Closure $callback = null): self + public function actions($prefixOrCallback, ?Closure $callback = null): self { if ($prefixOrCallback instanceof Closure && null === $callback) { $this->actionsPrefix = null; diff --git a/src/Routing/PendingServerRegistration.php b/src/Routing/PendingServerRegistration.php index 9863594..0e66bd9 100644 --- a/src/Routing/PendingServerRegistration.php +++ b/src/Routing/PendingServerRegistration.php @@ -1,18 +1,10 @@ getRelationRouteName($method, $defaultName, $options); $action = ['as' => $name, 'uses' => $this->controller.'@'.$method]; + $middleware = $this->getMiddleware($method, $options); - if (isset($options['middleware'])) { - $action['middleware'] = $options['middleware']; + if (!empty($middleware)) { + $action['middleware'] = $middleware; } if (isset($options['excluded_middleware'])) { @@ -284,6 +278,22 @@ private function getRelationshipAction( return $action; } + /** + * @param string $action + * @param array $options + * @return array + */ + private function getMiddleware(string $action, array $options): array + { + $all = $options['middleware'] ?? []; + $actions = $options['action_middleware'] ?? []; + + return [ + ...$all, + ...Arr::wrap($actions[$action] ?? null), + ]; + } + /** * @param string $fieldName * @return string diff --git a/src/Routing/Relationships.php b/src/Routing/Relationships.php index 7d9dfd1..9b967f7 100644 --- a/src/Routing/Relationships.php +++ b/src/Routing/Relationships.php @@ -1,18 +1,10 @@ getResourceRouteName($resourceType, $method, $options); $action = ['as' => $name, 'uses' => $controller.'@'.$method]; + $middleware = $this->getMiddleware($method, $options); - if (isset($options['middleware'])) { - $action['middleware'] = $options['middleware']; + if (!empty($middleware)) { + $action['middleware'] = $middleware; } if (isset($options['excluded_middleware'])) { @@ -355,6 +349,22 @@ private function getResourceAction( return $action; } + /** + * @param string $action + * @param array $options + * @return array + */ + private function getMiddleware(string $action, array $options): array + { + $all = $options['middleware'] ?? []; + $actions = $options['action_middleware'] ?? []; + + return [ + ...$all, + ...Arr::wrap($actions[$action] ?? null), + ]; + } + /** * Get the action array for the relationships group. * diff --git a/src/Routing/Route.php b/src/Routing/Route.php index 1d27826..ca037a7 100644 --- a/src/Routing/Route.php +++ b/src/Routing/Route.php @@ -1,18 +1,10 @@ $this->created_at, - 'updatedAt' => $this->updated_at, + 'createdAt' => $this->resource->created_at, + 'updatedAt' => $this->resource->updated_at, ]; } diff --git a/tests/dummy/app/Http/Controllers/Api/V1/PostController.php b/tests/dummy/app/Http/Controllers/Api/V1/PostController.php index adcda8d..efafb8c 100644 --- a/tests/dummy/app/Http/Controllers/Api/V1/PostController.php +++ b/tests/dummy/app/Http/Controllers/Api/V1/PostController.php @@ -1,18 +1,10 @@ is($other); } + + /** + * Determine if the user can delete the other user. + * + * @param ?User $user + * @param User $other + * @return bool|Response + */ + public function delete(?User $user, User $other) + { + return $user?->is($other) ? true : Response::denyAsNotFound('not found message'); + } + } diff --git a/tests/dummy/app/Policies/VideoPolicy.php b/tests/dummy/app/Policies/VideoPolicy.php index 062b601..a5dd156 100644 --- a/tests/dummy/app/Policies/VideoPolicy.php +++ b/tests/dummy/app/Policies/VideoPolicy.php @@ -1,18 +1,10 @@ prefix('v1') @@ -33,7 +26,7 @@ }); /** Users */ - $server->resource('users')->only('show')->relationships(function ($relationships) { + $server->resource('users')->only('show','destroy')->relationships(function ($relationships) { $relationships->hasOne('phone'); })->actions(function ($actions) { $actions->get('me'); @@ -43,4 +36,6 @@ $server->resource('videos')->relationships(function ($relationships) { $relationships->hasMany('tags'); }); + + $server->resource('tags', '\\' . JsonApiController::class)->only('destroy'); }); diff --git a/tests/dummy/routes/web.php b/tests/dummy/routes/web.php index e6b924a..a555fe2 100644 --- a/tests/dummy/routes/web.php +++ b/tests/dummy/routes/web.php @@ -1,18 +1,10 @@ withoutExceptionHandling() ->jsonApi('users') - ->get($related = url('/api/v1/posts', [$this->post, 'author'])); + ->get($self = url('/api/v1/posts', [$this->post, 'author'])); - $response->assertFetchedOneExact($expected)->assertLinks([ - 'self' => url('/api/v1/posts', [$this->post, 'relationships', 'author']), - 'related' => $related, - ]); + $response + ->assertFetchedOneExact($expected) + ->assertLinks(['self' => $self]); } public function testFilterMatches(): void diff --git a/tests/dummy/tests/Api/V1/Posts/ReadCommentIdentifiersTest.php b/tests/dummy/tests/Api/V1/Posts/ReadCommentIdentifiersTest.php index cd55d74..21299ca 100644 --- a/tests/dummy/tests/Api/V1/Posts/ReadCommentIdentifiersTest.php +++ b/tests/dummy/tests/Api/V1/Posts/ReadCommentIdentifiersTest.php @@ -1,18 +1,10 @@ withoutExceptionHandling() ->jsonApi('comments') - ->get($related = url('/api/v1/posts', [$this->post, 'comments'])); - - $links = [ - 'self' => url('/api/v1/posts', [$this->post, 'relationships', 'comments']), - 'related' => $related, - ]; + ->get($self = url('/api/v1/posts', [$this->post, 'comments'])); $response->assertFetchedMany($expected) - ->assertLinks($links) + ->assertLinks(['self' => $self]) ->assertExactMeta(['count' => 3]); } diff --git a/tests/dummy/tests/Api/V1/Posts/ReadMediaTest.php b/tests/dummy/tests/Api/V1/Posts/ReadMediaTest.php index 98f0d84..d3de278 100644 --- a/tests/dummy/tests/Api/V1/Posts/ReadMediaTest.php +++ b/tests/dummy/tests/Api/V1/Posts/ReadMediaTest.php @@ -1,18 +1,10 @@ jsonApi('tags') - ->get(url('/api/v1/posts', [$this->post, 'tags'])); + ->get($self = url('/api/v1/posts', [$this->post, 'tags'])); - $response->assertFetchedMany($expected)->assertExactMeta([ - 'count' => count($expected) - ]); + $response + ->assertFetchedMany($expected) + ->assertExactMeta(['count' => count($expected)]) + ->assertLinks(['self' => $self]); } public function testSort(): void diff --git a/tests/dummy/tests/Api/V1/Posts/ReadTest.php b/tests/dummy/tests/Api/V1/Posts/ReadTest.php index db8ebaa..900a4b7 100644 --- a/tests/dummy/tests/Api/V1/Posts/ReadTest.php +++ b/tests/dummy/tests/Api/V1/Posts/ReadTest.php @@ -1,18 +1,10 @@ createOne(); + + $response = $this + ->actingAs(User::factory()->createOne()) + ->jsonApi('users') + ->delete(url('/api/v1/tags', $tag)); + + $response->assertNotFound()->assertErrorStatus([ + 'detail' => 'not found message', + 'status' => '404', + 'title' => 'Not Found', + ]); + } + + public function testUnauthenticated(): void + { + $tag = Tag::factory()->createOne(); + + $response = $this + ->jsonApi('users') + ->delete(url('/api/v1/tags', $tag)); + + $response->assertNotFound()->assertErrorStatus([ + 'detail' => 'not found message', + 'status' => '404', + 'title' => 'Not Found', + ]); + } +} diff --git a/tests/dummy/tests/Api/V1/TestCase.php b/tests/dummy/tests/Api/V1/TestCase.php index d82f7f4..f0f5599 100644 --- a/tests/dummy/tests/Api/V1/TestCase.php +++ b/tests/dummy/tests/Api/V1/TestCase.php @@ -1,18 +1,10 @@ createOne(); + + $response = $this + ->actingAs(User::factory()->createOne()) + ->jsonApi('users') + ->delete(url('/api/v1/users', $user)); + + $response->assertNotFound()->assertErrorStatus([ + 'detail' => 'not found message', + 'status' => '404', + 'title' => 'Not Found', + ]); + } + + public function testUnauthenticated(): void + { + $user = User::factory()->createOne(); + + $response = $this + ->jsonApi('users') + ->delete(url('/api/v1/users', $user)); + + $response->assertNotFound()->assertErrorStatus([ + 'detail' => 'not found message', + 'status' => '404', + 'title' => 'Not Found', + ]); + } +} diff --git a/tests/dummy/tests/Api/V1/Users/ReadTest.php b/tests/dummy/tests/Api/V1/Users/ReadTest.php index cecebc3..c731a4e 100644 --- a/tests/dummy/tests/Api/V1/Users/ReadTest.php +++ b/tests/dummy/tests/Api/V1/Users/ReadTest.php @@ -1,18 +1,10 @@ [ @@ -107,39 +100,81 @@ static function (PostSchema $schema, Post $post) { /** * @param Closure $scenario * @return void - * @dataProvider scenarioProvider + * @dataProvider relationshipProvider */ - public function testRelated(Closure $scenario): void + public function testRelationship(Closure $scenario): void { $expected = $scenario($this->schema, $this->post); $response = $this ->withoutExceptionHandling() ->jsonApi('tags') - ->get(url('/api/v1/posts', [$this->post, 'tags'])); + ->get(url('/api/v1/posts', [$this->post, 'relationships', 'tags'])); - $response->assertFetchedMany([$this->tag]); + $response->assertFetchedToMany([$this->tag]); if (is_array($expected)) { $response->assertLinks($expected); } } + + /** + * @return array[] + */ + public static function relatedProvider(): array + { + return [ + 'hidden' => [ + static function (PostSchema $schema) { + $schema->relationship('tags')->hidden(); + return null; + }, + ], + 'no links' => [ + static function (PostSchema $schema) { + $schema->relationship('tags')->serializeUsing( + static fn($relation) => $relation->withoutLinks() + ); + return null; + }, + ], + 'no self link' => [ + static function (PostSchema $schema, Post $post) { + $schema->relationship('tags')->serializeUsing( + static fn($relation) => $relation->withoutSelfLink() + ); + // related becomes self. + return ['self' => url('/api/v1/posts', [$post, 'tags'])]; + }, + ], + 'no related link' => [ + static function (PostSchema $schema, Post $post) { + $schema->relationship('tags')->serializeUsing( + static fn($relation) => $relation->withoutRelatedLink() + ); + // related becomes self, but it's missing so we can't do that. + return null; + }, + ], + ]; + } + /** * @param Closure $scenario * @return void - * @dataProvider scenarioProvider + * @dataProvider relatedProvider */ - public function testSelf(Closure $scenario): void + public function testRelated(Closure $scenario): void { $expected = $scenario($this->schema, $this->post); $response = $this ->withoutExceptionHandling() ->jsonApi('tags') - ->get(url('/api/v1/posts', [$this->post, 'relationships', 'tags'])); + ->get(url('/api/v1/posts', [$this->post, 'tags'])); - $response->assertFetchedToMany([$this->tag]); + $response->assertFetchedMany([$this->tag]); if (is_array($expected)) { $response->assertLinks($expected); diff --git a/tests/lib/Acceptance/Relationships/ToOneLinksTest.php b/tests/lib/Acceptance/Relationships/ToOneLinksTest.php index 12096b9..a931654 100644 --- a/tests/lib/Acceptance/Relationships/ToOneLinksTest.php +++ b/tests/lib/Acceptance/Relationships/ToOneLinksTest.php @@ -1,18 +1,10 @@ [ @@ -100,39 +92,80 @@ static function (PostSchema $schema, Post $post) { /** * @param Closure $scenario * @return void - * @dataProvider scenarioProvider + * @dataProvider relationshipProvider */ - public function testRelated(Closure $scenario): void + public function testRelationship(Closure $scenario): void { $expected = $scenario($this->schema, $this->post); $response = $this ->withoutExceptionHandling() ->jsonApi('users') - ->get(url('/api/v1/posts', [$this->post, 'author'])); + ->get(url('/api/v1/posts', [$this->post, 'relationships', 'author'])); - $response->assertFetchedOne($this->post->author); + $response->assertFetchedToOne($this->post->author); if (is_array($expected)) { $response->assertLinks($expected); } } + /** + * @return array[] + */ + public static function relatedProvider(): array + { + return [ + 'hidden' => [ + static function (PostSchema $schema) { + $schema->relationship('author')->hidden(); + return null; + }, + ], + 'no links' => [ + static function (PostSchema $schema) { + $schema->relationship('author')->serializeUsing( + static fn($relation) => $relation->withoutLinks() + ); + return null; + }, + ], + 'no self link' => [ + static function (PostSchema $schema, Post $post) { + $schema->relationship('author')->serializeUsing( + static fn($relation) => $relation->withoutSelfLink() + ); + // related becomes self + return ['self' => url('/api/v1/posts', [$post, 'author'])]; + }, + ], + 'no related link' => [ + static function (PostSchema $schema, Post $post) { + $schema->relationship('author')->serializeUsing( + static fn($relation) => $relation->withoutRelatedLink() + ); + // related becomes self but it's missing + return null; + }, + ], + ]; + } + /** * @param Closure $scenario * @return void - * @dataProvider scenarioProvider + * @dataProvider relatedProvider */ - public function testSelf(Closure $scenario): void + public function testRelated(Closure $scenario): void { $expected = $scenario($this->schema, $this->post); $response = $this ->withoutExceptionHandling() ->jsonApi('users') - ->get(url('/api/v1/posts', [$this->post, 'relationships', 'author'])); + ->get(url('/api/v1/posts', [$this->post, 'author'])); - $response->assertFetchedToOne($this->post->author); + $response->assertFetchedOne($this->post->author); if (is_array($expected)) { $response->assertLinks($expected); diff --git a/tests/lib/Acceptance/RequestBodyContentTest.php b/tests/lib/Acceptance/RequestBodyContentTest.php index 9ad3495..d493ffe 100644 --- a/tests/lib/Acceptance/RequestBodyContentTest.php +++ b/tests/lib/Acceptance/RequestBodyContentTest.php @@ -1,18 +1,10 @@ assertResourceCreated(); } + public function testModelWithoutNamespace(): void + { + config()->set('jsonapi.servers', [ + 'v1' => Server::class, + ]); + + $result = $this->artisan('jsonapi:resource posts --model BlogPost'); + + $this->assertSame(0, $result); + $this->assertResourceCreated('App\Models\BlogPost', 'BlogPost'); + } + + public function testModelWithNamespace(): void + { + config()->set('jsonapi.servers', [ + 'v1' => Server::class, + ]); + + $result = $this->artisan('jsonapi:resource', [ + 'name' => 'posts', + '--model' => '\App\Foo\Bar\BlogPost', + ]); + + $this->assertSame(0, $result); + $this->assertResourceCreated('App\Foo\Bar\BlogPost', 'BlogPost'); + } + public function testServer(): void { config()->set('jsonapi.servers', [ @@ -104,9 +122,14 @@ public function testInvalidServer(): void } /** + * @param string $namespacedModel + * @param string $model * @return void */ - private function assertResourceCreated(): void + private function assertResourceCreated( + string $namespacedModel = 'App\Models\Post', + string $model = 'Post' + ): void { $this->assertFileExists($path = app_path('JsonApi/V1/Posts/PostResource.php')); $content = file_get_contents($path); @@ -115,6 +138,8 @@ private function assertResourceCreated(): void 'namespace App\JsonApi\V1\Posts;', 'use LaravelJsonApi\Core\Resources\JsonApiResource;', 'class PostResource extends JsonApiResource', + "use {$namespacedModel};", + "@property {$model} \$resource", ]; foreach ($tests as $expected) { diff --git a/tests/lib/Integration/Console/MakeSchemaTest.php b/tests/lib/Integration/Console/MakeSchemaTest.php index de7a907..7d99697 100644 --- a/tests/lib/Integration/Console/MakeSchemaTest.php +++ b/tests/lib/Integration/Console/MakeSchemaTest.php @@ -1,18 +1,10 @@ middleware('foo') ->resources(function ($server) { $server->resource('posts')->middleware('bar')->relationships(function ($relations) { - $relations->hasMany('tags')->middleware('baz'); + $relations->hasMany('tags')->middleware('baz1', 'baz2'); + }); + }); + }); + + $route = $this->assertMatch($method, $uri); + $this->assertSame(['api', 'jsonapi:v1', 'foo', 'bar', 'baz1', 'baz2'], $route->action['middleware']); + } + + /** + * @param string $method + * @param string $uri + * @dataProvider genericProvider + */ + public function testMiddlewareAsArrayList(string $method, string $uri): void + { + $server = $this->createServer('v1'); + $schema = $this->createSchema($server, 'posts', '\d+'); + $this->createRelation($schema, 'tags'); + + $this->defaultApiRoutesWithNamespace(function () { + JsonApiRoute::server('v1') + ->prefix('v1') + ->namespace('Api\\V1') + ->middleware('foo') + ->resources(function ($server) { + $server->resource('posts')->middleware('bar')->relationships(function ($relations) { + $relations->hasMany('tags')->middleware(['baz1', 'baz2']); }); }); }); $route = $this->assertMatch($method, $uri); - $this->assertSame(['api', 'jsonapi:v1', 'foo', 'bar', 'baz'], $route->action['middleware']); + $this->assertSame(['api', 'jsonapi:v1', 'foo', 'bar', 'baz1', 'baz2'], $route->action['middleware']); + } + + /** + * @param string $method + * @param string $uri + * @param string $action + * @dataProvider genericProvider + */ + public function testActionMiddleware(string $method, string $uri, string $action): void + { + $actions = [ + '*' => ['baz1', 'baz2'], + 'showRelated' => 'showRelated1', + 'showRelationship' => ['showRelationship1', 'showRelationship2'], + 'updateRelationship' => 'updateRelationship1', + 'attachRelationship' => ['attachRelationship1', 'attachRelationship2'], + 'detachRelationship' => 'detachRelationship1', + ]; + + $expected = [ + 'api', + 'jsonapi:v1', + 'foo', + 'bar', + ...$actions['*'], + ...Arr::wrap($actions[$action]), + ]; + + $server = $this->createServer('v1'); + $schema = $this->createSchema($server, 'posts', '\d+'); + $this->createRelation($schema, 'tags'); + + $this->defaultApiRoutesWithNamespace(function () use ($actions) { + JsonApiRoute::server('v1') + ->prefix('v1') + ->namespace('Api\\V1') + ->middleware('foo') + ->resources(function ($server) use ($actions) { + $server->resource('posts')->middleware('bar')->relationships( + function ($relations) use ($actions) { + $relations->hasMany('tags')->middleware([ + '*' => $actions['*'], + 'related' => $actions['showRelated'], + 'show' => $actions['showRelationship'], + 'update' => $actions['updateRelationship'], + 'attach' => $actions['attachRelationship'], + 'detach' => $actions['detachRelationship'], + ]); + }, + ); + }); + }); + + $route = $this->assertMatch($method, $uri); + $this->assertSame($expected, $route->action['middleware']); } /** diff --git a/tests/lib/Integration/Routing/HasOneTest.php b/tests/lib/Integration/Routing/HasOneTest.php index 8a85cc6..a2d8e23 100644 --- a/tests/lib/Integration/Routing/HasOneTest.php +++ b/tests/lib/Integration/Routing/HasOneTest.php @@ -1,18 +1,10 @@ createServer('v1'); $schema = $this->createSchema($server, 'posts', '\d+'); @@ -134,13 +126,91 @@ public function testMiddleware(string $method, string $uri, string $action, stri ->middleware('foo') ->resources(function ($server) { $server->resource('posts')->middleware('bar')->relationships(function ($relations) { - $relations->hasOne('author')->middleware('baz'); + $relations->hasOne('author')->middleware('baz1', 'baz2'); + }); + }); + }); + + $route = $this->assertMatch($method, $uri); + $this->assertSame(['api', 'jsonapi:v1', 'foo', 'bar', 'baz1', 'baz2'], $route->action['middleware']); + } + + /** + * @param string $method + * @param string $uri + * @dataProvider genericProvider + */ + public function testMiddlewareAsArrayList(string $method, string $uri): void + { + $server = $this->createServer('v1'); + $schema = $this->createSchema($server, 'posts', '\d+'); + $this->createRelation($schema, 'author'); + + $this->defaultApiRoutesWithNamespace(function () { + JsonApiRoute::server('v1') + ->prefix('v1') + ->namespace('Api\\V1') + ->middleware('foo') + ->resources(function (ResourceRegistrar $server) { + $server->resource('posts')->middleware('bar')->relationships(function (Relationships $relations) { + $relations->hasOne('author')->middleware(['baz1', 'baz2']); }); }); }); $route = $this->assertMatch($method, $uri); - $this->assertSame(['api', 'jsonapi:v1', 'foo', 'bar', 'baz'], $route->action['middleware']); + $this->assertSame(['api', 'jsonapi:v1', 'foo', 'bar', 'baz1', 'baz2'], $route->action['middleware']); + } + + /** + * @param string $method + * @param string $uri + * @param string $action + * @dataProvider genericProvider + */ + public function testActionMiddleware(string $method, string $uri, string $action): void + { + $actions = [ + '*' => ['baz1', 'baz2'], + 'showRelated' => 'showRelated1', + 'showRelationship' => ['showRelationship1', 'showRelationship2'], + 'updateRelationship' => 'updateRelationship1', + ]; + + $expected = [ + 'api', + 'jsonapi:v1', + 'foo', + 'bar', + ...$actions['*'], + ...Arr::wrap($actions[$action]), + ]; + + $server = $this->createServer('v1'); + $schema = $this->createSchema($server, 'posts', '\d+'); + $this->createRelation($schema, 'author'); + + $this->defaultApiRoutesWithNamespace(function () use ($actions) { + JsonApiRoute::server('v1') + ->prefix('v1') + ->namespace('Api\\V1') + ->middleware('foo') + ->resources(function (ResourceRegistrar $server) use ($actions) { + $server->resource('posts')->middleware('bar')->relationships( + function (Relationships $relations) use ($actions) { + $relations->hasOne('author')->middleware([ + '*' => $actions['*'], + 'related' => $actions['showRelated'], + 'show' => $actions['showRelationship'], + 'update' => $actions['updateRelationship'], + ]); + }, + ); + }); + }); + + $route = $this->assertMatch($method, $uri); + $this->assertSame($expected, $route->action['middleware']); } /** diff --git a/tests/lib/Integration/Routing/ResourceTest.php b/tests/lib/Integration/Routing/ResourceTest.php index 2dad057..bb3c152 100644 --- a/tests/lib/Integration/Routing/ResourceTest.php +++ b/tests/lib/Integration/Routing/ResourceTest.php @@ -1,18 +1,10 @@ prefix('v1') ->namespace('Api\\V1') - ->middleware('foo') + ->middleware('foo', 'bar') ->resources(function ($server) { $server->resource('posts'); }); }); $route = $this->assertMatch($method, $uri); - $this->assertSame(['api', 'jsonapi:v1', 'foo'], $route->action['middleware']); + $this->assertSame(['api', 'jsonapi:v1', 'foo', 'bar'], $route->action['middleware']); } /** @@ -251,6 +244,97 @@ public function testResourceMiddleware(string $method, string $uri): void $this->assertSame(['api', 'jsonapi:v1', 'foo', 'bar'], $route->action['middleware']); } + /** + * @param string $method + * @param string $uri + * @dataProvider routeProvider + */ + public function testResourceWithMultipleMiddleware(string $method, string $uri): void + { + $server = $this->createServer('v1'); + $this->createSchema($server, 'posts', '\d+'); + + $this->defaultApiRoutesWithNamespace(function () { + JsonApiRoute::server('v1') + ->prefix('v1') + ->namespace('Api\\V1') + ->middleware('foo') + ->resources(function ($server) { + $server->resource('posts')->middleware('bar1', 'bar2'); + }); + }); + + $route = $this->assertMatch($method, $uri); + $this->assertSame(['api', 'jsonapi:v1', 'foo', 'bar1', 'bar2'], $route->action['middleware']); + } + + /** + * @param string $method + * @param string $uri + * @dataProvider routeProvider + */ + public function testResourceMiddlewareArrayList(string $method, string $uri): void + { + $server = $this->createServer('v1'); + $this->createSchema($server, 'posts', '\d+'); + + $this->defaultApiRoutesWithNamespace(function () { + JsonApiRoute::server('v1') + ->prefix('v1') + ->namespace('Api\\V1') + ->middleware('foo') + ->resources(function (ResourceRegistrar $server) { + $server->resource('posts')->middleware(['bar1', 'bar2']); + }); + }); + + $route = $this->assertMatch($method, $uri); + $this->assertSame(['api', 'jsonapi:v1', 'foo', 'bar1', 'bar2'], $route->action['middleware']); + } + + + /** + * @param string $method + * @param string $uri + * @param string $action + * @dataProvider routeProvider + */ + public function testResourceActionMiddleware(string $method, string $uri, string $action): void + { + $actions = [ + '*' => ['bar1', 'bar2'], + 'index' => 'index1', + 'store' => ['store1', 'store2'], + 'show' => ['show1'], + 'update' => 'update1', + 'destroy' => 'destroy1', + ]; + + $expected = [ + 'api', + 'jsonapi:v1', + 'foo', + ...$actions['*'], + ...Arr::wrap($actions[$action]), + ]; + + $server = $this->createServer('v1'); + $this->createSchema($server, 'posts', '\d+'); + + $this->defaultApiRoutesWithNamespace(function () use ($actions) { + JsonApiRoute::server('v1') + ->prefix('v1') + ->namespace('Api\\V1') + ->middleware('foo') + ->resources(function (ResourceRegistrar $server) use ($actions) { + $server->resource('posts')->middleware($actions); + }); + }); + + $route = $this->assertMatch($method, $uri); + $this->assertSame($expected, $route->action['middleware']); + } + /** * @param string $method * @param string $uri diff --git a/tests/lib/Integration/Routing/TestCase.php b/tests/lib/Integration/Routing/TestCase.php index 6aa5e45..d8d589f 100644 --- a/tests/lib/Integration/Routing/TestCase.php +++ b/tests/lib/Integration/Routing/TestCase.php @@ -1,18 +1,10 @@ createMock(Schema::class); @@ -97,7 +89,7 @@ protected function createSchema( * @param string|null $uriName * @return void */ - protected function createRelation(MockObject $schema, string $fieldName, string $uriName = null): void + protected function createRelation(MockObject $schema, string $fieldName, ?string $uriName = null): void { $relation = $this->createMock(Relation::class); $relation->method('name')->willReturn($fieldName); diff --git a/tests/lib/Integration/TestCase.php b/tests/lib/Integration/TestCase.php index 2b108e4..cee8398 100644 --- a/tests/lib/Integration/TestCase.php +++ b/tests/lib/Integration/TestCase.php @@ -1,18 +1,10 @@