Skip to content

Commit

Permalink
Fixed polling because of direct attribute checks
Browse files Browse the repository at this point in the history
  • Loading branch information
rennokki committed Nov 13, 2022
1 parent dfb54c2 commit 81fec34
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 7 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"require-dev": {
"chelout/laravel-relationship-events": "^1.5",
"laravel/legacy-factories": "^1.3",
"livewire/livewire": "dev-master",
"mockery/mockery": "^1.5",
"orchestra/database": "^6.28|^7.0",
"orchestra/testbench": "^6.28|^7.0",
Expand Down
13 changes: 8 additions & 5 deletions src/Traits/QueryCacheable.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ trait QueryCacheable
*/
public static function bootQueryCacheable()
{
/** @var \Illuminate\Database\Eloquent\Model $this */
if (isset(static::$flushCacheOnUpdate) && static::$flushCacheOnUpdate) {
static::observe(
static::getFlushQueryCacheObserver()
Expand Down Expand Up @@ -69,6 +70,7 @@ protected function getCacheBaseTags(): array
*/
public function getCacheTagsToInvalidateOnUpdate($relation = null, $pivotedModels = null): array
{
/** @var \Illuminate\Database\Eloquent\Model $this */
return $this->getCacheBaseTags();
}

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

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

$builder->dontCache();

if ($this->cacheFor) {
if (property_exists($this, 'cacheFor')) {
$builder->cacheFor($this->cacheFor);
}

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

if ($this->cacheTags) {
if (property_exists($this, 'cacheTags')) {
$builder->cacheTags($this->cacheTags);
}

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

if ($this->cachePrefix) {
if (property_exists($this, 'cachePrefix')) {
$builder->cachePrefix($this->cachePrefix);
}

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

if ($this->cacheDriver) {
if (property_exists($this, 'cacheDriver')) {
$builder->cacheDriver($this->cacheDriver);
}

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

if ($this->cacheUsePlainKey) {
if (property_exists($this, 'cacheUsePlainKey')) {
$builder->withPlainKey();
}

Expand Down
41 changes: 41 additions & 0 deletions tests/LivewireTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Rennokki\QueryCache\Test;

use Illuminate\Support\Facades\Cache;
use Livewire\Component;
use Livewire\Livewire;
use Rennokki\QueryCache\Test\Models\Post;

class LivewireTest extends TestCase
{
/**
* @dataProvider strictModeContextProvider
*/
public function test_livewire_component_poll_doesnt_break_when_callback_is_already_set()
{
// See: https://github.com/renoki-co/laravel-eloquent-query-cache/issues/163
Livewire::component(PostComponent::class);

$posts = factory(Post::class, 30)->create();

/** @var \Livewire\Testing\TestableLivewire $component */
Livewire::test(PostComponent::class, ['post' => $posts->first()])
->assertOk()
->assertSee($posts[0]->name)
->pretendWereSendingAComponentUpdateRequest(
'callMethod',
['id' => 'grwk', 'method' => '$refresh', 'params' => []],
);
}
}

class PostComponent extends Component
{
public Post $post;

public static function getName()
{
return 'post';
}
}
13 changes: 11 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function setUp(): void
protected function getPackageProviders($app)
{
return [
//
\Livewire\LivewireServiceProvider::class,
];
}

Expand All @@ -52,15 +52,24 @@ public function getEnvironmentSetUp($app)
'database' => __DIR__.'/database/database.sqlite',
'prefix' => '',
]);

$app['config']->set(
'cache.driver', getenv('CACHE_DRIVER') ?: env('CACHE_DRIVER', 'array')
'cache.driver',
getenv('CACHE_DRIVER') ?: env('CACHE_DRIVER', 'array')
);

$app['config']->set('auth.providers.users.model', User::class);
$app['config']->set('auth.providers.posts.model', Post::class);
$app['config']->set('auth.providers.kids.model', Kid::class);
$app['config']->set('auth.providers.books.model', Book::class);
$app['config']->set('auth.providers.pages.model', Page::class);
$app['config']->set('app.key', 'wslxrEFGWY6GfGhvN9L3wH3KSRJQQpBD');

$app['config']->set('view.paths', [
__DIR__.'/views',
]);

$app['config']->set('livewire.view_path', __DIR__.'/views/livewire');
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/views/livewire/layout.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
@livewireStyles
</head>
<body>
<livewire:post :post="$post">
@livewireScripts
</body>
</html>
4 changes: 4 additions & 0 deletions tests/views/livewire/post.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div wire:poll>
<p>Title: {{ $post->name }}</p>
<p>Time: <b>{{ now() }}</b></p>
</div>

0 comments on commit 81fec34

Please sign in to comment.