Skip to content

Commit b0814fe

Browse files
authored
Merge pull request #165 from renoki-co/fix/strict-mode-in-models
[3.x] Fix the strict mode in models
2 parents 6e705d6 + 81fec34 commit b0814fe

14 files changed

+174
-9
lines changed

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
}
2121
],
2222
"require": {
23-
"illuminate/database": "^8.83|^9.0.1",
24-
"illuminate/support": "^8.83|^9.0.1"
23+
"illuminate/database": "^8.83|^9.35",
24+
"illuminate/support": "^8.83|^9.35"
2525
},
2626
"autoload": {
2727
"psr-4": {
@@ -39,6 +39,7 @@
3939
"require-dev": {
4040
"chelout/laravel-relationship-events": "^1.5",
4141
"laravel/legacy-factories": "^1.3",
42+
"livewire/livewire": "dev-master",
4243
"mockery/mockery": "^1.5",
4344
"orchestra/database": "^6.28|^7.0",
4445
"orchestra/testbench": "^6.28|^7.0",

src/Traits/QueryCacheable.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ trait QueryCacheable
2727
*/
2828
public static function bootQueryCacheable()
2929
{
30+
/** @var \Illuminate\Database\Eloquent\Model $this */
3031
if (isset(static::$flushCacheOnUpdate) && static::$flushCacheOnUpdate) {
3132
static::observe(
3233
static::getFlushQueryCacheObserver()
@@ -69,6 +70,7 @@ protected function getCacheBaseTags(): array
6970
*/
7071
public function getCacheTagsToInvalidateOnUpdate($relation = null, $pivotedModels = null): array
7172
{
73+
/** @var \Illuminate\Database\Eloquent\Model $this */
7274
return $this->getCacheBaseTags();
7375
}
7476

@@ -77,6 +79,7 @@ public function getCacheTagsToInvalidateOnUpdate($relation = null, $pivotedModel
7779
*/
7880
protected function newBaseQueryBuilder()
7981
{
82+
/** @var \Illuminate\Database\Eloquent\Model $this */
8083
$connection = $this->getConnection();
8184

8285
$builder = new Builder(
@@ -87,39 +90,39 @@ protected function newBaseQueryBuilder()
8790

8891
$builder->dontCache();
8992

90-
if ($this->cacheFor) {
93+
if (property_exists($this, 'cacheFor')) {
9194
$builder->cacheFor($this->cacheFor);
9295
}
9396

9497
if (method_exists($this, 'cacheForValue')) {
9598
$builder->cacheFor($this->cacheForValue($builder));
9699
}
97100

98-
if ($this->cacheTags) {
101+
if (property_exists($this, 'cacheTags')) {
99102
$builder->cacheTags($this->cacheTags);
100103
}
101104

102105
if (method_exists($this, 'cacheTagsValue')) {
103106
$builder->cacheTags($this->cacheTagsValue($builder));
104107
}
105108

106-
if ($this->cachePrefix) {
109+
if (property_exists($this, 'cachePrefix')) {
107110
$builder->cachePrefix($this->cachePrefix);
108111
}
109112

110113
if (method_exists($this, 'cachePrefixValue')) {
111114
$builder->cachePrefix($this->cachePrefixValue($builder));
112115
}
113116

114-
if ($this->cacheDriver) {
117+
if (property_exists($this, 'cacheDriver')) {
115118
$builder->cacheDriver($this->cacheDriver);
116119
}
117120

118121
if (method_exists($this, 'cacheDriverValue')) {
119122
$builder->cacheDriver($this->cacheDriverValue($builder));
120123
}
121124

122-
if ($this->cacheUsePlainKey) {
125+
if (property_exists($this, 'cacheUsePlainKey')) {
123126
$builder->withPlainKey();
124127
}
125128

tests/CountTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
class CountTest extends TestCase
99
{
10+
/**
11+
* @dataProvider strictModeContextProvider
12+
*/
1013
public function test_count()
1114
{
1215
$posts = factory(Post::class, 5)->create();
@@ -21,6 +24,9 @@ public function test_count()
2124
);
2225
}
2326

27+
/**
28+
* @dataProvider strictModeContextProvider
29+
*/
2430
public function test_count_with_columns()
2531
{
2632
$posts = factory(Post::class, 5)->create();

tests/FirstTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
class FirstTest extends TestCase
99
{
10+
/**
11+
* @dataProvider strictModeContextProvider
12+
*/
1013
public function test_first()
1114
{
1215
$post = factory(Post::class)->create();
@@ -21,6 +24,9 @@ public function test_first()
2124
);
2225
}
2326

27+
/**
28+
* @dataProvider strictModeContextProvider
29+
*/
2430
public function test_first_with_columns()
2531
{
2632
$post = factory(Post::class)->create();

tests/FlushCacheOnUpdatePivotTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
class FlushCacheOnUpdatePivotTest extends TestCase
99
{
10+
/**
11+
* @dataProvider strictModeContextProvider
12+
*/
1013
public function test_belongs_to_many()
1114
{
1215
$key = 'leqc:sqlitegetselect "roles".*, "role_user"."user_id" as "pivot_user_id", "role_user"."role_id" as "pivot_role_id" from "roles" inner join "role_user" on "roles"."id" = "role_user"."role_id" where "role_user"."user_id" = ? limit 1a:1:{i:0;i:1;}';

tests/FlushCacheOnUpdateTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
class FlushCacheOnUpdateTest extends TestCase
88
{
9+
/**
10+
* @dataProvider strictModeContextProvider
11+
*/
912
public function test_flush_cache_on_create()
1013
{
1114
$page = factory(Page::class)->create();
@@ -28,6 +31,9 @@ public function test_flush_cache_on_create()
2831
$this->assertNull($cache);
2932
}
3033

34+
/**
35+
* @dataProvider strictModeContextProvider
36+
*/
3137
public function test_flush_cache_on_update()
3238
{
3339
$page = factory(Page::class)->create();
@@ -50,6 +56,9 @@ public function test_flush_cache_on_update()
5056
$this->assertNull($cache);
5157
}
5258

59+
/**
60+
* @dataProvider strictModeContextProvider
61+
*/
5362
public function test_flush_cache_on_delete()
5463
{
5564
$page = factory(Page::class)->create();
@@ -70,6 +79,9 @@ public function test_flush_cache_on_delete()
7079
$this->assertNull($cache);
7180
}
7281

82+
/**
83+
* @dataProvider strictModeContextProvider
84+
*/
7385
public function test_flush_cache_on_force_deletion()
7486
{
7587
$page = factory(Page::class)->create();

tests/GetTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
class GetTest extends TestCase
99
{
10+
/**
11+
* @dataProvider strictModeContextProvider
12+
*/
1013
public function test_get()
1114
{
1215
$post = factory(Post::class)->create();
@@ -26,6 +29,9 @@ public function test_get()
2629
);
2730
}
2831

32+
/**
33+
* @dataProvider strictModeContextProvider
34+
*/
2935
public function test_get_with_columns()
3036
{
3137
$post = factory(Post::class)->create();
@@ -45,6 +51,9 @@ public function test_get_with_columns()
4551
);
4652
}
4753

54+
/**
55+
* @dataProvider strictModeContextProvider
56+
*/
4857
public function test_get_with_string_columns()
4958
{
5059
$post = factory(Post::class)->create();

tests/LivewireTest.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Rennokki\QueryCache\Test;
4+
5+
use Illuminate\Support\Facades\Cache;
6+
use Livewire\Component;
7+
use Livewire\Livewire;
8+
use Rennokki\QueryCache\Test\Models\Post;
9+
10+
class LivewireTest extends TestCase
11+
{
12+
/**
13+
* @dataProvider strictModeContextProvider
14+
*/
15+
public function test_livewire_component_poll_doesnt_break_when_callback_is_already_set()
16+
{
17+
// See: https://github.com/renoki-co/laravel-eloquent-query-cache/issues/163
18+
Livewire::component(PostComponent::class);
19+
20+
$posts = factory(Post::class, 30)->create();
21+
22+
/** @var \Livewire\Testing\TestableLivewire $component */
23+
Livewire::test(PostComponent::class, ['post' => $posts->first()])
24+
->assertOk()
25+
->assertSee($posts[0]->name)
26+
->pretendWereSendingAComponentUpdateRequest(
27+
'callMethod',
28+
['id' => 'grwk', 'method' => '$refresh', 'params' => []],
29+
);
30+
}
31+
}
32+
33+
class PostComponent extends Component
34+
{
35+
public Post $post;
36+
37+
public static function getName()
38+
{
39+
return 'post';
40+
}
41+
}

tests/MethodsTest.php

+33
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
class MethodsTest extends TestCase
1212
{
13+
/**
14+
* @dataProvider strictModeContextProvider
15+
*/
1316
public function test_do_not_cache()
1417
{
1518
$post = factory(Post::class)->create();
@@ -23,6 +26,9 @@ public function test_do_not_cache()
2326
$this->assertNull($cache);
2427
}
2528

29+
/**
30+
* @dataProvider strictModeContextProvider
31+
*/
2632
public function test_cache_prefix()
2733
{
2834
$post = factory(Post::class)->create();
@@ -32,6 +38,9 @@ public function test_cache_prefix()
3238
$this->assertNotNull($cache);
3339
}
3440

41+
/**
42+
* @dataProvider strictModeContextProvider
43+
*/
3544
public function test_cache_tags()
3645
{
3746
$post = factory(Post::class)->create();
@@ -49,6 +58,9 @@ public function test_cache_tags()
4958
$this->assertNotNull($cache);
5059
}
5160

61+
/**
62+
* @dataProvider strictModeContextProvider
63+
*/
5264
public function test_cache_flush_with_the_right_tag()
5365
{
5466
$post = factory(Post::class)->create();
@@ -63,6 +75,9 @@ public function test_cache_flush_with_the_right_tag()
6375
$this->assertNull($cache);
6476
}
6577

78+
/**
79+
* @dataProvider strictModeContextProvider
80+
*/
6681
public function test_cache_flush_without_the_right_tag()
6782
{
6883
$post = factory(Post::class)->create();
@@ -83,6 +98,9 @@ public function test_cache_flush_without_the_right_tag()
8398
: $this->assertNull($cache);
8499
}
85100

101+
/**
102+
* @dataProvider strictModeContextProvider
103+
*/
86104
public function test_cache_flush_with_more_tags()
87105
{
88106
$post = factory(Post::class)->create();
@@ -101,6 +119,9 @@ public function test_cache_flush_with_more_tags()
101119
$this->assertNull($cache);
102120
}
103121

122+
/**
123+
* @dataProvider strictModeContextProvider
124+
*/
104125
public function test_cache_flush_with_default_tags_attached()
105126
{
106127
$book = factory(Book::class)->create();
@@ -116,6 +137,9 @@ public function test_cache_flush_with_default_tags_attached()
116137
$this->assertNull($cache);
117138
}
118139

140+
/**
141+
* @dataProvider strictModeContextProvider
142+
*/
119143
public function test_hashed_key()
120144
{
121145
$kid = factory(Kid::class)->create();
@@ -125,6 +149,9 @@ public function test_hashed_key()
125149
$this->assertNotNull($cache);
126150
}
127151

152+
/**
153+
* @dataProvider strictModeContextProvider
154+
*/
128155
public function test_append_cache_tags()
129156
{
130157
$post = factory(Post::class)->create();
@@ -142,6 +169,9 @@ public function test_append_cache_tags()
142169
$this->assertNotNull($cache);
143170
}
144171

172+
/**
173+
* @dataProvider strictModeContextProvider
174+
*/
145175
public function test_multiple_append_cache_tags()
146176
{
147177
$post = factory(Post::class)->create();
@@ -150,6 +180,9 @@ public function test_multiple_append_cache_tags()
150180
$this->assertEquals($storedPostQuery->getQuery()->getCacheTags(), ['test', 'test2']);
151181
}
152182

183+
/**
184+
* @dataProvider strictModeContextProvider
185+
*/
153186
public function test_append_cache_tags_with_sub_query()
154187
{
155188
$user = factory(User::class)->create();

tests/PaginateTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
class PaginateTest extends TestCase
99
{
10+
/**
11+
* @dataProvider strictModeContextProvider
12+
*/
1013
public function test_paginate()
1114
{
1215
$posts = factory(Post::class, 30)->create();
@@ -28,6 +31,9 @@ public function test_paginate()
2831
$this->assertEquals(1, $postsCache->first()->id);
2932
}
3033

34+
/**
35+
* @dataProvider strictModeContextProvider
36+
*/
3137
public function test_paginate_with_columns()
3238
{
3339
$posts = factory(Post::class, 30)->create();

tests/SimplePaginateTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
class SimplePaginateTest extends TestCase
99
{
10+
/**
11+
* @dataProvider strictModeContextProvider
12+
*/
1013
public function test_simple_paginate()
1114
{
1215
$posts = factory(Post::class, 30)->create();
@@ -26,6 +29,9 @@ public function test_simple_paginate()
2629
);
2730
}
2831

32+
/**
33+
* @dataProvider strictModeContextProvider
34+
*/
2935
public function test_simple_paginate_with_columns()
3036
{
3137
$posts = factory(Post::class, 30)->create();

0 commit comments

Comments
 (0)