From 5ad221da658a7b556702104b09878bf68370b3ee Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Wed, 9 Nov 2022 14:12:21 +0200 Subject: [PATCH 1/4] Added tests for strict mode in models --- composer.json | 4 ++-- tests/CountTest.php | 6 +++++ tests/FirstTest.php | 6 +++++ tests/FlushCacheOnUpdatePivotTest.php | 3 +++ tests/FlushCacheOnUpdateTest.php | 12 ++++++++++ tests/GetTest.php | 9 +++++++ tests/MethodsTest.php | 34 +++++++++++++++++++++++++++ tests/PaginateTest.php | 6 +++++ tests/SimplePaginateTest.php | 7 ++++++ tests/TestCase.php | 12 ++++++++++ 10 files changed, 97 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 1209e0b..9538e7c 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": { 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/MethodsTest.php b/tests/MethodsTest.php index 5eb28ac..89754e6 100644 --- a/tests/MethodsTest.php +++ b/tests/MethodsTest.php @@ -2,6 +2,7 @@ namespace Rennokki\QueryCache\Test; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Cache; use Rennokki\QueryCache\Test\Models\Book; use Rennokki\QueryCache\Test\Models\Kid; @@ -10,6 +11,9 @@ class MethodsTest extends TestCase { + /** + * @dataProvider strictModeContextProvider + */ public function test_do_not_cache() { $post = factory(Post::class)->create(); @@ -23,6 +27,9 @@ public function test_do_not_cache() $this->assertNull($cache); } + /** + * @dataProvider strictModeContextProvider + */ public function test_cache_prefix() { $post = factory(Post::class)->create(); @@ -32,6 +39,9 @@ public function test_cache_prefix() $this->assertNotNull($cache); } + /** + * @dataProvider strictModeContextProvider + */ public function test_cache_tags() { $post = factory(Post::class)->create(); @@ -49,6 +59,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 +76,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 +99,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 +120,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 +138,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 +150,9 @@ public function test_hashed_key() $this->assertNotNull($cache); } + /** + * @dataProvider strictModeContextProvider + */ public function test_append_cache_tags() { $post = factory(Post::class)->create(); @@ -142,6 +170,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 +181,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..c243a0d 100644 --- a/tests/SimplePaginateTest.php +++ b/tests/SimplePaginateTest.php @@ -2,11 +2,15 @@ namespace Rennokki\QueryCache\Test; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Cache; use Rennokki\QueryCache\Test\Models\Post; class SimplePaginateTest extends TestCase { + /** + * @dataProvider strictModeContextProvider + */ public function test_simple_paginate() { $posts = factory(Post::class, 30)->create(); @@ -26,6 +30,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..4b9e763 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()) { + [$strict] = $this->getProvidedData(); + Model::preventAccessingMissingAttributes($strict); + } + $this->resetDatabase(); $this->clearCache(); @@ -91,6 +97,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. * From dc6c7bfaf2b1fdf988949804d5a7f33c95f46556 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Wed, 9 Nov 2022 14:18:12 +0200 Subject: [PATCH 2/4] Check if method exists beforehand --- tests/TestCase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TestCase.php b/tests/TestCase.php index 4b9e763..3705f41 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -15,7 +15,7 @@ public function setUp(): void { parent::setUp(); - if ($this->getProvidedData()) { + if ($this->getProvidedData() && method_exists(Model::class, 'preventAccessingMissingAttributes')) { [$strict] = $this->getProvidedData(); Model::preventAccessingMissingAttributes($strict); } From dfb54c27ebf084d0516aec2441d9aa09f41c9d8b Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Fri, 11 Nov 2022 13:31:57 +0200 Subject: [PATCH 3/4] Linting --- tests/MethodsTest.php | 1 - tests/SimplePaginateTest.php | 1 - 2 files changed, 2 deletions(-) diff --git a/tests/MethodsTest.php b/tests/MethodsTest.php index 89754e6..12e2dc1 100644 --- a/tests/MethodsTest.php +++ b/tests/MethodsTest.php @@ -2,7 +2,6 @@ namespace Rennokki\QueryCache\Test; -use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Cache; use Rennokki\QueryCache\Test\Models\Book; use Rennokki\QueryCache\Test\Models\Kid; diff --git a/tests/SimplePaginateTest.php b/tests/SimplePaginateTest.php index c243a0d..ebca437 100644 --- a/tests/SimplePaginateTest.php +++ b/tests/SimplePaginateTest.php @@ -2,7 +2,6 @@ namespace Rennokki\QueryCache\Test; -use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Cache; use Rennokki\QueryCache\Test\Models\Post; From 81fec3431d480822a7cbec223a5e6137ade119e3 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Sun, 13 Nov 2022 20:46:17 +0200 Subject: [PATCH 4/4] Fixed polling because of direct attribute checks --- composer.json | 1 + src/Traits/QueryCacheable.php | 13 +++++---- tests/LivewireTest.php | 41 +++++++++++++++++++++++++++ tests/TestCase.php | 13 +++++++-- tests/views/livewire/layout.blade.php | 14 +++++++++ tests/views/livewire/post.blade.php | 4 +++ 6 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 tests/LivewireTest.php create mode 100644 tests/views/livewire/layout.blade.php create mode 100644 tests/views/livewire/post.blade.php diff --git a/composer.json b/composer.json index 9538e7c..1803029 100644 --- a/composer.json +++ b/composer.json @@ -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/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/TestCase.php b/tests/TestCase.php index 3705f41..0e2ec9d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -37,7 +37,7 @@ public function setUp(): void protected function getPackageProviders($app) { return [ - // + \Livewire\LivewireServiceProvider::class, ]; } @@ -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'); } /** 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 @@ + + + + + + + Document + @livewireStyles + + + + @livewireScripts + + diff --git a/tests/views/livewire/post.blade.php b/tests/views/livewire/post.blade.php new file mode 100644 index 0000000..fb74375 --- /dev/null +++ b/tests/views/livewire/post.blade.php @@ -0,0 +1,4 @@ +
+

Title: {{ $post->name }}

+

Time: {{ now() }}

+