diff --git a/composer.json b/composer.json index 1209e0b..1803029 100644 --- a/composer.json +++ b/composer.json @@ -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": { @@ -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", diff --git a/src/Traits/QueryCacheable.php b/src/Traits/QueryCacheable.php index d7be9c1..c9f85a8 100644 --- a/src/Traits/QueryCacheable.php +++ b/src/Traits/QueryCacheable.php @@ -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() @@ -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(); } @@ -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( @@ -87,7 +90,7 @@ protected function newBaseQueryBuilder() $builder->dontCache(); - if ($this->cacheFor) { + if (property_exists($this, 'cacheFor')) { $builder->cacheFor($this->cacheFor); } @@ -95,7 +98,7 @@ protected function newBaseQueryBuilder() $builder->cacheFor($this->cacheForValue($builder)); } - if ($this->cacheTags) { + if (property_exists($this, 'cacheTags')) { $builder->cacheTags($this->cacheTags); } @@ -103,7 +106,7 @@ protected function newBaseQueryBuilder() $builder->cacheTags($this->cacheTagsValue($builder)); } - if ($this->cachePrefix) { + if (property_exists($this, 'cachePrefix')) { $builder->cachePrefix($this->cachePrefix); } @@ -111,7 +114,7 @@ protected function newBaseQueryBuilder() $builder->cachePrefix($this->cachePrefixValue($builder)); } - if ($this->cacheDriver) { + if (property_exists($this, 'cacheDriver')) { $builder->cacheDriver($this->cacheDriver); } @@ -119,7 +122,7 @@ protected function newBaseQueryBuilder() $builder->cacheDriver($this->cacheDriverValue($builder)); } - if ($this->cacheUsePlainKey) { + if (property_exists($this, 'cacheUsePlainKey')) { $builder->withPlainKey(); } diff --git a/tests/CountTest.php b/tests/CountTest.php index 9684659..4a4cc71 100644 --- a/tests/CountTest.php +++ b/tests/CountTest.php @@ -7,6 +7,9 @@ class CountTest extends TestCase { + /** + * @dataProvider strictModeContextProvider + */ public function test_count() { $posts = factory(Post::class, 5)->create(); @@ -21,6 +24,9 @@ public function test_count() ); } + /** + * @dataProvider strictModeContextProvider + */ public function test_count_with_columns() { $posts = factory(Post::class, 5)->create(); diff --git a/tests/FirstTest.php b/tests/FirstTest.php index 58c809e..70c407b 100644 --- a/tests/FirstTest.php +++ b/tests/FirstTest.php @@ -7,6 +7,9 @@ class FirstTest extends TestCase { + /** + * @dataProvider strictModeContextProvider + */ public function test_first() { $post = factory(Post::class)->create(); @@ -21,6 +24,9 @@ public function test_first() ); } + /** + * @dataProvider strictModeContextProvider + */ public function test_first_with_columns() { $post = factory(Post::class)->create(); diff --git a/tests/FlushCacheOnUpdatePivotTest.php b/tests/FlushCacheOnUpdatePivotTest.php index cc6d32a..876973d 100644 --- a/tests/FlushCacheOnUpdatePivotTest.php +++ b/tests/FlushCacheOnUpdatePivotTest.php @@ -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;}'; diff --git a/tests/FlushCacheOnUpdateTest.php b/tests/FlushCacheOnUpdateTest.php index 9cf0335..1fe40b1 100644 --- a/tests/FlushCacheOnUpdateTest.php +++ b/tests/FlushCacheOnUpdateTest.php @@ -6,6 +6,9 @@ class FlushCacheOnUpdateTest extends TestCase { + /** + * @dataProvider strictModeContextProvider + */ public function test_flush_cache_on_create() { $page = factory(Page::class)->create(); @@ -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(); @@ -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(); @@ -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(); diff --git a/tests/GetTest.php b/tests/GetTest.php index 3e03618..89793b5 100644 --- a/tests/GetTest.php +++ b/tests/GetTest.php @@ -7,6 +7,9 @@ class GetTest extends TestCase { + /** + * @dataProvider strictModeContextProvider + */ public function test_get() { $post = factory(Post::class)->create(); @@ -26,6 +29,9 @@ public function test_get() ); } + /** + * @dataProvider strictModeContextProvider + */ public function test_get_with_columns() { $post = factory(Post::class)->create(); @@ -45,6 +51,9 @@ public function test_get_with_columns() ); } + /** + * @dataProvider strictModeContextProvider + */ public function test_get_with_string_columns() { $post = factory(Post::class)->create(); diff --git a/tests/LivewireTest.php b/tests/LivewireTest.php new file mode 100644 index 0000000..20fd5f8 --- /dev/null +++ b/tests/LivewireTest.php @@ -0,0 +1,41 @@ +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'; + } +} diff --git a/tests/MethodsTest.php b/tests/MethodsTest.php index 5eb28ac..12e2dc1 100644 --- a/tests/MethodsTest.php +++ b/tests/MethodsTest.php @@ -10,6 +10,9 @@ class MethodsTest extends TestCase { + /** + * @dataProvider strictModeContextProvider + */ public function test_do_not_cache() { $post = factory(Post::class)->create(); @@ -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(); @@ -32,6 +38,9 @@ public function test_cache_prefix() $this->assertNotNull($cache); } + /** + * @dataProvider strictModeContextProvider + */ public function test_cache_tags() { $post = factory(Post::class)->create(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); diff --git a/tests/PaginateTest.php b/tests/PaginateTest.php index 7d8874d..3a98a44 100644 --- a/tests/PaginateTest.php +++ b/tests/PaginateTest.php @@ -7,6 +7,9 @@ class PaginateTest extends TestCase { + /** + * @dataProvider strictModeContextProvider + */ public function test_paginate() { $posts = factory(Post::class, 30)->create(); @@ -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(); diff --git a/tests/SimplePaginateTest.php b/tests/SimplePaginateTest.php index 26d9930..ebca437 100644 --- a/tests/SimplePaginateTest.php +++ b/tests/SimplePaginateTest.php @@ -7,6 +7,9 @@ class SimplePaginateTest extends TestCase { + /** + * @dataProvider strictModeContextProvider + */ public function test_simple_paginate() { $posts = factory(Post::class, 30)->create(); @@ -26,6 +29,9 @@ public function test_simple_paginate() ); } + /** + * @dataProvider strictModeContextProvider + */ public function test_simple_paginate_with_columns() { $posts = factory(Post::class, 30)->create(); diff --git a/tests/TestCase.php b/tests/TestCase.php index 37d41a7..0e2ec9d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,6 +2,7 @@ namespace Rennokki\QueryCache\Test; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Cache; use Orchestra\Testbench\TestCase as Orchestra; @@ -14,6 +15,11 @@ public function setUp(): void { parent::setUp(); + if ($this->getProvidedData() && method_exists(Model::class, 'preventAccessingMissingAttributes')) { + [$strict] = $this->getProvidedData(); + Model::preventAccessingMissingAttributes($strict); + } + $this->resetDatabase(); $this->clearCache(); @@ -31,7 +37,7 @@ public function setUp(): void protected function getPackageProviders($app) { return [ - // + \Livewire\LivewireServiceProvider::class, ]; } @@ -46,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'); } /** @@ -91,6 +106,12 @@ protected function getCacheWithTags(string $key, $tags = null) : Cache::get($key); } + public function strictModeContextProvider(): iterable + { + yield [true]; + yield [false]; + } + /** * Check if the current driver supports tags. * diff --git a/tests/views/livewire/layout.blade.php b/tests/views/livewire/layout.blade.php new file mode 100644 index 0000000..2ef1395 --- /dev/null +++ b/tests/views/livewire/layout.blade.php @@ -0,0 +1,14 @@ + + +
+ + + +Title: {{ $post->name }}
+Time: {{ now() }}
+