Skip to content

Commit 81fec34

Browse files
committed
Fixed polling because of direct attribute checks
1 parent dfb54c2 commit 81fec34

File tree

6 files changed

+79
-7
lines changed

6 files changed

+79
-7
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -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/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/TestCase.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function setUp(): void
3737
protected function getPackageProviders($app)
3838
{
3939
return [
40-
//
40+
\Livewire\LivewireServiceProvider::class,
4141
];
4242
}
4343

@@ -52,15 +52,24 @@ public function getEnvironmentSetUp($app)
5252
'database' => __DIR__.'/database/database.sqlite',
5353
'prefix' => '',
5454
]);
55+
5556
$app['config']->set(
56-
'cache.driver', getenv('CACHE_DRIVER') ?: env('CACHE_DRIVER', 'array')
57+
'cache.driver',
58+
getenv('CACHE_DRIVER') ?: env('CACHE_DRIVER', 'array')
5759
);
60+
5861
$app['config']->set('auth.providers.users.model', User::class);
5962
$app['config']->set('auth.providers.posts.model', Post::class);
6063
$app['config']->set('auth.providers.kids.model', Kid::class);
6164
$app['config']->set('auth.providers.books.model', Book::class);
6265
$app['config']->set('auth.providers.pages.model', Page::class);
6366
$app['config']->set('app.key', 'wslxrEFGWY6GfGhvN9L3wH3KSRJQQpBD');
67+
68+
$app['config']->set('view.paths', [
69+
__DIR__.'/views',
70+
]);
71+
72+
$app['config']->set('livewire.view_path', __DIR__.'/views/livewire');
6473
}
6574

6675
/**

tests/views/livewire/layout.blade.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Document</title>
8+
@livewireStyles
9+
</head>
10+
<body>
11+
<livewire:post :post="$post">
12+
@livewireScripts
13+
</body>
14+
</html>

tests/views/livewire/post.blade.php

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div wire:poll>
2+
<p>Title: {{ $post->name }}</p>
3+
<p>Time: <b>{{ now() }}</b></p>
4+
</div>

0 commit comments

Comments
 (0)