|
2 | 2 |
|
3 | 3 | namespace Binaryk\LaravelRestify\Tests\Feature; |
4 | 4 |
|
| 5 | +use Binaryk\LaravelRestify\Cache\Cacheable; |
| 6 | +use Binaryk\LaravelRestify\Cache\PolicyCache; |
5 | 7 | use Binaryk\LaravelRestify\Tests\Database\Factories\PostFactory; |
6 | 8 | use Binaryk\LaravelRestify\Tests\Fixtures\Post\Post; |
7 | 9 | use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostPolicy; |
8 | 10 | use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostRepository; |
9 | 11 | use Binaryk\LaravelRestify\Tests\Fixtures\User\User; |
10 | 12 | use Binaryk\LaravelRestify\Tests\IntegrationTestCase; |
| 13 | +use Carbon\Carbon; |
| 14 | +use Carbon\CarbonInterface; |
11 | 15 | use Illuminate\Foundation\Testing\RefreshDatabase; |
12 | 16 | use Illuminate\Support\Facades\Cache; |
13 | 17 | use Illuminate\Support\Facades\Gate; |
| 18 | +use Symfony\Component\ErrorHandler\ErrorHandler; |
14 | 19 |
|
15 | 20 | class AuthorizableModelsTest extends IntegrationTestCase |
16 | 21 | { |
@@ -121,4 +126,46 @@ public function test_can_disable_policies_cache(): void |
121 | 126 | $this->getJson(PostRepository::route()) |
122 | 127 | ->assertForbidden(); |
123 | 128 | } |
| 129 | + |
| 130 | + public function test_can_enable_individual_policy_cache(): void |
| 131 | + { |
| 132 | + $this->freezeTime(); |
| 133 | + |
| 134 | + $this->partialMock(PostPolicy::class) |
| 135 | + ->shouldReceive('show') |
| 136 | + ->andReturn(true); |
| 137 | + |
| 138 | + $this->partialMock(PostPolicy::class) |
| 139 | + ->shouldReceive('cache') |
| 140 | + ->once() |
| 141 | + ->andReturn(now()->addMinutes(3)); |
| 142 | + |
| 143 | + Gate::policy(Post::class, PostPolicy::class); |
| 144 | + |
| 145 | + /** @var Carbon $timeCached */ |
| 146 | + $timeCached = Gate::getPolicyFor(Post::class) |
| 147 | + ->cache(); |
| 148 | + |
| 149 | + $this->assertInstanceOf(CarbonInterface::class, $timeCached); |
| 150 | + |
| 151 | + $post = PostFactory::one(); |
| 152 | + |
| 153 | + $this->getJson(PostRepository::route()) |
| 154 | + ->assertOk(); |
| 155 | + |
| 156 | + $this->assertTrue(Cache::has(PolicyCache::keyForPolicyMethods('posts', 'show', $post->getKey()))); |
| 157 | + |
| 158 | + $this->partialMock(PostPolicy::class) |
| 159 | + ->shouldReceive('show') |
| 160 | + ->andReturn(false); |
| 161 | + |
| 162 | + $this->getJson(PostRepository::route()) |
| 163 | + ->assertOk(); |
| 164 | + |
| 165 | + $this->assertTrue(Cache::get(PolicyCache::keyForPolicyMethods('posts', 'show', $post->getKey()))); |
| 166 | + |
| 167 | + $this->travelTo(now()->addMinutes(5)); |
| 168 | + |
| 169 | + $this->assertFalse(Cache::has(PolicyCache::keyForPolicyMethods('posts', 'show', $post->getKey()))); |
| 170 | + } |
124 | 171 | } |
0 commit comments