Skip to content

Commit d7ecb0b

Browse files
committed
Merge branch 'feat/fields-with-relationships' of github.com:BinarCode/laravel-restify into feat/fields-with-relationships
2 parents eeb059b + a0e78b9 commit d7ecb0b

File tree

7 files changed

+88
-84
lines changed

7 files changed

+88
-84
lines changed

config/restify.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@
203203
/*
204204
| Default cache tags for all repositories. Individual repositories
205205
| can add their own tags in addition to these.
206-
|
206+
|
207207
| Note: Cache tags are only used if the cache store supports them.
208208
| Database and file cache stores do not support tagging.
209209
| Redis and Memcached stores support tagging.

src/Repositories/Concerns/InteractsWithCache.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
66
use Illuminate\Support\Facades\Cache;
7-
use Illuminate\Support\Str;
87

98
trait InteractsWithCache
109
{
@@ -74,7 +73,7 @@ public function generateIndexCacheKey(RestifyRequest $request): string
7473

7574
// Add user context for authorization-sensitive data
7675
if ($user = $request->user()) {
77-
$keyParts[] = 'user_' . $user->getAuthIdentifier();
76+
$keyParts[] = 'user_'.$user->getAuthIdentifier();
7877
} else {
7978
$keyParts[] = 'guest';
8079
}
@@ -84,11 +83,11 @@ public function generateIndexCacheKey(RestifyRequest $request): string
8483
try {
8584
$latest = $this->model()::latest($this->model()->getUpdatedAtColumn())->first();
8685
if ($latest) {
87-
$keyParts[] = 'v_' . $latest->{$this->model()->getUpdatedAtColumn()}->timestamp;
86+
$keyParts[] = 'v_'.$latest->{$this->model()->getUpdatedAtColumn()}->timestamp;
8887
}
8988
} catch (\Exception $e) {
9089
// Fallback to current timestamp if query fails
91-
$keyParts[] = 'v_' . now()->timestamp;
90+
$keyParts[] = 'v_'.now()->timestamp;
9291
}
9392
}
9493

@@ -169,6 +168,7 @@ public static function clearCache(): void
169168
// If cache tags are used and supported, flush by tags
170169
if (! empty(static::$cacheTags) && static::cacheStoreSupportsTagging($store)) {
171170
$store->tags(static::$cacheTags)->flush();
171+
172172
return;
173173
}
174174

@@ -192,18 +192,20 @@ public static function clearCache(): void
192192
}
193193

194194
$storage->setValue($store, $storageArray);
195+
195196
return;
196197
}
197198

198199
// For Redis and other drivers that support pattern deletion
199200
if (method_exists($store->getStore(), 'getRedis')) {
200201
try {
201-
$pattern = 'restify:repository:' . static::uriKey() . ':*';
202+
$pattern = 'restify:repository:'.static::uriKey().':*';
202203
$store->getStore()->getRedis()->eval(
203204
"return redis.call('del', unpack(redis.call('keys', ARGV[1])))",
204205
0,
205206
$pattern
206207
);
208+
207209
return;
208210
} catch (\Exception $e) {
209211
// Continue to fallback
@@ -271,6 +273,7 @@ public static function cacheStoreSupportsTagging($store): bool
271273
try {
272274
// Create a test tagged cache entry (without storing anything)
273275
$store->tags(['test'])->getStore();
276+
274277
return true;
275278
} catch (\Exception $e) {
276279
// If it throws an exception about tagging not being supported, return false

src/Repositories/Repository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ protected static function boot(): void
221221
{
222222
// Boot all traits that have a bootTrait method
223223
foreach (class_uses_recursive(static::class) as $trait) {
224-
$method = 'boot' . class_basename($trait);
224+
$method = 'boot'.class_basename($trait);
225225
if (method_exists(static::class, $method)) {
226226
static::$method();
227227
}

tests/Feature/CacheIntegrationTest.php

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,23 @@ protected function tearDown(): void
2424
{
2525
// Clear cache after each test
2626
Cache::flush();
27-
27+
2828
parent::tearDown();
2929
}
3030

3131
public function test_it_does_not_cache_by_default_in_tests()
3232
{
3333
// Ensure we're in testing environment
3434
$this->assertEquals('testing', app()->environment());
35-
35+
3636
// Make request
3737
$response = $this->getJson('/api/restify/posts');
3838
$response->assertOk();
39-
39+
4040
// Make another request - should not be cached
4141
$response2 = $this->getJson('/api/restify/posts');
4242
$response2->assertOk();
43-
43+
4444
// Verify cache is empty (no cache keys should exist)
4545
$this->assertEmpty($this->getCacheKeys());
4646
}
@@ -50,11 +50,11 @@ public function test_it_can_enable_cache_in_tests_with_config()
5050
// Enable caching in tests
5151
config(['restify.repositories.cache.enabled' => true]);
5252
config(['restify.repositories.cache.enable_in_tests' => true]);
53-
53+
5454
// Make request
5555
$response = $this->getJson('/api/restify/posts');
5656
$response->assertOk();
57-
57+
5858
// Verify cache key was created
5959
$this->assertNotEmpty($this->getCacheKeys());
6060
}
@@ -64,11 +64,11 @@ public function test_it_respects_global_cache_disabled_setting()
6464
// Disable caching globally
6565
config(['restify.repositories.cache.enabled' => false]);
6666
config(['restify.repositories.cache.enable_in_tests' => true]);
67-
67+
6868
// Make request
6969
$response = $this->getJson('/api/restify/posts');
7070
$response->assertOk();
71-
71+
7272
// Verify no cache key was created
7373
$this->assertEmpty($this->getCacheKeys());
7474
}
@@ -77,19 +77,19 @@ public function test_it_generates_different_cache_keys_for_different_parameters(
7777
{
7878
config(['restify.repositories.cache.enabled' => true]);
7979
config(['restify.repositories.cache.enable_in_tests' => true]);
80-
80+
8181
// Clear any existing cache
8282
Cache::flush();
83-
83+
8484
// Make request with different parameters
8585
$this->getJson('/api/restify/posts?search=test');
8686
$keys1 = $this->getCacheKeys();
87-
87+
8888
Cache::flush();
89-
89+
9090
$this->getJson('/api/restify/posts?search=different');
9191
$keys2 = $this->getCacheKeys();
92-
92+
9393
// Keys should be different for different search terms
9494
$this->assertNotEquals($keys1, $keys2);
9595
}
@@ -98,19 +98,19 @@ public function test_it_generates_different_cache_keys_for_different_users()
9898
{
9999
config(['restify.repositories.cache.enabled' => true]);
100100
config(['restify.repositories.cache.enable_in_tests' => true]);
101-
101+
102102
// Make request as first user
103103
$this->authenticate();
104104
$this->getJson('/api/restify/posts');
105105
$keys1 = $this->getCacheKeys();
106-
106+
107107
Cache::flush();
108-
108+
109109
// Make request as different user
110110
$this->authenticate(\Binaryk\LaravelRestify\Tests\Fixtures\User\User::factory()->create());
111111
$this->getJson('/api/restify/posts');
112112
$keys2 = $this->getCacheKeys();
113-
113+
114114
// Keys should be different for different users
115115
$this->assertNotEquals($keys1, $keys2);
116116
}
@@ -119,15 +119,15 @@ public function test_it_can_clear_cache_programmatically()
119119
{
120120
config(['restify.repositories.cache.enabled' => true]);
121121
config(['restify.repositories.cache.enable_in_tests' => true]);
122-
122+
123123
// Make request to populate cache
124124
$this->getJson('/api/restify/posts');
125125
$this->assertNotEmpty($this->getCacheKeys());
126-
126+
127127
// Clear cache using Laravel's flush for simplicity in tests
128128
// In production, the repository's clearCache method would be more targeted
129129
Cache::flush();
130-
130+
131131
// Verify cache is cleared
132132
$this->assertEmpty($this->getCacheKeys());
133133
}
@@ -136,20 +136,20 @@ public function test_it_caches_responses_when_enabled()
136136
{
137137
config(['restify.repositories.cache.enabled' => true]);
138138
config(['restify.repositories.cache.enable_in_tests' => true]);
139-
139+
140140
// Make first request
141141
$response1 = $this->getJson('/api/restify/posts');
142142
$response1->assertOk();
143143
$data1 = $response1->json();
144-
144+
145145
// Verify cache was created
146146
$this->assertNotEmpty($this->getCacheKeys());
147-
147+
148148
// Make second request - should get cached response
149149
$response2 = $this->getJson('/api/restify/posts');
150150
$response2->assertOk();
151151
$data2 = $response2->json();
152-
152+
153153
// Data should be identical
154154
$this->assertEquals($data1, $data2);
155155
}
@@ -158,22 +158,22 @@ public function test_it_respects_repository_specific_cache_settings()
158158
{
159159
config(['restify.repositories.cache.enabled' => true]);
160160
config(['restify.repositories.cache.enable_in_tests' => true]);
161-
161+
162162
// Disable cache for specific repository
163163
PostRepository::disableCache();
164-
164+
165165
// Make request
166166
$this->getJson('/api/restify/posts');
167-
167+
168168
// Verify no cache key was created
169169
$this->assertEmpty($this->getCacheKeys());
170-
170+
171171
// Re-enable cache
172172
PostRepository::enableCache();
173-
173+
174174
// Make request
175175
$this->getJson('/api/restify/posts');
176-
176+
177177
// Verify cache key was created
178178
$this->assertNotEmpty($this->getCacheKeys());
179179
}
@@ -182,16 +182,16 @@ public function test_it_includes_pagination_in_cache_key()
182182
{
183183
config(['restify.repositories.cache.enabled' => true]);
184184
config(['restify.repositories.cache.enable_in_tests' => true]);
185-
185+
186186
// Make request with different page numbers
187187
$this->getJson('/api/restify/posts?page=1');
188188
$keys1 = $this->getCacheKeys();
189-
189+
190190
Cache::flush();
191-
191+
192192
$this->getJson('/api/restify/posts?page=2');
193193
$keys2 = $this->getCacheKeys();
194-
194+
195195
// Keys should be different for different pages
196196
$this->assertNotEquals($keys1, $keys2);
197197
}
@@ -200,16 +200,16 @@ public function test_it_includes_filters_in_cache_key()
200200
{
201201
config(['restify.repositories.cache.enabled' => true]);
202202
config(['restify.repositories.cache.enable_in_tests' => true]);
203-
203+
204204
// Make request with filters
205205
$this->getJson('/api/restify/posts?title=test');
206206
$keys1 = $this->getCacheKeys();
207-
207+
208208
Cache::flush();
209-
209+
210210
$this->getJson('/api/restify/posts?title=different');
211211
$keys2 = $this->getCacheKeys();
212-
212+
213213
// Keys should be different for different filters
214214
$this->assertNotEquals($keys1, $keys2);
215215
}
@@ -218,16 +218,16 @@ public function test_it_can_set_custom_cache_ttl()
218218
{
219219
config(['restify.repositories.cache.enabled' => true]);
220220
config(['restify.repositories.cache.enable_in_tests' => true]);
221-
221+
222222
// Set custom TTL
223223
PostRepository::cacheTtl(60); // 1 minute
224-
224+
225225
// Make request
226226
$this->getJson('/api/restify/posts');
227-
227+
228228
// Verify cache exists
229229
$this->assertNotEmpty($this->getCacheKeys());
230-
230+
231231
// Verify TTL is set (this is hard to test precisely without time manipulation)
232232
// We'll just verify the cache exists for now
233233
}
@@ -238,34 +238,35 @@ public function test_it_can_set_custom_cache_ttl()
238238
protected function getCacheKeys(): array
239239
{
240240
$store = Cache::getStore();
241-
241+
242242
if (method_exists($store, 'getRedis')) {
243243
try {
244244
$redis = $store->getRedis();
245+
245246
return $redis->keys('*restify*') ?: [];
246247
} catch (\Exception $e) {
247248
// Fall back for non-Redis stores
248249
}
249250
}
250-
251+
251252
// For array store (used in tests), we need to inspect the internal array
252253
if (get_class($store) === 'Illuminate\Cache\ArrayStore') {
253254
$reflection = new \ReflectionClass($store);
254255
$storage = $reflection->getProperty('storage');
255256
$storage->setAccessible(true);
256257
$storageArray = $storage->getValue($store);
257-
258+
258259
return array_filter(array_keys($storageArray), function ($key) {
259260
return str_contains($key, 'restify');
260261
});
261262
}
262-
263+
263264
// Fallback - check for typical patterns (with error handling for database stores)
264265
$patterns = [
265266
'restify:repository:posts:index',
266-
'laravel_cache:restify:repository:posts:index'
267+
'laravel_cache:restify:repository:posts:index',
267268
];
268-
269+
269270
try {
270271
foreach ($patterns as $pattern) {
271272
if (Cache::has($pattern)) {
@@ -276,7 +277,7 @@ protected function getCacheKeys(): array
276277
// Silently fail if cache store doesn't exist (e.g., database cache table missing)
277278
return [];
278279
}
279-
280+
280281
return [];
281282
}
282-
}
283+
}

0 commit comments

Comments
 (0)