|
33 | 33 | use Hypervel\Tests\Auth\Stub\AccessGateTestResource; |
34 | 34 | use Hypervel\Tests\Auth\Stub\AccessGateTestStaticClass; |
35 | 35 | use Hypervel\Tests\Auth\Stub\AccessGateTestSubDummy; |
| 36 | +use Hypervel\Tests\Auth\Stub\DummyWithoutUsePolicy; |
| 37 | +use Hypervel\Tests\Auth\Stub\DummyWithUsePolicy; |
| 38 | +use Hypervel\Tests\Auth\Stub\DummyWithUsePolicyPolicy; |
| 39 | +use Hypervel\Tests\Auth\Stub\SubDummyWithUsePolicy; |
36 | 40 | use Hypervel\Tests\TestCase; |
37 | 41 | use InvalidArgumentException; |
38 | 42 | use PHPUnit\Framework\Attributes\DataProvider; |
@@ -1011,6 +1015,61 @@ public function testClassesCanBeDefinedAsCallbacksUsingAtNotationForGuests() |
1011 | 1015 | $this->assertFalse($gate->check('absent_invokable')); |
1012 | 1016 | } |
1013 | 1017 |
|
| 1018 | + public function testPolicyCanBeResolvedFromUsePolicyAttribute(): void |
| 1019 | + { |
| 1020 | + $gate = $this->getBasicGate(); |
| 1021 | + |
| 1022 | + $this->assertInstanceOf( |
| 1023 | + DummyWithUsePolicyPolicy::class, |
| 1024 | + $gate->getPolicyFor(DummyWithUsePolicy::class) |
| 1025 | + ); |
| 1026 | + } |
| 1027 | + |
| 1028 | + public function testPolicyFromUsePolicyAttributeWorksWithObjectInstance(): void |
| 1029 | + { |
| 1030 | + $gate = $this->getBasicGate(); |
| 1031 | + |
| 1032 | + $this->assertInstanceOf( |
| 1033 | + DummyWithUsePolicyPolicy::class, |
| 1034 | + $gate->getPolicyFor(new DummyWithUsePolicy()) |
| 1035 | + ); |
| 1036 | + } |
| 1037 | + |
| 1038 | + public function testExplicitPolicyTakesPrecedenceOverUsePolicyAttribute(): void |
| 1039 | + { |
| 1040 | + $gate = $this->getBasicGate(); |
| 1041 | + |
| 1042 | + // Register an explicit policy that should take precedence |
| 1043 | + $gate->policy(DummyWithUsePolicy::class, AccessGateTestPolicy::class); |
| 1044 | + |
| 1045 | + $this->assertInstanceOf( |
| 1046 | + AccessGateTestPolicy::class, |
| 1047 | + $gate->getPolicyFor(DummyWithUsePolicy::class) |
| 1048 | + ); |
| 1049 | + } |
| 1050 | + |
| 1051 | + public function testUsePolicyAttributeTakesPrecedenceOverSubclassFallback(): void |
| 1052 | + { |
| 1053 | + $gate = $this->getBasicGate(); |
| 1054 | + |
| 1055 | + // Register a policy for the parent class |
| 1056 | + $gate->policy(DummyWithUsePolicy::class, AccessGateTestPolicy::class); |
| 1057 | + |
| 1058 | + // SubDummyWithUsePolicy extends DummyWithUsePolicy but has its own #[UsePolicy] attribute |
| 1059 | + // The attribute should take precedence over the subclass fallback |
| 1060 | + $this->assertInstanceOf( |
| 1061 | + DummyWithUsePolicyPolicy::class, |
| 1062 | + $gate->getPolicyFor(SubDummyWithUsePolicy::class) |
| 1063 | + ); |
| 1064 | + } |
| 1065 | + |
| 1066 | + public function testGetPolicyForReturnsNullForClassWithoutUsePolicyAttribute(): void |
| 1067 | + { |
| 1068 | + $gate = $this->getBasicGate(); |
| 1069 | + |
| 1070 | + $this->assertNull($gate->getPolicyFor(DummyWithoutUsePolicy::class)); |
| 1071 | + } |
| 1072 | + |
1014 | 1073 | public function testCanSetDenialResponseInConstructor() |
1015 | 1074 | { |
1016 | 1075 | $gate = $this->getGuestGate(); |
|
0 commit comments