GatePolicyResolutionTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace Illuminate\Tests\Integration\Auth;
  3. use Illuminate\Auth\Access\Events\GateEvaluated;
  4. use Illuminate\Support\Facades\Event;
  5. use Illuminate\Support\Facades\Gate;
  6. use Illuminate\Tests\Integration\Auth\Fixtures\AuthenticationTestUser;
  7. use Illuminate\Tests\Integration\Auth\Fixtures\Policies\AuthenticationTestUserPolicy;
  8. use Orchestra\Testbench\TestCase;
  9. class GatePolicyResolutionTest extends TestCase
  10. {
  11. public function testGateEvaluationEventIsFired()
  12. {
  13. Event::fake();
  14. Gate::check('foo');
  15. Event::assertDispatched(GateEvaluated::class);
  16. }
  17. public function testPolicyCanBeGuessedUsingClassConventions()
  18. {
  19. $this->assertInstanceOf(
  20. AuthenticationTestUserPolicy::class,
  21. Gate::getPolicyFor(AuthenticationTestUser::class)
  22. );
  23. $this->assertInstanceOf(
  24. AuthenticationTestUserPolicy::class,
  25. Gate::getPolicyFor(Fixtures\Models\AuthenticationTestUser::class)
  26. );
  27. $this->assertNull(
  28. Gate::getPolicyFor(static::class)
  29. );
  30. }
  31. public function testPolicyCanBeGuessedUsingCallback()
  32. {
  33. Gate::guessPolicyNamesUsing(function () {
  34. return AuthenticationTestUserPolicy::class;
  35. });
  36. $this->assertInstanceOf(
  37. AuthenticationTestUserPolicy::class,
  38. Gate::getPolicyFor(AuthenticationTestUser::class)
  39. );
  40. }
  41. public function testPolicyCanBeGuessedMultipleTimes()
  42. {
  43. Gate::guessPolicyNamesUsing(function () {
  44. return [
  45. 'App\\Policies\\TestUserPolicy',
  46. AuthenticationTestUserPolicy::class,
  47. ];
  48. });
  49. $this->assertInstanceOf(
  50. AuthenticationTestUserPolicy::class,
  51. Gate::getPolicyFor(AuthenticationTestUser::class)
  52. );
  53. }
  54. }