diff --git a/src/Auth/BaseCanDirective.php b/src/Auth/BaseCanDirective.php index b90506922..9e0efb8e5 100644 --- a/src/Auth/BaseCanDirective.php +++ b/src/Auth/BaseCanDirective.php @@ -2,32 +2,14 @@ namespace Nuwave\Lighthouse\Auth; -use GraphQL\Error\Error; -use GraphQL\Language\AST\FieldDefinitionNode; -use GraphQL\Language\AST\InterfaceTypeDefinitionNode; -use GraphQL\Language\AST\ObjectTypeDefinitionNode; use Illuminate\Contracts\Auth\Access\Gate; -use Illuminate\Contracts\Pagination\Paginator; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\ModelNotFoundException; -use Illuminate\Support\Arr; use Nuwave\Lighthouse\Exceptions\AuthorizationException; -use Nuwave\Lighthouse\Exceptions\DefinitionException; -use Nuwave\Lighthouse\Execution\Resolved; use Nuwave\Lighthouse\Execution\ResolveInfo; -use Nuwave\Lighthouse\Schema\AST\DocumentAST; use Nuwave\Lighthouse\Schema\Directives\BaseDirective; -use Nuwave\Lighthouse\Schema\RootType; use Nuwave\Lighthouse\Schema\Values\FieldValue; -use Nuwave\Lighthouse\SoftDeletes\ForceDeleteDirective; -use Nuwave\Lighthouse\SoftDeletes\RestoreDirective; -use Nuwave\Lighthouse\SoftDeletes\TrashedDirective; -use Nuwave\Lighthouse\Support\Contracts\FieldManipulator; use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware; use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; use Nuwave\Lighthouse\Support\Utils; -use Throwable; abstract class BaseCanDirective extends BaseDirective implements FieldMiddleware { @@ -103,13 +85,13 @@ public function handleField(FieldValue $fieldValue): void $fieldValue->wrapResolver(fn (callable $resolver): \Closure => function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($resolver, $ability) { $gate = $this->gate->forUser($context->user()); $checkArguments = $this->buildCheckArguments($args); - $authorizeModel = fn(string|object|array|null $model) => $this->authorizeModel($gate, $ability, $model, $checkArguments); + $authorizeModel = fn (string|object|array|null $model) => $this->authorizeModel($gate, $ability, $model, $checkArguments); try { return $this->authorizeRequest($root, $args, $context, $resolveInfo, $resolver, $authorizeModel); - } catch (Throwable $e) { + } catch (\Throwable $e) { $action = $this->directiveArgValue('action'); - if ($action === 'EXCEPTION_NOT_AUTHORIZED'){ + if ($action === 'EXCEPTION_NOT_AUTHORIZED') { throw new AuthorizationException(); } @@ -117,24 +99,21 @@ public function handleField(FieldValue $fieldValue): void return $this->directiveArgValue('return_value'); } - throw $e; } }); } /** - * Authorizes request and resolves the field + * Authorizes request and resolves the field. * - * @param array $args - * @throws \Nuwave\Lighthouse\Exceptions\AuthorizationException + * @param array $args */ - protected abstract function authorizeRequest(mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo, callable $resolver, callable $authorize): mixed; + abstract protected function authorizeRequest(mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo, callable $resolver, callable $authorize): mixed; /** - * @param string|array $ability - * @param array $arguments - * @throws \Nuwave\Lighthouse\Exceptions\AuthorizationException + * @param string|array $ability + * @param array $arguments */ protected function authorizeModel(Gate $gate, string|array $ability, mixed $model, array $arguments): void { diff --git a/src/Auth/CanFindDirective.php b/src/Auth/CanFindDirective.php index da68ff2f5..3a7ba8841 100644 --- a/src/Auth/CanFindDirective.php +++ b/src/Auth/CanFindDirective.php @@ -3,28 +3,14 @@ namespace Nuwave\Lighthouse\Auth; use GraphQL\Error\Error; -use GraphQL\Language\AST\FieldDefinitionNode; -use GraphQL\Language\AST\InterfaceTypeDefinitionNode; -use GraphQL\Language\AST\ObjectTypeDefinitionNode; -use Illuminate\Contracts\Auth\Access\Gate; -use Illuminate\Contracts\Pagination\Paginator; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Support\Arr; -use Nuwave\Lighthouse\Exceptions\AuthorizationException; -use Nuwave\Lighthouse\Exceptions\DefinitionException; -use Nuwave\Lighthouse\Execution\Resolved; use Nuwave\Lighthouse\Execution\ResolveInfo; -use Nuwave\Lighthouse\Schema\AST\DocumentAST; -use Nuwave\Lighthouse\Schema\Directives\BaseDirective; -use Nuwave\Lighthouse\Schema\RootType; -use Nuwave\Lighthouse\Schema\Values\FieldValue; use Nuwave\Lighthouse\SoftDeletes\ForceDeleteDirective; use Nuwave\Lighthouse\SoftDeletes\RestoreDirective; use Nuwave\Lighthouse\SoftDeletes\TrashedDirective; -use Nuwave\Lighthouse\Support\Contracts\FieldManipulator; -use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware; use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; use Nuwave\Lighthouse\Support\Utils; @@ -33,6 +19,7 @@ class CanFindDirective extends BaseCanDirective public static function definition(): string { $commonArguments = BaseCanDirective::commonArguments(); + return /** @lang GraphQL */ <<modelsToCheck($root, $args, $context, $resolveInfo) as $model) { $authorize($model); } + return $resolver($root, $args, $context, $resolveInfo); } diff --git a/src/Auth/CanModelDirective.php b/src/Auth/CanModelDirective.php index e98fec611..d1c0d490b 100644 --- a/src/Auth/CanModelDirective.php +++ b/src/Auth/CanModelDirective.php @@ -2,37 +2,15 @@ namespace Nuwave\Lighthouse\Auth; -use GraphQL\Error\Error; -use GraphQL\Language\AST\FieldDefinitionNode; -use GraphQL\Language\AST\InterfaceTypeDefinitionNode; -use GraphQL\Language\AST\ObjectTypeDefinitionNode; -use Illuminate\Contracts\Auth\Access\Gate; -use Illuminate\Contracts\Pagination\Paginator; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\ModelNotFoundException; -use Illuminate\Support\Arr; -use Nuwave\Lighthouse\Exceptions\AuthorizationException; -use Nuwave\Lighthouse\Exceptions\DefinitionException; -use Nuwave\Lighthouse\Execution\Resolved; use Nuwave\Lighthouse\Execution\ResolveInfo; -use Nuwave\Lighthouse\Schema\AST\DocumentAST; -use Nuwave\Lighthouse\Schema\Directives\BaseDirective; -use Nuwave\Lighthouse\Schema\RootType; -use Nuwave\Lighthouse\Schema\Values\FieldValue; -use Nuwave\Lighthouse\SoftDeletes\ForceDeleteDirective; -use Nuwave\Lighthouse\SoftDeletes\RestoreDirective; -use Nuwave\Lighthouse\SoftDeletes\TrashedDirective; -use Nuwave\Lighthouse\Support\Contracts\FieldManipulator; -use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware; use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; -use Nuwave\Lighthouse\Support\Utils; class CanModelDirective extends BaseCanDirective { public static function definition(): string { $commonArguments = BaseCanDirective::commonArguments(); + return /** @lang GraphQL */ <<getModelClass()); + return $resolver($root, $args, $context, $resolveInfo); } } diff --git a/src/Auth/CanQueryDirective.php b/src/Auth/CanQueryDirective.php index 382be8c86..c8046f454 100644 --- a/src/Auth/CanQueryDirective.php +++ b/src/Auth/CanQueryDirective.php @@ -2,37 +2,15 @@ namespace Nuwave\Lighthouse\Auth; -use GraphQL\Error\Error; -use GraphQL\Language\AST\FieldDefinitionNode; -use GraphQL\Language\AST\InterfaceTypeDefinitionNode; -use GraphQL\Language\AST\ObjectTypeDefinitionNode; -use Illuminate\Contracts\Auth\Access\Gate; -use Illuminate\Contracts\Pagination\Paginator; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\ModelNotFoundException; -use Illuminate\Support\Arr; -use Nuwave\Lighthouse\Exceptions\AuthorizationException; -use Nuwave\Lighthouse\Exceptions\DefinitionException; -use Nuwave\Lighthouse\Execution\Resolved; use Nuwave\Lighthouse\Execution\ResolveInfo; -use Nuwave\Lighthouse\Schema\AST\DocumentAST; -use Nuwave\Lighthouse\Schema\Directives\BaseDirective; -use Nuwave\Lighthouse\Schema\RootType; -use Nuwave\Lighthouse\Schema\Values\FieldValue; -use Nuwave\Lighthouse\SoftDeletes\ForceDeleteDirective; -use Nuwave\Lighthouse\SoftDeletes\RestoreDirective; -use Nuwave\Lighthouse\SoftDeletes\TrashedDirective; -use Nuwave\Lighthouse\Support\Contracts\FieldManipulator; -use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware; use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; -use Nuwave\Lighthouse\Support\Utils; class CanQueryDirective extends BaseCanDirective { public static function definition(): string { $commonArguments = BaseCanDirective::commonArguments(); + return /** @lang GraphQL */ <<get(); - foreach ($models as $model){ + foreach ($models as $model) { $authorize($model); } + return $resolver($root, $args, $context, $resolveInfo); } } diff --git a/src/Auth/CanResolvedDirective.php b/src/Auth/CanResolvedDirective.php index 53e7cb65a..5bd5e2614 100644 --- a/src/Auth/CanResolvedDirective.php +++ b/src/Auth/CanResolvedDirective.php @@ -2,29 +2,17 @@ namespace Nuwave\Lighthouse\Auth; -use GraphQL\Error\Error; use GraphQL\Language\AST\FieldDefinitionNode; use GraphQL\Language\AST\InterfaceTypeDefinitionNode; use GraphQL\Language\AST\ObjectTypeDefinitionNode; -use Illuminate\Contracts\Auth\Access\Gate; use Illuminate\Contracts\Pagination\Paginator; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\ModelNotFoundException; -use Illuminate\Support\Arr; -use Nuwave\Lighthouse\Exceptions\AuthorizationException; use Nuwave\Lighthouse\Exceptions\DefinitionException; use Nuwave\Lighthouse\Execution\Resolved; use Nuwave\Lighthouse\Execution\ResolveInfo; use Nuwave\Lighthouse\Schema\AST\DocumentAST; -use Nuwave\Lighthouse\Schema\Directives\BaseDirective; use Nuwave\Lighthouse\Schema\RootType; -use Nuwave\Lighthouse\Schema\Values\FieldValue; -use Nuwave\Lighthouse\SoftDeletes\ForceDeleteDirective; -use Nuwave\Lighthouse\SoftDeletes\RestoreDirective; -use Nuwave\Lighthouse\SoftDeletes\TrashedDirective; use Nuwave\Lighthouse\Support\Contracts\FieldManipulator; -use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware; use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; use Nuwave\Lighthouse\Support\Utils; @@ -33,6 +21,7 @@ class CanResolvedDirective extends BaseCanDirective implements FieldManipulator public static function definition(): string { $commonArguments = BaseCanDirective::commonArguments(); + return /** @lang GraphQL */ <<name->value === RootType::MUTATION) { - throw new DefinitionException("Do not use @can with `resolved` on mutation $fieldDefinition->name->value, it is unsafe as the resolver will run before checking permissions. Use `query` or `find`."); + throw new DefinitionException("Do not use @can with `resolved` on mutation {$fieldDefinition->name}->value, it is unsafe as the resolver will run before checking permissions. Use `query` or `find`."); } } - } diff --git a/src/Auth/CanRootDirective.php b/src/Auth/CanRootDirective.php index ece2d90e3..4f1814b28 100644 --- a/src/Auth/CanRootDirective.php +++ b/src/Auth/CanRootDirective.php @@ -2,37 +2,15 @@ namespace Nuwave\Lighthouse\Auth; -use GraphQL\Error\Error; -use GraphQL\Language\AST\FieldDefinitionNode; -use GraphQL\Language\AST\InterfaceTypeDefinitionNode; -use GraphQL\Language\AST\ObjectTypeDefinitionNode; -use Illuminate\Contracts\Auth\Access\Gate; -use Illuminate\Contracts\Pagination\Paginator; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\ModelNotFoundException; -use Illuminate\Support\Arr; -use Nuwave\Lighthouse\Exceptions\AuthorizationException; -use Nuwave\Lighthouse\Exceptions\DefinitionException; -use Nuwave\Lighthouse\Execution\Resolved; use Nuwave\Lighthouse\Execution\ResolveInfo; -use Nuwave\Lighthouse\Schema\AST\DocumentAST; -use Nuwave\Lighthouse\Schema\Directives\BaseDirective; -use Nuwave\Lighthouse\Schema\RootType; -use Nuwave\Lighthouse\Schema\Values\FieldValue; -use Nuwave\Lighthouse\SoftDeletes\ForceDeleteDirective; -use Nuwave\Lighthouse\SoftDeletes\RestoreDirective; -use Nuwave\Lighthouse\SoftDeletes\TrashedDirective; -use Nuwave\Lighthouse\Support\Contracts\FieldManipulator; -use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware; use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; -use Nuwave\Lighthouse\Support\Utils; class CanRootDirective extends BaseCanDirective { public static function definition(): string { $commonArguments = BaseCanDirective::commonArguments(); + return /** @lang GraphQL */ <<|array|\Google\Protobuf\Internal\RepeatedField $cache_latency_count * This array includes the latency buckets for all operations included in cache_hits - * See comment on latency_count for details. + * See comment on latency_count for details * @var \Nuwave\Lighthouse\Tracing\FederatedTracing\Proto\PathErrorStats $root_error_stats * Paths and counts for each error. The total number of requests with errors within this object should be the same as * requests_with_errors_count below. diff --git a/src/Tracing/FederatedTracing/Proto/Trace.php b/src/Tracing/FederatedTracing/Proto/Trace.php index a8fab6516..f8fe37ed3 100644 --- a/src/Tracing/FederatedTracing/Proto/Trace.php +++ b/src/Tracing/FederatedTracing/Proto/Trace.php @@ -170,15 +170,15 @@ class Trace extends \Google\Protobuf\Internal\Message * Optional. Data for populating the Message object. * * @var \Google\Protobuf\Timestamp $start_time - * Wallclock time when the trace began. + * Wallclock time when the trace began * @var \Google\Protobuf\Timestamp $end_time - * Wallclock time when the trace ended. + * Wallclock time when the trace ended * @var int|string $duration_ns * High precision duration of the trace; may not equal end_time-start_time - * (eg, if your machine's clock changed during the trace). + * (eg, if your machine's clock changed during the trace) * @var \Nuwave\Lighthouse\Tracing\FederatedTracing\Proto\Trace\Node $root * A tree containing information about all resolvers run directly by this - * service, including errors. + * service, including errors * @var bool $is_incomplete * If this is true, the trace is potentially missing some nodes that were * present on the query plan. This can happen if the trace span buffer used @@ -201,7 +201,7 @@ class Trace extends \Google\Protobuf\Internal\Message * @var string $unexecutedOperationBody * Optional: when GraphQL parsing or validation against the GraphQL schema fails, these fields * can include reference to the operation being sent for users to dig into the set of operations - * that are failing validation. + * that are failing validation * @var string $unexecutedOperationName * @var \Nuwave\Lighthouse\Tracing\FederatedTracing\Proto\Trace\Details $details * @var string $client_name diff --git a/src/Tracing/FederatedTracing/Proto/Trace/QueryPlanNode/FetchNode.php b/src/Tracing/FederatedTracing/Proto/Trace/QueryPlanNode/FetchNode.php index f1a4a029f..edc48c865 100644 --- a/src/Tracing/FederatedTracing/Proto/Trace/QueryPlanNode/FetchNode.php +++ b/src/Tracing/FederatedTracing/Proto/Trace/QueryPlanNode/FetchNode.php @@ -66,12 +66,12 @@ class FetchNode extends \Google\Protobuf\Internal\Message * @var \Nuwave\Lighthouse\Tracing\FederatedTracing\Proto\Trace $trace * This Trace only contains start_time, end_time, duration_ns, and root; * all timings were calculated **on the subgraph**, and clock skew - * will be handled by the ingress server. + * will be handled by the ingress server * @var int|string $sent_time_offset - * relative to the outer trace's start_time, in ns, measured in the Router/Gateway. + * relative to the outer trace's start_time, in ns, measured in the Router/Gateway * @var \Google\Protobuf\Timestamp $sent_time * Wallclock times measured in the Router/Gateway for when this operation was - * sent and received. + * sent and received * @var \Google\Protobuf\Timestamp $received_time * } */ diff --git a/tests/Integration/Auth/CanFindDirectiveDBTest.php b/tests/Integration/Auth/CanFindDirectiveDBTest.php index 7d5e3c925..b307b0791 100644 --- a/tests/Integration/Auth/CanFindDirectiveDBTest.php +++ b/tests/Integration/Auth/CanFindDirectiveDBTest.php @@ -5,7 +5,6 @@ use Nuwave\Lighthouse\Auth\CanDirective; use Nuwave\Lighthouse\Exceptions\AuthorizationException; use Tests\DBTestCase; -use Tests\Utils\Models\Company; use Tests\Utils\Models\Post; use Tests\Utils\Models\Task; use Tests\Utils\Models\User; diff --git a/tests/Integration/Auth/CanQueryDirectiveDBTest.php b/tests/Integration/Auth/CanQueryDirectiveDBTest.php index 54d6208c5..702ca19e8 100644 --- a/tests/Integration/Auth/CanQueryDirectiveDBTest.php +++ b/tests/Integration/Auth/CanQueryDirectiveDBTest.php @@ -2,10 +2,7 @@ namespace Auth; -use Nuwave\Lighthouse\Auth\CanDirective; -use Nuwave\Lighthouse\Exceptions\AuthorizationException; use Tests\DBTestCase; -use Tests\Utils\Models\Company; use Tests\Utils\Models\Post; use Tests\Utils\Models\Task; use Tests\Utils\Models\User; diff --git a/tests/Integration/Auth/CanResolvedDirectiveDBTest.php b/tests/Integration/Auth/CanResolvedDirectiveDBTest.php index a5bbe8076..dd6924ebf 100644 --- a/tests/Integration/Auth/CanResolvedDirectiveDBTest.php +++ b/tests/Integration/Auth/CanResolvedDirectiveDBTest.php @@ -2,12 +2,8 @@ namespace Auth; -use Nuwave\Lighthouse\Auth\CanDirective; -use Nuwave\Lighthouse\Exceptions\AuthorizationException; use Tests\DBTestCase; use Tests\Utils\Models\Company; -use Tests\Utils\Models\Post; -use Tests\Utils\Models\Task; use Tests\Utils\Models\User; use Tests\Utils\Policies\UserPolicy; diff --git a/tests/Unit/Auth/CanDirectiveTestBase.php b/tests/Unit/Auth/CanDirectiveTestBase.php index dd8ef8ca7..c3bd0fc02 100644 --- a/tests/Unit/Auth/CanDirectiveTestBase.php +++ b/tests/Unit/Auth/CanDirectiveTestBase.php @@ -2,7 +2,6 @@ namespace Tests\Unit\Auth; -use Nuwave\Lighthouse\Auth\CanDirective; use Nuwave\Lighthouse\Exceptions\AuthorizationException; use Tests\TestCase; use Tests\Utils\Models\User; @@ -10,7 +9,7 @@ abstract class CanDirectiveTestBase extends TestCase { - abstract static function getSchema(string $commonArgs): string; + abstract public static function getSchema(string $commonArgs): string; protected function getQuery(): string { @@ -84,7 +83,7 @@ public function testPassesAuthIfAuthorized(): void $user->name = UserPolicy::ADMIN; $this->be($user); - $this->mockResolver(fn(): User => $this->resolveUser()); + $this->mockResolver(fn (): User => $this->resolveUser()); $this->schema = $this->getSchema('ability: "adminOnly"'); @@ -99,7 +98,7 @@ public function testPassesAuthIfAuthorized(): void public function testAcceptsGuestUser(): void { - $this->mockResolver(fn(): User => $this->resolveUser()); + $this->mockResolver(fn (): User => $this->resolveUser()); $this->schema = $this->getSchema('ability: "guestOnly"'); @@ -118,7 +117,7 @@ public function testPassesMultiplePolicies(): void $user->name = UserPolicy::ADMIN; $this->be($user); - $this->mockResolver(fn(): User => $this->resolveUser()); + $this->mockResolver(fn (): User => $this->resolveUser()); $this->schema = $this->getSchema('ability: ["adminOnly", "alwaysTrue"]'); @@ -142,7 +141,7 @@ public function testInjectArgsPassesClientArgumentToPolicy(): void { $this->be(new User()); - $this->mockResolver(fn(): User => $this->resolveUser()); + $this->mockResolver(fn (): User => $this->resolveUser()); $this->schema = $this->getSchema('ability: "injectArgs", injectArgs: [true]'); @@ -159,7 +158,7 @@ public function testInjectedArgsAndStaticArgs(): void { $this->be(new User()); - $this->mockResolver(fn(): User => $this->resolveUser()); + $this->mockResolver(fn (): User => $this->resolveUser()); $this->schema = $this->getSchema('ability: "argsWithInjectedArgs", args: { foo: "static" }, injectArgs: true'); diff --git a/tests/Unit/Auth/CanModelDirectiveTest.php b/tests/Unit/Auth/CanModelDirectiveTest.php index d23d8c7a5..6a393e520 100644 --- a/tests/Unit/Auth/CanModelDirectiveTest.php +++ b/tests/Unit/Auth/CanModelDirectiveTest.php @@ -3,17 +3,15 @@ namespace Auth; use Tests\Unit\Auth\CanDirectiveTestBase; -use Tests\Utils\Models\User; -use Tests\Utils\Policies\UserPolicy; class CanModelDirectiveTest extends CanDirectiveTestBase { - static function getSchema(string $commonArgs): string + public static function getSchema(string $commonArgs): string { return /** @lang GraphQL */ " type Query { user(foo: String): User - @canModel($commonArgs) + @canModel({$commonArgs}) @mock } diff --git a/tests/Unit/Auth/CanResolvedDirectiveTest.php b/tests/Unit/Auth/CanResolvedDirectiveTest.php index 68bc4a4b0..13ee12c29 100644 --- a/tests/Unit/Auth/CanResolvedDirectiveTest.php +++ b/tests/Unit/Auth/CanResolvedDirectiveTest.php @@ -7,12 +7,12 @@ class CanResolvedDirectiveTest extends CanDirectiveTestBase { - static function getSchema(string $commonArgs): string + public static function getSchema(string $commonArgs): string { return /** @lang GraphQL */ " type Query { user(foo: String): User - @canResolved($commonArgs) + @canResolved({$commonArgs}) @mock } @@ -46,7 +46,6 @@ public function testReturnsValue(): void parent::testReturnsValue(); } - public function testProcessesTheArgsArgument(): void { $this->mockResolver(fn (): User => $this->resolveUser()); diff --git a/tests/Unit/Auth/CanRootDirectiveTest.php b/tests/Unit/Auth/CanRootDirectiveTest.php index f0ab265e1..e6a7bb8ef 100644 --- a/tests/Unit/Auth/CanRootDirectiveTest.php +++ b/tests/Unit/Auth/CanRootDirectiveTest.php @@ -9,7 +9,7 @@ class CanRootDirectiveTest extends CanDirectiveTestBase { - static function getSchema(string $commonArgs): string + public static function getSchema(string $commonArgs): string { return /** @lang GraphQL */ " type Query { @@ -18,12 +18,11 @@ static function getSchema(string $commonArgs): string } type User { - name(foo: String): String @canRoot($commonArgs) + name(foo: String): String @canRoot({$commonArgs}) } "; } - protected function getQuery(): string { return /** @lang GraphQL */ ' @@ -105,14 +104,14 @@ public function testChecksAgainstObject(): void $user->name = UserPolicy::ADMIN; $this->be($user); - $return = new class { + $return = new class() { public string $name = 'foo'; }; $this->mockResolver(fn (): object => $return); $this->app ->make(Gate::class) - ->define('customObject', fn(User $authorizedUser, object $root) => $authorizedUser === $user && $root == $return); + ->define('customObject', fn (User $authorizedUser, object $root) => $authorizedUser === $user && $root == $return); $this->schema = $this->getSchema('ability: "customObject"'); @@ -136,7 +135,7 @@ public function testChecksAgainstArray(): void $this->app ->make(Gate::class) - ->define('customArray', fn(User $authorizedUser, array $root) => $authorizedUser === $user && $root == $return); + ->define('customArray', fn (User $authorizedUser, array $root) => $authorizedUser === $user && $root == $return); $this->schema = $this->getSchema('ability: "customArray"'); @@ -149,27 +148,26 @@ public function testChecksAgainstArray(): void ]); } - public function testGlobalGate(): void { $user = new User(); $this->be($user); - $this->app->make(Gate::class)->define('globalAdmin', fn($authorizedUser) => $authorizedUser === $user); + $this->app->make(Gate::class)->define('globalAdmin', fn ($authorizedUser) => $authorizedUser === $user); $this->mockResolver(fn (): User => $this->resolveUser()); - $this->schema = /** @lang GraphQL */ " + $this->schema = /** @lang GraphQL */ ' type Query { user: User! - @canRoot(ability: \"globalAdmin\") + @canRoot(ability: "globalAdmin") @mock } type User { name(foo: String): String } - "; + '; $this->graphQL($this->getQuery())->assertJson([ 'data' => [ diff --git a/tests/Utils/Policies/UserPolicy.php b/tests/Utils/Policies/UserPolicy.php index f1e24ecf9..cba98188b 100644 --- a/tests/Utils/Policies/UserPolicy.php +++ b/tests/Utils/Policies/UserPolicy.php @@ -47,28 +47,24 @@ public function dependingOnArg(User $viewer, bool $pass): bool return $pass; } - /** - * @param User|array ...$args - */ + /** @param User|array ...$args */ public function injectArgs(User $viewer, ...$args): bool { $injectedArgs = $args[0]; - if ($injectedArgs instanceof User){ + if ($injectedArgs instanceof User) { $injectedArgs = $args[1]; } return $injectedArgs === ['foo' => 'bar']; } - /** - * @param User|array ...$args - */ + /** @param User|array ...$args */ public function argsWithInjectedArgs(User $viewer, ...$args): bool { $injectedArgs = $args[0]; $staticArgs = $args[1]; - if ($injectedArgs instanceof User){ + if ($injectedArgs instanceof User) { $injectedArgs = $args[1]; $staticArgs = $args[2]; }