Skip to content

Commit

Permalink
Merge pull request #165 from renoki-co/fix/strict-mode-in-models
Browse files Browse the repository at this point in the history
[3.x] Fix the strict mode in models
  • Loading branch information
rennokki committed Nov 13, 2022
2 parents 6e705d6 + 81fec34 commit b0814fe
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 9 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
}
],
"require": {
"illuminate/database": "^8.83|^9.0.1",
"illuminate/support": "^8.83|^9.0.1"
"illuminate/database": "^8.83|^9.35",
"illuminate/support": "^8.83|^9.35"
},
"autoload": {
"psr-4": {
Expand All @@ -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
6 changes: 6 additions & 0 deletions tests/CountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

class CountTest extends TestCase
{
/**
* @dataProvider strictModeContextProvider
*/
public function test_count()
{
$posts = factory(Post::class, 5)->create();
Expand All @@ -21,6 +24,9 @@ public function test_count()
);
}

/**
* @dataProvider strictModeContextProvider
*/
public function test_count_with_columns()
{
$posts = factory(Post::class, 5)->create();
Expand Down
6 changes: 6 additions & 0 deletions tests/FirstTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

class FirstTest extends TestCase
{
/**
* @dataProvider strictModeContextProvider
*/
public function test_first()
{
$post = factory(Post::class)->create();
Expand All @@ -21,6 +24,9 @@ public function test_first()
);
}

/**
* @dataProvider strictModeContextProvider
*/
public function test_first_with_columns()
{
$post = factory(Post::class)->create();
Expand Down
3 changes: 3 additions & 0 deletions tests/FlushCacheOnUpdatePivotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

class FlushCacheOnUpdatePivotTest extends TestCase
{
/**
* @dataProvider strictModeContextProvider
*/
public function test_belongs_to_many()
{
$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;}';
Expand Down
12 changes: 12 additions & 0 deletions tests/FlushCacheOnUpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

class FlushCacheOnUpdateTest extends TestCase
{
/**
* @dataProvider strictModeContextProvider
*/
public function test_flush_cache_on_create()
{
$page = factory(Page::class)->create();
Expand All @@ -28,6 +31,9 @@ public function test_flush_cache_on_create()
$this->assertNull($cache);
}

/**
* @dataProvider strictModeContextProvider
*/
public function test_flush_cache_on_update()
{
$page = factory(Page::class)->create();
Expand All @@ -50,6 +56,9 @@ public function test_flush_cache_on_update()
$this->assertNull($cache);
}

/**
* @dataProvider strictModeContextProvider
*/
public function test_flush_cache_on_delete()
{
$page = factory(Page::class)->create();
Expand All @@ -70,6 +79,9 @@ public function test_flush_cache_on_delete()
$this->assertNull($cache);
}

/**
* @dataProvider strictModeContextProvider
*/
public function test_flush_cache_on_force_deletion()
{
$page = factory(Page::class)->create();
Expand Down
9 changes: 9 additions & 0 deletions tests/GetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

class GetTest extends TestCase
{
/**
* @dataProvider strictModeContextProvider
*/
public function test_get()
{
$post = factory(Post::class)->create();
Expand All @@ -26,6 +29,9 @@ public function test_get()
);
}

/**
* @dataProvider strictModeContextProvider
*/
public function test_get_with_columns()
{
$post = factory(Post::class)->create();
Expand All @@ -45,6 +51,9 @@ public function test_get_with_columns()
);
}

/**
* @dataProvider strictModeContextProvider
*/
public function test_get_with_string_columns()
{
$post = factory(Post::class)->create();
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';
}
}
33 changes: 33 additions & 0 deletions tests/MethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

class MethodsTest extends TestCase
{
/**
* @dataProvider strictModeContextProvider
*/
public function test_do_not_cache()
{
$post = factory(Post::class)->create();
Expand All @@ -23,6 +26,9 @@ public function test_do_not_cache()
$this->assertNull($cache);
}

/**
* @dataProvider strictModeContextProvider
*/
public function test_cache_prefix()
{
$post = factory(Post::class)->create();
Expand All @@ -32,6 +38,9 @@ public function test_cache_prefix()
$this->assertNotNull($cache);
}

/**
* @dataProvider strictModeContextProvider
*/
public function test_cache_tags()
{
$post = factory(Post::class)->create();
Expand All @@ -49,6 +58,9 @@ public function test_cache_tags()
$this->assertNotNull($cache);
}

/**
* @dataProvider strictModeContextProvider
*/
public function test_cache_flush_with_the_right_tag()
{
$post = factory(Post::class)->create();
Expand All @@ -63,6 +75,9 @@ public function test_cache_flush_with_the_right_tag()
$this->assertNull($cache);
}

/**
* @dataProvider strictModeContextProvider
*/
public function test_cache_flush_without_the_right_tag()
{
$post = factory(Post::class)->create();
Expand All @@ -83,6 +98,9 @@ public function test_cache_flush_without_the_right_tag()
: $this->assertNull($cache);
}

/**
* @dataProvider strictModeContextProvider
*/
public function test_cache_flush_with_more_tags()
{
$post = factory(Post::class)->create();
Expand All @@ -101,6 +119,9 @@ public function test_cache_flush_with_more_tags()
$this->assertNull($cache);
}

/**
* @dataProvider strictModeContextProvider
*/
public function test_cache_flush_with_default_tags_attached()
{
$book = factory(Book::class)->create();
Expand All @@ -116,6 +137,9 @@ public function test_cache_flush_with_default_tags_attached()
$this->assertNull($cache);
}

/**
* @dataProvider strictModeContextProvider
*/
public function test_hashed_key()
{
$kid = factory(Kid::class)->create();
Expand All @@ -125,6 +149,9 @@ public function test_hashed_key()
$this->assertNotNull($cache);
}

/**
* @dataProvider strictModeContextProvider
*/
public function test_append_cache_tags()
{
$post = factory(Post::class)->create();
Expand All @@ -142,6 +169,9 @@ public function test_append_cache_tags()
$this->assertNotNull($cache);
}

/**
* @dataProvider strictModeContextProvider
*/
public function test_multiple_append_cache_tags()
{
$post = factory(Post::class)->create();
Expand All @@ -150,6 +180,9 @@ public function test_multiple_append_cache_tags()
$this->assertEquals($storedPostQuery->getQuery()->getCacheTags(), ['test', 'test2']);
}

/**
* @dataProvider strictModeContextProvider
*/
public function test_append_cache_tags_with_sub_query()
{
$user = factory(User::class)->create();
Expand Down
6 changes: 6 additions & 0 deletions tests/PaginateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

class PaginateTest extends TestCase
{
/**
* @dataProvider strictModeContextProvider
*/
public function test_paginate()
{
$posts = factory(Post::class, 30)->create();
Expand All @@ -28,6 +31,9 @@ public function test_paginate()
$this->assertEquals(1, $postsCache->first()->id);
}

/**
* @dataProvider strictModeContextProvider
*/
public function test_paginate_with_columns()
{
$posts = factory(Post::class, 30)->create();
Expand Down
6 changes: 6 additions & 0 deletions tests/SimplePaginateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

class SimplePaginateTest extends TestCase
{
/**
* @dataProvider strictModeContextProvider
*/
public function test_simple_paginate()
{
$posts = factory(Post::class, 30)->create();
Expand All @@ -26,6 +29,9 @@ public function test_simple_paginate()
);
}

/**
* @dataProvider strictModeContextProvider
*/
public function test_simple_paginate_with_columns()
{
$posts = factory(Post::class, 30)->create();
Expand Down
Loading

0 comments on commit b0814fe

Please sign in to comment.