Skip to content

Commit

Permalink
test: switch to gate facade
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-perri committed Nov 5, 2024
1 parent 5a844f7 commit 3866307
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions tests/Unit/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
use Closure;
use DateTime;
use Illuminate\Auth\AuthManager;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Contracts\Validation\InvokableRule;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Auth\User;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use Illuminate\Validation\Rules\Dimensions;
Expand Down Expand Up @@ -478,7 +478,11 @@ public static function ruleDataProvider(): array
'data' => 'value-a',
'rules' => function () {
$mockUser = new User;
$this->mockGateAllows(true, 'modify', [User::class, $mockUser, 'value-a']);

Gate::expects('allows')
->once()
->with('modify', [User::class, $mockUser, 'value-a'])
->andReturn(true);

return RuleSet::create()->can('modify', User::class, $mockUser);
},
Expand All @@ -488,7 +492,11 @@ public static function ruleDataProvider(): array
'data' => 'value-b',
'rules' => function () {
$mockUser = new User;
$this->mockGateAllows(false, 'modify', [User::class, $mockUser, 'value-b']);

Gate::expects('allows')
->once()
->with('modify', [User::class, $mockUser, 'value-b'])
->andReturn(false);

return RuleSet::create()->can('modify', User::class, $mockUser);
},
Expand Down Expand Up @@ -3114,18 +3122,4 @@ public function getClientOriginalExtension(): string
}
};
}

private function mockGateAllows(bool $return, ...$arguments): void
{
$gate = $this->mock(Gate::class);

$gate
->expects('allows')
->once()
->with(...$arguments)
->andReturn($return)
->getMock();

$this->app->instance(Gate::class, $gate);
}
}

0 comments on commit 3866307

Please sign in to comment.