From e62fb04ebd28625784acaa4d153fa08453c676e7 Mon Sep 17 00:00:00 2001 From: Tim Van Dijck Date: Mon, 29 Apr 2024 11:25:29 +0200 Subject: [PATCH 01/11] Update testbench --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 709455d..1086fe6 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "livewire/livewire": "^2.11|^3.3.5", "mockery/mockery": "^1.5.1", "openai-php/client": "^0.8.1", - "orchestra/testbench": "^8.0|^9.0", + "orchestra/testbench": "^9.0", "pestphp/pest": "^2.30", "phpstan/extension-installer": "^1.2", "phpstan/phpstan-deprecation-rules": "^1.1.1", From e6de7d5cb7373947d68d359e74b4ac8a243d88bc Mon Sep 17 00:00:00 2001 From: Tim Van Dijck Date: Mon, 29 Apr 2024 11:35:13 +0200 Subject: [PATCH 02/11] Revert "Update testbench" This reverts commit 5fb3d98e2935c2e5cb6fa61327d2aa70e32af408. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 1086fe6..709455d 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "livewire/livewire": "^2.11|^3.3.5", "mockery/mockery": "^1.5.1", "openai-php/client": "^0.8.1", - "orchestra/testbench": "^9.0", + "orchestra/testbench": "^8.0|^9.0", "pestphp/pest": "^2.30", "phpstan/extension-installer": "^1.2", "phpstan/phpstan-deprecation-rules": "^1.1.1", From 2c5c12370a33cf5196420fe3d6a653cdd7b92851 Mon Sep 17 00:00:00 2001 From: Tim Van Dijck Date: Mon, 29 Apr 2024 11:40:01 +0200 Subject: [PATCH 03/11] Update dependencies. --- composer.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 709455d..43288be 100644 --- a/composer.json +++ b/composer.json @@ -31,11 +31,11 @@ "livewire/livewire": "^2.11|^3.3.5", "mockery/mockery": "^1.5.1", "openai-php/client": "^0.8.1", - "orchestra/testbench": "^8.0|^9.0", - "pestphp/pest": "^2.30", - "phpstan/extension-installer": "^1.2", + "orchestra/testbench": "8.22.3|^9.0", + "pestphp/pest": "^2.34", + "phpstan/extension-installer": "^1.3.1", "phpstan/phpstan-deprecation-rules": "^1.1.1", - "phpstan/phpstan-phpunit": "^1.3.3", + "phpstan/phpstan-phpunit": "^1.3.16", "vlucas/phpdotenv": "^5.5" }, "suggest": { From be6663a6687f6d8d7fc2f95c39c1aa957c49c694 Mon Sep 17 00:00:00 2001 From: Tim Van Dijck Date: Mon, 29 Apr 2024 11:46:09 +0200 Subject: [PATCH 04/11] Remove skip for Livewire 3. --- .../LaravelLivewireRequestContextProviderTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/ContextProviders/LaravelLivewireRequestContextProviderTest.php b/tests/ContextProviders/LaravelLivewireRequestContextProviderTest.php index 2cf7fc1..b8a7d00 100644 --- a/tests/ContextProviders/LaravelLivewireRequestContextProviderTest.php +++ b/tests/ContextProviders/LaravelLivewireRequestContextProviderTest.php @@ -5,10 +5,6 @@ use Spatie\LaravelIgnition\ContextProviders\LaravelLivewireRequestContextProvider; use Spatie\LaravelIgnition\Tests\TestClasses\FakeLivewireManager; -beforeEach(function () { - $this->livewireManager = FakeLivewireManager::setUp(); -})->skip(LIVEWIRE_VERSION_3, 'Missing Livewire 3 support.'); - it('returns the referer url and method', function () { $context = createRequestContext([ 'path' => 'referred', From 514798ed2c9d621cdb000ff0c7426bea119fbd3e Mon Sep 17 00:00:00 2001 From: timvandijck Date: Mon, 29 Apr 2024 09:46:13 +0000 Subject: [PATCH 05/11] Fix styling --- .../LaravelLivewireRequestContextProviderTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/ContextProviders/LaravelLivewireRequestContextProviderTest.php b/tests/ContextProviders/LaravelLivewireRequestContextProviderTest.php index b8a7d00..77a821c 100644 --- a/tests/ContextProviders/LaravelLivewireRequestContextProviderTest.php +++ b/tests/ContextProviders/LaravelLivewireRequestContextProviderTest.php @@ -3,7 +3,6 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; use Spatie\LaravelIgnition\ContextProviders\LaravelLivewireRequestContextProvider; -use Spatie\LaravelIgnition\Tests\TestClasses\FakeLivewireManager; it('returns the referer url and method', function () { $context = createRequestContext([ From 45f5ee2840d9a23a061753cf1bce9c69233cc40e Mon Sep 17 00:00:00 2001 From: Tim Van Dijck Date: Mon, 29 Apr 2024 12:31:04 +0200 Subject: [PATCH 06/11] Fix Livewire tests. --- ...avelLivewireRequestContextProviderTest.php | 31 ++-- ...gacyLivewireRequestContextProviderTest.php | 172 ++++++++++++++++++ tests/Pest.php | 1 + tests/TestClasses/FakeLivewireManager.php | 3 +- 4 files changed, 192 insertions(+), 15 deletions(-) create mode 100644 tests/ContextProviders/LegacyLivewireRequestContextProviderTest.php diff --git a/tests/ContextProviders/LaravelLivewireRequestContextProviderTest.php b/tests/ContextProviders/LaravelLivewireRequestContextProviderTest.php index 77a821c..c2e615e 100644 --- a/tests/ContextProviders/LaravelLivewireRequestContextProviderTest.php +++ b/tests/ContextProviders/LaravelLivewireRequestContextProviderTest.php @@ -4,6 +4,10 @@ use Illuminate\Support\Facades\Route; use Spatie\LaravelIgnition\ContextProviders\LaravelLivewireRequestContextProvider; +beforeEach(function () { + $this->livewireManager = FakeLivewireManager::setUp(); +})->skip(LIVEWIRE_VERSION_2, 'Only test Livewire 3.'); + it('returns the referer url and method', function () { $context = createRequestContext([ 'path' => 'referred', @@ -12,15 +16,15 @@ $request = $context->getRequest(); - expect($request['url'])->toBe('http://localhost/referred'); - expect($request['method'])->toBe('GET'); + expect($request['url'])->toBe('http://localhost/POST'); + expect($request['method'])->toBe('POST'); }); it('returns livewire component information', function () { $alias = 'fake-component'; $class = 'fake-class'; - $this->livewireManager->fakeAliases[$alias] = $class; + resolve(FakeLivewireManager::class)->addAlias($alias, $class); $context = createRequestContext([ 'path' => 'http://localhost/referred', @@ -31,9 +35,8 @@ $livewire = $context->toArray()['livewire']; - expect($livewire['component_id'])->toBe($id); - expect($livewire['component_alias'])->toBe($alias); - expect($livewire['component_class'])->toBe($class); + expect($livewire[0]['component_id'])->toBe($id); + expect($livewire[0]['component_alias'])->toBe($alias); }); it('returns livewire component information when it does not exist', function () { @@ -46,9 +49,9 @@ $livewire = $context->toArray()['livewire']; - expect($livewire['component_id'])->toBe($id); - expect($livewire['component_alias'])->toBe($name); - expect($livewire['component_class'])->toBeNull(); + expect($livewire[0]['component_id'])->toBe($id); + expect($livewire[0]['component_alias'])->toBe($name); + expect($livewire[0]['component_class'])->toBeNull(); }); it('removes ids from update payloads', function () { @@ -70,9 +73,9 @@ $livewire = $context->toArray()['livewire']; - expect($livewire['component_id'])->toBe($id); - expect($livewire['component_alias'])->toBe($name); - expect($livewire['component_class'])->toBeNull(); + expect($livewire[0]['component_id'])->toBe($id); + expect($livewire[0]['component_alias'])->toBe($name); + expect($livewire[0]['component_class'])->toBeNull(); }); it('combines data into one payload', function () { @@ -145,7 +148,7 @@ "collection" => ['a', 'b'], "stringable" => "Test", "wireable" => ['a', 'b'], - ], $livewire['data']); + ], $livewire[0]['data']); }); // Helpers @@ -163,5 +166,5 @@ function createRequestContext(array $fingerprint, array $updates = [], array $se 'updates' => $updates, ], ['X-Livewire' => 1]); - return new LaravelLivewireRequestContextProvider($providedRequest, test()->livewireManager); + return new LaravelLivewireRequestContextProvider($providedRequest, new FakeLivewireManager()); } diff --git a/tests/ContextProviders/LegacyLivewireRequestContextProviderTest.php b/tests/ContextProviders/LegacyLivewireRequestContextProviderTest.php new file mode 100644 index 0000000..335ea9f --- /dev/null +++ b/tests/ContextProviders/LegacyLivewireRequestContextProviderTest.php @@ -0,0 +1,172 @@ +livewireManager = FakeLivewireManager::setUp(); +})->skip(LIVEWIRE_VERSION_3, 'Only test Livewire 2.'); + +it('returns the referer url and method', function () { + $context = createRequestContext([ + 'path' => 'referred', + 'method' => 'GET', + ]); + + $request = $context->getRequest(); + + expect($request['url'])->toBe('http://localhost/referred'); + expect($request['method'])->toBe('GET'); +}); + +it('returns livewire component information', function () { + $alias = 'fake-component'; + $class = 'fake-class'; + + $this->livewireManager->fakeAliases[$alias] = $class; + + $context = createRequestContext([ + 'path' => 'http://localhost/referred', + 'method' => 'GET', + 'id' => $id = uniqid(), + 'name' => $alias, + ]); + + $livewire = $context->toArray()['livewire']; + + expect($livewire[0]['component_id'])->toBe($id); + expect($livewire[0]['component_alias'])->toBe($alias); + expect($livewire[0]['component_class'])->toBe($class); +}); + +it('returns livewire component information when it does not exist', function () { + $context = createRequestContext([ + 'path' => 'http://localhost/referred', + 'method' => 'GET', + 'id' => $id = uniqid(), + 'name' => $name = 'fake-component', + ]); + + $livewire = $context->toArray()['livewire']; + + expect($livewire[0]['component_id'])->toBe($id); + expect($livewire[0]['component_alias'])->toBe($name); + expect($livewire[0]['component_class'])->toBeNull(); +}); + +it('removes ids from update payloads', function () { + $context = createRequestContext([ + 'path' => 'http://localhost/referred', + 'method' => 'GET', + 'id' => $id = uniqid(), + 'name' => $name = 'fake-component', + ], [ + [ + 'type' => 'callMethod', + 'payload' => [ + 'id' => 'remove-me', + 'method' => 'chang', + 'params' => ['a'], + ], + ], + ]); + + $livewire = $context->toArray()['livewire']; + + expect($livewire[0]['component_id'])->toBe($id); + expect($livewire[0]['component_alias'])->toBe($name); + expect($livewire[0]['component_class'])->toBeNull(); +}); + +it('combines data into one payload', function () { + $context = createRequestContext([ + 'path' => 'http://localhost/referred', + 'method' => 'GET', + 'id' => uniqid(), + 'name' => 'fake-component', + ], [], [ + 'data' => [ + 'string' => 'Ruben', + 'array' => ['a', 'b'], + 'modelCollection' => [], + 'model' => [], + 'date' => '2021-11-10T14:20:36+0000', + 'collection' => ['a', 'b'], + 'stringable' => 'Test', + 'wireable' => ['a', 'b'], + ], + 'dataMeta' => [ + 'modelCollections' => [ + 'modelCollection' => [ + 'class' => 'App\\\\Models\\\\User', + 'id' => [1, 2, 3, 4], + 'relations' => [], + 'connection' => 'mysql', + ], + ], + 'models' => [ + 'model' => [ + 'class' => 'App\\\\Models\\\\User', + 'id' => 1, + 'relations' => [], + 'connection' => 'mysql', + ], + ], + 'dates' => [ + 'date' => 'carbonImmutable', + ], + 'collections' => [ + 'collection', + ], + 'stringables' => [ + 'stringable', + ], + 'wireables' => [ + 'wireable', + ], + ], + ]); + + $livewire = $context->toArray()['livewire']; + + $this->assertEquals([ + "string" => "Ruben", + "array" => ['a', 'b'], + "modelCollection" => [ + "class" => "App\\\\Models\\\\User", + "id" => [1, 2, 3, 4], + "relations" => [], + "connection" => "mysql", + ], + "model" => [ + "class" => "App\\\\Models\\\\User", + "id" => 1, + "relations" => [], + "connection" => "mysql", + ], + "date" => "2021-11-10T14:20:36+0000", + "collection" => ['a', 'b'], + "stringable" => "Test", + "wireable" => ['a', 'b'], + ], $livewire[0]['data']); +}); + +// Helpers +function createRequestContext(array $fingerprint, array $updates = [], array $serverMemo = []): LaravelLivewireRequestContextProvider +{ + $providedRequest = null; + + Route::post('livewire', function (Request $request) use (&$providedRequest) { + $providedRequest = $request; + })->name('livewire.message'); + + test()->postJson('livewire', [ + 'fingerprint' => $fingerprint, + 'serverMemo' => $serverMemo, + 'updates' => $updates, + ], ['X-Livewire' => 1]); + + return new LaravelLivewireRequestContextProvider($providedRequest, test()->livewireManager); +} diff --git a/tests/Pest.php b/tests/Pest.php index 2166e3e..4a647f7 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -4,6 +4,7 @@ use Livewire\Mechanisms\ComponentRegistry; use Spatie\LaravelIgnition\Tests\TestCase; +define('LIVEWIRE_VERSION_2', !class_exists(ComponentRegistry::class)); define('LIVEWIRE_VERSION_3', class_exists(ComponentRegistry::class)); uses(TestCase::class)->in(__DIR__); diff --git a/tests/TestClasses/FakeLivewireManager.php b/tests/TestClasses/FakeLivewireManager.php index 394ae3f..11344a4 100644 --- a/tests/TestClasses/FakeLivewireManager.php +++ b/tests/TestClasses/FakeLivewireManager.php @@ -3,6 +3,7 @@ namespace Spatie\LaravelIgnition\Tests\TestClasses; use Livewire\LivewireManager; +use Livewire\Mechanisms\ComponentRegistry; class FakeLivewireManager extends LivewireManager { @@ -24,7 +25,7 @@ public function isDefinitelyLivewireRequest() public function getClass($alias) { - return $this->fakeAliases[$alias] ?? parent::getClass($alias); + return $this->fakeAliases[$alias] ?? app(ComponentRegistry::class)->getClass($alias); } public function addAlias(string $alias, string $class): void From 7e96cd0d6a957a7d365bd0afe1bedbc8dfc5f037 Mon Sep 17 00:00:00 2001 From: timvandijck Date: Mon, 29 Apr 2024 10:32:09 +0000 Subject: [PATCH 07/11] Fix styling --- tests/Pest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Pest.php b/tests/Pest.php index 4a647f7..1094629 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -4,7 +4,7 @@ use Livewire\Mechanisms\ComponentRegistry; use Spatie\LaravelIgnition\Tests\TestCase; -define('LIVEWIRE_VERSION_2', !class_exists(ComponentRegistry::class)); +define('LIVEWIRE_VERSION_2', ! class_exists(ComponentRegistry::class)); define('LIVEWIRE_VERSION_3', class_exists(ComponentRegistry::class)); uses(TestCase::class)->in(__DIR__); From bc24e3b6511a24b37e16a2accf38cb4470dce880 Mon Sep 17 00:00:00 2001 From: Tim Van Dijck Date: Mon, 29 Apr 2024 12:34:28 +0200 Subject: [PATCH 08/11] Avoid duplicate method name. --- .../LegacyLivewireRequestContextProviderTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/ContextProviders/LegacyLivewireRequestContextProviderTest.php b/tests/ContextProviders/LegacyLivewireRequestContextProviderTest.php index 335ea9f..8c1c958 100644 --- a/tests/ContextProviders/LegacyLivewireRequestContextProviderTest.php +++ b/tests/ContextProviders/LegacyLivewireRequestContextProviderTest.php @@ -10,7 +10,7 @@ })->skip(LIVEWIRE_VERSION_3, 'Only test Livewire 2.'); it('returns the referer url and method', function () { - $context = createRequestContext([ + $context = createLegacyRequestContext([ 'path' => 'referred', 'method' => 'GET', ]); @@ -27,7 +27,7 @@ $this->livewireManager->fakeAliases[$alias] = $class; - $context = createRequestContext([ + $context = createLegacyRequestContext([ 'path' => 'http://localhost/referred', 'method' => 'GET', 'id' => $id = uniqid(), @@ -42,7 +42,7 @@ }); it('returns livewire component information when it does not exist', function () { - $context = createRequestContext([ + $context = createLegacyRequestContext([ 'path' => 'http://localhost/referred', 'method' => 'GET', 'id' => $id = uniqid(), @@ -57,7 +57,7 @@ }); it('removes ids from update payloads', function () { - $context = createRequestContext([ + $context = createLegacyRequestContext([ 'path' => 'http://localhost/referred', 'method' => 'GET', 'id' => $id = uniqid(), @@ -81,7 +81,7 @@ }); it('combines data into one payload', function () { - $context = createRequestContext([ + $context = createLegacyRequestContext([ 'path' => 'http://localhost/referred', 'method' => 'GET', 'id' => uniqid(), @@ -154,7 +154,7 @@ }); // Helpers -function createRequestContext(array $fingerprint, array $updates = [], array $serverMemo = []): LaravelLivewireRequestContextProvider +function createLegacyRequestContext(array $fingerprint, array $updates = [], array $serverMemo = []): LaravelLivewireRequestContextProvider { $providedRequest = null; From 1f4ab777b65ecce96e9f0c283e4d219e49dd6573 Mon Sep 17 00:00:00 2001 From: Tim Van Dijck Date: Mon, 29 Apr 2024 12:40:38 +0200 Subject: [PATCH 09/11] Fix test. --- .../LaravelLivewireRequestContextProviderTest.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/ContextProviders/LaravelLivewireRequestContextProviderTest.php b/tests/ContextProviders/LaravelLivewireRequestContextProviderTest.php index c2e615e..5e0137f 100644 --- a/tests/ContextProviders/LaravelLivewireRequestContextProviderTest.php +++ b/tests/ContextProviders/LaravelLivewireRequestContextProviderTest.php @@ -3,9 +3,10 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; use Spatie\LaravelIgnition\ContextProviders\LaravelLivewireRequestContextProvider; +use Spatie\LaravelIgnition\Tests\TestClasses\FakeLivewireManager; beforeEach(function () { - $this->livewireManager = FakeLivewireManager::setUp(); + $this->livewireManager = resolve(FakeLivewireManager::class); })->skip(LIVEWIRE_VERSION_2, 'Only test Livewire 3.'); it('returns the referer url and method', function () { @@ -24,7 +25,7 @@ $alias = 'fake-component'; $class = 'fake-class'; - resolve(FakeLivewireManager::class)->addAlias($alias, $class); + $this->livewireManager->addAlias($alias, $class); $context = createRequestContext([ 'path' => 'http://localhost/referred', @@ -166,5 +167,5 @@ function createRequestContext(array $fingerprint, array $updates = [], array $se 'updates' => $updates, ], ['X-Livewire' => 1]); - return new LaravelLivewireRequestContextProvider($providedRequest, new FakeLivewireManager()); + return new LaravelLivewireRequestContextProvider($providedRequest, test()->livewireManager); } From 1f106c93639ae4f3553848cf801c9e5f34d4cf50 Mon Sep 17 00:00:00 2001 From: Tim Van Dijck Date: Mon, 29 Apr 2024 13:49:41 +0200 Subject: [PATCH 10/11] Reapply reverted code --- .../LaravelLivewireRequestContextProvider.php | 40 ++++++++++++++++--- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/src/ContextProviders/LaravelLivewireRequestContextProvider.php b/src/ContextProviders/LaravelLivewireRequestContextProvider.php index e421e11..7a73d6e 100644 --- a/src/ContextProviders/LaravelLivewireRequestContextProvider.php +++ b/src/ContextProviders/LaravelLivewireRequestContextProvider.php @@ -40,6 +40,26 @@ public function toArray(): array /** @return array */ protected function getLivewireInformation(): array { + if ($this->request->has('components')) { + $data = []; + + foreach ($this->request->get('components') as $component) { + $snapshot = json_decode($component['snapshot'], true); + + $class = app(ComponentRegistry::class)->getClass($snapshot['memo']['name']); + + $data[] = [ + 'component_class' => $class ?? null, + 'data' => $snapshot['data'], + 'memo' => $snapshot['memo'], + 'updates' => $this->resolveUpdates($component['updates']), + 'calls' => $component['calls'], + ]; + } + + return $data; + } + /** @phpstan-ignore-next-line */ $componentId = $this->request->input('fingerprint.id'); @@ -56,12 +76,20 @@ protected function getLivewireInformation(): array $componentClass = null; } + /** @phpstan-ignore-next-line */ + $updates = $this->request->input('updates') ?? []; + + /** @phpstan-ignore-next-line */ + $updates = $this->request->input('updates') ?? []; + return [ - 'component_class' => $componentClass, - 'component_alias' => $componentAlias, - 'component_id' => $componentId, - 'data' => $this->resolveData(), - 'updates' => $this->resolveUpdates(), + [ + 'component_class' => $componentClass, + 'component_alias' => $componentAlias, + 'component_id' => $componentId, + 'data' => $this->resolveData(), + 'updates' => $this->resolveUpdates($updates), + ], ]; } @@ -86,7 +114,7 @@ protected function resolveData(): array } /** @return array */ - protected function resolveUpdates(): array + protected function resolveUpdates(array $updates): array { /** @phpstan-ignore-next-line */ $updates = $this->request->input('updates') ?? []; From f265b42cab5664c46676316fb555a1c19f12be5b Mon Sep 17 00:00:00 2001 From: Tim Van Dijck Date: Mon, 29 Apr 2024 15:18:06 +0200 Subject: [PATCH 11/11] Fix tests. --- .github/workflows/run-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index e3a9a75..8c8898c 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -30,7 +30,7 @@ jobs: uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} - extensions: mbstring, fileinfo + extensions: mbstring, fileinfo, pdo_sqlite coverage: none tools: composer:v2