From a172fd1f635d7290525efacfc13942b5da8884ea Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Tue, 22 Apr 2025 02:52:59 +0100 Subject: [PATCH 01/10] Initial Commit --- src/Views/Columns/LivewireComponentColumn.php | 44 +++----- .../LivewireComponentColumnConfiguration.php | 7 ++ .../LivewireComponentColumnHelpers.php | 106 +++++++++++++++++- tests/Http/Livewire/TestComponent.php | 27 +++++ .../Columns/LivewireComponentColumnTest.php | 76 +++++++++++++ 5 files changed, 228 insertions(+), 32 deletions(-) create mode 100644 tests/Http/Livewire/TestComponent.php diff --git a/src/Views/Columns/LivewireComponentColumn.php b/src/Views/Columns/LivewireComponentColumn.php index 0049c338a..e6ae66462 100644 --- a/src/Views/Columns/LivewireComponentColumn.php +++ b/src/Views/Columns/LivewireComponentColumn.php @@ -16,40 +16,26 @@ class LivewireComponentColumn extends Column use LivewireComponentColumnConfiguration, LivewireComponentColumnHelpers; + /** + * The Livewire Component assigned to this Column + * + * @var string|null + */ protected ?string $livewireComponent; + /** + * Gets the contents for current row + * + * @param Model $row + * @return null|string|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View + */ public function getContents(Model $row): null|string|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View { - if (! $this->hasLivewireComponent()) { - throw new DataTableConfigurationException('You must define a Livewire Component for this column'); - } + $this->runPreChecks(); + + $attributes = $this->retrieveAttributes($row); - if ($this->isLabel()) { - throw new DataTableConfigurationException('You can not use a label column with a Livewire Component column'); - } - - $attributes = []; - $value = $this->getValue($row); - - if ($this->hasAttributesCallback()) { - $attributes = call_user_func($this->getAttributesCallback(), $value, $row, $this); - - if (! is_array($attributes)) { - throw new DataTableConfigurationException('The return type of callback must be an array'); - } - } - - $implodedAttributes = collect($attributes)->map(function ($value, $key) { - return ':'.$key.'="$'.$key.'"'; - })->implode(' '); - - return new HtmlString(Blade::render( - '', - [ - 'component' => $this->getLivewireComponent(), - ...$attributes, - ], - )); + return $this->getHtmlString($attributes, $this->getTable()."-".$row->{$row->getKeyName()}); } } diff --git a/src/Views/Columns/Traits/Configuration/LivewireComponentColumnConfiguration.php b/src/Views/Columns/Traits/Configuration/LivewireComponentColumnConfiguration.php index 54b62de71..fff0d3cac 100644 --- a/src/Views/Columns/Traits/Configuration/LivewireComponentColumnConfiguration.php +++ b/src/Views/Columns/Traits/Configuration/LivewireComponentColumnConfiguration.php @@ -6,6 +6,13 @@ trait LivewireComponentColumnConfiguration { + + /** + * Defines which component to use + * + * @param string $livewireComponent + * @return self + */ public function component(string $livewireComponent): self { $this->livewireComponent = (Str::startsWith($livewireComponent, 'livewire:')) ? substr($livewireComponent, 9) : $livewireComponent; diff --git a/src/Views/Columns/Traits/Helpers/LivewireComponentColumnHelpers.php b/src/Views/Columns/Traits/Helpers/LivewireComponentColumnHelpers.php index f1c4cff1b..32cfc0eaf 100644 --- a/src/Views/Columns/Traits/Helpers/LivewireComponentColumnHelpers.php +++ b/src/Views/Columns/Traits/Helpers/LivewireComponentColumnHelpers.php @@ -2,21 +2,121 @@ namespace Rappasoft\LaravelLivewireTables\Views\Columns\Traits\Helpers; +use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\Blade; +use Illuminate\Support\HtmlString; + trait LivewireComponentColumnHelpers { + /** * Retrieves the defined Component View - */ + * + * @return string|null + */ public function getLivewireComponent(): ?string { return $this->livewireComponent ?? null; } + /** - * Determines whether a Component View has been set - */ + * Determines whether a Livewire Component has been set + * + * @return boolean + */ public function hasLivewireComponent(): bool { return isset($this->livewireComponent); } + + /** + * Retrieves attributes based on callback + * + * @param Model $row + * @return array + */ + protected function retrieveAttributes(Model $row): array + { + $value = $this->getValue($row); + + $attributes = ['value' => $value]; + + if ($this->hasAttributesCallback()) { + $attributes = call_user_func($this->getAttributesCallback(), $value, $row, $this); + + if (! is_array($attributes)) { + throw new DataTableConfigurationException('The return type of callback must be an array'); + } + } + return $attributes; + } + + /** + * Runs pre-checks + * + * @return boolean + */ + protected function runPreChecks(): bool + { + if (! $this->hasLivewireComponent()) { + throw new DataTableConfigurationException('You must define a Livewire Component for this column'); + return false; + } + + if ($this->isLabel()) { + throw new DataTableConfigurationException('You can not use a label column with a Livewire Component column'); + return false; + } + + return true; + } + + /** + * Implodes defined attributes to be used + * + * @param array $attributes + * @return string + */ + protected function implodeAttributes(array $attributes): string + { + return collect($attributes)->map(function ($value, $key) { + return ':'.$key.'="$'.$key.'"'; + })->implode(' '); + } + + /** + * getBlade Render + * + * @param array $attributes + * @param string $key + */ + protected function getBlade(array $attributes, string $key) + { + return Blade::render( + 'implodeAttributes($attributes).' />', + [ + 'component' => $this->getLivewireComponent(), + 'key' => $key, + ...$attributes, + ], + ); + } + + /** + * Gets HTML STring + * + * @param array $attributes + * @param string $key + * @return HtmlString + */ + protected function getHtmlString(array $attributes, string $key): HtmlString + { + return new HtmlString($this->getBlade($attributes, $key)); + + } + + + } diff --git a/tests/Http/Livewire/TestComponent.php b/tests/Http/Livewire/TestComponent.php new file mode 100644 index 000000000..ec715d8d1 --- /dev/null +++ b/tests/Http/Livewire/TestComponent.php @@ -0,0 +1,27 @@ +testItem = $age * 110; + } + + /** + * Get the view / contents that represent the component. + */ + public function render(): View|Closure|string + { + return \Illuminate\Support\Facades\Blade::render( + '
'.($this->testItem ?? 'Unknown').'
'); + + } +} diff --git a/tests/Unit/Views/Columns/LivewireComponentColumnTest.php b/tests/Unit/Views/Columns/LivewireComponentColumnTest.php index ac9c5b0a0..75e1b6a35 100644 --- a/tests/Unit/Views/Columns/LivewireComponentColumnTest.php +++ b/tests/Unit/Views/Columns/LivewireComponentColumnTest.php @@ -7,6 +7,7 @@ use Rappasoft\LaravelLivewireTables\Tests\Models\Pet; use Rappasoft\LaravelLivewireTables\Views\Column; use Rappasoft\LaravelLivewireTables\Views\Columns\LivewireComponentColumn; +use Illuminate\Database\Eloquent\Model; #[Group('Columns')] final class LivewireComponentColumnTest extends ColumnTestCase @@ -89,4 +90,79 @@ public function test_can_set_attribute_callback(): void $this->assertTrue(self::$columnInstance->hasAttributesCallback()); } + + public static function setup_with_public_methods() + { + \Livewire\Livewire::component('test-component', \Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\TestComponent::class); + + $row = Pet::find(1); + + $temp = (new class("name", "name") extends LivewireComponentColumn + { + + public function pubRetrieveAttributes(Model $row) + { + return $this->retrieveAttributes($row); + } + + public function pubImplodeAttributes(array $attributes) + { + return $this->implodeAttributes($attributes); + } + + public function pubGetBlade(array $attributes, string $key) + { + return $this->getBlade($attributes,$key); + } + + public function pubGetHtmlString(array $attributes, string $key) + { + return $this->getHtmlString($attributes,$key); + } + + })->component('test-component')->attributes(function ($columnValue, $row) { + return [ + 'type' => "test", + 'name' => $row->name, + ]; + }); + + $temp->setTable('test-table'); + + return $temp; + } + + + public function test_can_get_attributes_correctly(): void + { + $row = Pet::find(1); + $temp = self::setup_with_public_methods(); + $key = "test-table-".$row->{$row->getKeyName()}; + + $this->assertSame(['type' => 'test', 'name' => 'Cartman'], $temp->pubRetrieveAttributes($row)); + + $this->assertSame(':type="$type" :name="$name"', $temp->pubImplodeAttributes($temp->pubRetrieveAttributes($row))); + } + + public function test_can_get_blade_correctly(): void + { + $row = Pet::find(1); + $temp = self::setup_with_public_methods(); + $key = "test-table-".$row->{$row->getKeyName()}; + + $this->assertStringContainsString('wire:snapshot="{"data":{"id":null,"name":"Cartman","value":null,"type":"test"}', $temp->pubGetBlade($temp->pubRetrieveAttributes($row),$key)); + + $this->assertStringContainsString('
Name:Cartman
Type:test
', $temp->pubGetBlade($temp->pubRetrieveAttributes($row),$key)); + } + + public function test_can_get_html_string_correctly(): void + { + $row = Pet::find(1); + $temp = self::setup_with_public_methods(); + $key = "test-table-".$row->{$row->getKeyName()}; + + $this->assertStringContainsString('
Name:Cartman
Type:test
', $temp->pubGetHtmlString($temp->pubRetrieveAttributes($row),$key)); + } + } + From 5ae60206c8faf2dad9351c383790dbad5ee1a102 Mon Sep 17 00:00:00 2001 From: lrljoe <104938042+lrljoe@users.noreply.github.com> Date: Tue, 22 Apr 2025 01:53:22 +0000 Subject: [PATCH 02/10] Fix styling --- src/Views/Columns/LivewireComponentColumn.php | 9 +--- .../LivewireComponentColumnConfiguration.php | 4 -- .../LivewireComponentColumnHelpers.php | 45 +++++----------- tests/Http/Livewire/TestComponent.php | 54 +++++++++---------- .../Columns/LivewireComponentColumnTest.php | 29 +++++----- 5 files changed, 53 insertions(+), 88 deletions(-) diff --git a/src/Views/Columns/LivewireComponentColumn.php b/src/Views/Columns/LivewireComponentColumn.php index e6ae66462..4111a6728 100644 --- a/src/Views/Columns/LivewireComponentColumn.php +++ b/src/Views/Columns/LivewireComponentColumn.php @@ -18,24 +18,19 @@ class LivewireComponentColumn extends Column /** * The Livewire Component assigned to this Column - * - * @var string|null */ protected ?string $livewireComponent; /** * Gets the contents for current row - * - * @param Model $row - * @return null|string|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View */ public function getContents(Model $row): null|string|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View { $this->runPreChecks(); - + $attributes = $this->retrieveAttributes($row); - return $this->getHtmlString($attributes, $this->getTable()."-".$row->{$row->getKeyName()}); + return $this->getHtmlString($attributes, $this->getTable().'-'.$row->{$row->getKeyName()}); } } diff --git a/src/Views/Columns/Traits/Configuration/LivewireComponentColumnConfiguration.php b/src/Views/Columns/Traits/Configuration/LivewireComponentColumnConfiguration.php index fff0d3cac..e5a168484 100644 --- a/src/Views/Columns/Traits/Configuration/LivewireComponentColumnConfiguration.php +++ b/src/Views/Columns/Traits/Configuration/LivewireComponentColumnConfiguration.php @@ -6,12 +6,8 @@ trait LivewireComponentColumnConfiguration { - /** * Defines which component to use - * - * @param string $livewireComponent - * @return self */ public function component(string $livewireComponent): self { diff --git a/src/Views/Columns/Traits/Helpers/LivewireComponentColumnHelpers.php b/src/Views/Columns/Traits/Helpers/LivewireComponentColumnHelpers.php index 32cfc0eaf..55bedf325 100644 --- a/src/Views/Columns/Traits/Helpers/LivewireComponentColumnHelpers.php +++ b/src/Views/Columns/Traits/Helpers/LivewireComponentColumnHelpers.php @@ -2,30 +2,24 @@ namespace Rappasoft\LaravelLivewireTables\Views\Columns\Traits\Helpers; -use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Blade; use Illuminate\Support\HtmlString; +use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; trait LivewireComponentColumnHelpers { - /** * Retrieves the defined Component View - * - * @return string|null - */ + */ public function getLivewireComponent(): ?string { return $this->livewireComponent ?? null; } - /** * Determines whether a Livewire Component has been set - * - * @return boolean - */ + */ public function hasLivewireComponent(): bool { return isset($this->livewireComponent); @@ -33,9 +27,6 @@ public function hasLivewireComponent(): bool /** * Retrieves attributes based on callback - * - * @param Model $row - * @return array */ protected function retrieveAttributes(Model $row): array { @@ -50,23 +41,24 @@ protected function retrieveAttributes(Model $row): array throw new DataTableConfigurationException('The return type of callback must be an array'); } } + return $attributes; } /** * Runs pre-checks - * - * @return boolean */ protected function runPreChecks(): bool { if (! $this->hasLivewireComponent()) { throw new DataTableConfigurationException('You must define a Livewire Component for this column'); + return false; } if ($this->isLabel()) { throw new DataTableConfigurationException('You can not use a label column with a Livewire Component column'); + return false; } @@ -74,11 +66,8 @@ protected function runPreChecks(): bool } /** - * Implodes defined attributes to be used - * - * @param array $attributes - * @return string - */ + * Implodes defined attributes to be used + */ protected function implodeAttributes(array $attributes): string { return collect($attributes)->map(function ($value, $key) { @@ -86,12 +75,9 @@ protected function implodeAttributes(array $attributes): string })->implode(' '); } - /** - * getBlade Render - * - * @param array $attributes - * @param string $key - */ + /** + * getBlade Render + */ protected function getBlade(array $attributes, string $key) { return Blade::render( @@ -106,17 +92,10 @@ protected function getBlade(array $attributes, string $key) /** * Gets HTML STring - * - * @param array $attributes - * @param string $key - * @return HtmlString */ - protected function getHtmlString(array $attributes, string $key): HtmlString + protected function getHtmlString(array $attributes, string $key): HtmlString { return new HtmlString($this->getBlade($attributes, $key)); } - - - } diff --git a/tests/Http/Livewire/TestComponent.php b/tests/Http/Livewire/TestComponent.php index ec715d8d1..7fa31a7ba 100644 --- a/tests/Http/Livewire/TestComponent.php +++ b/tests/Http/Livewire/TestComponent.php @@ -1,27 +1,27 @@ -testItem = $age * 110; - } - - /** - * Get the view / contents that represent the component. - */ - public function render(): View|Closure|string - { - return \Illuminate\Support\Facades\Blade::render( - '
'.($this->testItem ?? 'Unknown').'
'); - - } -} +testItem = $age * 110; + } + + /** + * Get the view / contents that represent the component. + */ + public function render(): View|Closure|string + { + return \Illuminate\Support\Facades\Blade::render( + '
'.($this->testItem ?? 'Unknown').'
'); + + } +} diff --git a/tests/Unit/Views/Columns/LivewireComponentColumnTest.php b/tests/Unit/Views/Columns/LivewireComponentColumnTest.php index 75e1b6a35..d6d4ac978 100644 --- a/tests/Unit/Views/Columns/LivewireComponentColumnTest.php +++ b/tests/Unit/Views/Columns/LivewireComponentColumnTest.php @@ -2,12 +2,12 @@ namespace Rappasoft\LaravelLivewireTables\Tests\Unit\Views\Columns; +use Illuminate\Database\Eloquent\Model; use PHPUnit\Framework\Attributes\Group; use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; use Rappasoft\LaravelLivewireTables\Tests\Models\Pet; use Rappasoft\LaravelLivewireTables\Views\Column; use Rappasoft\LaravelLivewireTables\Views\Columns\LivewireComponentColumn; -use Illuminate\Database\Eloquent\Model; #[Group('Columns')] final class LivewireComponentColumnTest extends ColumnTestCase @@ -97,9 +97,8 @@ public static function setup_with_public_methods() $row = Pet::find(1); - $temp = (new class("name", "name") extends LivewireComponentColumn + $temp = (new class('name', 'name') extends LivewireComponentColumn { - public function pubRetrieveAttributes(Model $row) { return $this->retrieveAttributes($row); @@ -112,17 +111,16 @@ public function pubImplodeAttributes(array $attributes) public function pubGetBlade(array $attributes, string $key) { - return $this->getBlade($attributes,$key); + return $this->getBlade($attributes, $key); } public function pubGetHtmlString(array $attributes, string $key) { - return $this->getHtmlString($attributes,$key); + return $this->getHtmlString($attributes, $key); } - })->component('test-component')->attributes(function ($columnValue, $row) { return [ - 'type' => "test", + 'type' => 'test', 'name' => $row->name, ]; }); @@ -132,12 +130,11 @@ public function pubGetHtmlString(array $attributes, string $key) return $temp; } - public function test_can_get_attributes_correctly(): void { $row = Pet::find(1); $temp = self::setup_with_public_methods(); - $key = "test-table-".$row->{$row->getKeyName()}; + $key = 'test-table-'.$row->{$row->getKeyName()}; $this->assertSame(['type' => 'test', 'name' => 'Cartman'], $temp->pubRetrieveAttributes($row)); @@ -148,21 +145,19 @@ public function test_can_get_blade_correctly(): void { $row = Pet::find(1); $temp = self::setup_with_public_methods(); - $key = "test-table-".$row->{$row->getKeyName()}; + $key = 'test-table-'.$row->{$row->getKeyName()}; + + $this->assertStringContainsString('wire:snapshot="{"data":{"id":null,"name":"Cartman","value":null,"type":"test"}', $temp->pubGetBlade($temp->pubRetrieveAttributes($row), $key)); - $this->assertStringContainsString('wire:snapshot="{"data":{"id":null,"name":"Cartman","value":null,"type":"test"}', $temp->pubGetBlade($temp->pubRetrieveAttributes($row),$key)); - - $this->assertStringContainsString('
Name:Cartman
Type:test
', $temp->pubGetBlade($temp->pubRetrieveAttributes($row),$key)); + $this->assertStringContainsString('
Name:Cartman
Type:test
', $temp->pubGetBlade($temp->pubRetrieveAttributes($row), $key)); } public function test_can_get_html_string_correctly(): void { $row = Pet::find(1); $temp = self::setup_with_public_methods(); - $key = "test-table-".$row->{$row->getKeyName()}; + $key = 'test-table-'.$row->{$row->getKeyName()}; - $this->assertStringContainsString('
Name:Cartman
Type:test
', $temp->pubGetHtmlString($temp->pubRetrieveAttributes($row),$key)); + $this->assertStringContainsString('
Name:Cartman
Type:test
', $temp->pubGetHtmlString($temp->pubRetrieveAttributes($row), $key)); } - } - From 8042c7d9a2d6453c400ed9f69e5c488ddad6d79b Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Tue, 22 Apr 2025 02:54:14 +0100 Subject: [PATCH 03/10] Initial Commit --- src/Views/Columns/LivewireComponentColumn.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Views/Columns/LivewireComponentColumn.php b/src/Views/Columns/LivewireComponentColumn.php index 4111a6728..1cf0b7e0d 100644 --- a/src/Views/Columns/LivewireComponentColumn.php +++ b/src/Views/Columns/LivewireComponentColumn.php @@ -17,7 +17,7 @@ class LivewireComponentColumn extends Column LivewireComponentColumnHelpers; /** - * The Livewire Component assigned to this Column + * The Livewire Component assigned to this Column */ protected ?string $livewireComponent; From 4d529086ed1a0acf3e2a2a65eb3a7c958bce9356 Mon Sep 17 00:00:00 2001 From: lrljoe <104938042+lrljoe@users.noreply.github.com> Date: Tue, 22 Apr 2025 01:54:40 +0000 Subject: [PATCH 04/10] Fix styling --- src/Views/Columns/LivewireComponentColumn.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Views/Columns/LivewireComponentColumn.php b/src/Views/Columns/LivewireComponentColumn.php index 1cf0b7e0d..4111a6728 100644 --- a/src/Views/Columns/LivewireComponentColumn.php +++ b/src/Views/Columns/LivewireComponentColumn.php @@ -17,7 +17,7 @@ class LivewireComponentColumn extends Column LivewireComponentColumnHelpers; /** - * The Livewire Component assigned to this Column + * The Livewire Component assigned to this Column */ protected ?string $livewireComponent; From 8cb5080c7b67a689056efc327631d155e1e60bdf Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Tue, 22 Apr 2025 02:58:21 +0100 Subject: [PATCH 05/10] Adjust test --- .../Livewire/TestLivewireColumnComponent.php | 25 +++++++++++++++++++ .../Columns/LivewireComponentColumnTest.php | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 tests/Http/Livewire/TestLivewireColumnComponent.php diff --git a/tests/Http/Livewire/TestLivewireColumnComponent.php b/tests/Http/Livewire/TestLivewireColumnComponent.php new file mode 100644 index 000000000..f1d857a28 --- /dev/null +++ b/tests/Http/Livewire/TestLivewireColumnComponent.php @@ -0,0 +1,25 @@ +'. + '
Name:'.($this->name ?? 'Unknown').'
'. + '
Type:'.($this->type ?? 'Unknown').'
'. + '' + ); + + } +} diff --git a/tests/Unit/Views/Columns/LivewireComponentColumnTest.php b/tests/Unit/Views/Columns/LivewireComponentColumnTest.php index d6d4ac978..d1549f54b 100644 --- a/tests/Unit/Views/Columns/LivewireComponentColumnTest.php +++ b/tests/Unit/Views/Columns/LivewireComponentColumnTest.php @@ -93,7 +93,7 @@ public function test_can_set_attribute_callback(): void public static function setup_with_public_methods() { - \Livewire\Livewire::component('test-component', \Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\TestComponent::class); + \Livewire\Livewire::component('test-livewire-column-component', \Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\TestLivewireColumnComponent::class); $row = Pet::find(1); From 145c44caa60b8b5ea845dd324bf3a938b6a2e6c1 Mon Sep 17 00:00:00 2001 From: lrljoe <104938042+lrljoe@users.noreply.github.com> Date: Tue, 22 Apr 2025 01:58:43 +0000 Subject: [PATCH 06/10] Fix styling --- tests/Http/Livewire/TestLivewireColumnComponent.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Http/Livewire/TestLivewireColumnComponent.php b/tests/Http/Livewire/TestLivewireColumnComponent.php index f1d857a28..e154fb730 100644 --- a/tests/Http/Livewire/TestLivewireColumnComponent.php +++ b/tests/Http/Livewire/TestLivewireColumnComponent.php @@ -5,8 +5,11 @@ class TestLivewireColumnComponent extends \Livewire\Component { public string $id; + public string $name; + public string $value; + public string $type; /** From b7ca6636e27a83a60998b1028189baecb79a5ae0 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Tue, 22 Apr 2025 02:59:34 +0100 Subject: [PATCH 07/10] Use test-livewire-column-component --- tests/Unit/Views/Columns/LivewireComponentColumnTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Views/Columns/LivewireComponentColumnTest.php b/tests/Unit/Views/Columns/LivewireComponentColumnTest.php index d1549f54b..ca7b6acd7 100644 --- a/tests/Unit/Views/Columns/LivewireComponentColumnTest.php +++ b/tests/Unit/Views/Columns/LivewireComponentColumnTest.php @@ -118,7 +118,7 @@ public function pubGetHtmlString(array $attributes, string $key) { return $this->getHtmlString($attributes, $key); } - })->component('test-component')->attributes(function ($columnValue, $row) { + })->component('test-livewire-column-component')->attributes(function ($columnValue, $row) { return [ 'type' => 'test', 'name' => $row->name, From b8a86f88ffd3bbb02f7f72124a1f69a32b500c9c Mon Sep 17 00:00:00 2001 From: Joe McElwee Date: Tue, 22 Apr 2025 03:39:43 +0100 Subject: [PATCH 08/10] Additional Tests for LivewireComponentColumn --- .../Livewire/PetsTableWithLivewireColumn.php | 41 ++++++++++++ tests/TestServiceProvider.php | 3 + .../LivewireComponentColumnVisualsTest.php | 66 +++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 tests/Http/Livewire/PetsTableWithLivewireColumn.php create mode 100644 tests/Visuals/Columns/LivewireComponentColumnVisualsTest.php diff --git a/tests/Http/Livewire/PetsTableWithLivewireColumn.php b/tests/Http/Livewire/PetsTableWithLivewireColumn.php new file mode 100644 index 000000000..9fc2192df --- /dev/null +++ b/tests/Http/Livewire/PetsTableWithLivewireColumn.php @@ -0,0 +1,41 @@ +setPrimaryKey('id'); + } + + public function columns(): array + { + return [ + Column::make('ID', 'id') + ->sortable(), + Column::make('Name', 'name') + ->sortable(), + LivewireComponentColumn::make("LW","name") + ->component('test-livewire-column-component')->attributes(function ($columnValue, $row) { + return [ + 'type' => 'test', + 'name' => $row->name, + ]; + }), + ]; + } +} diff --git a/tests/TestServiceProvider.php b/tests/TestServiceProvider.php index d3714c914..2e9753c25 100644 --- a/tests/TestServiceProvider.php +++ b/tests/TestServiceProvider.php @@ -11,6 +11,9 @@ class TestServiceProvider extends ServiceProvider public function boot(): void { Blade::component('test-component', TestComponent::class); + + \Livewire\Livewire::component('test-livewire-column-component', \Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\TestLivewireColumnComponent::class); + $this->loadViewsFrom(__DIR__.'/views', 'livewire-tables-test'); } diff --git a/tests/Visuals/Columns/LivewireComponentColumnVisualsTest.php b/tests/Visuals/Columns/LivewireComponentColumnVisualsTest.php new file mode 100644 index 000000000..ce3b3060f --- /dev/null +++ b/tests/Visuals/Columns/LivewireComponentColumnVisualsTest.php @@ -0,0 +1,66 @@ +assertSeeHtmlInOrder([ + '
Name:Ben
Type:test
', + '
Name:Cartman
Type:test
', + ]); + + } + + public function test_icon_column_renders_correctly_with_asc_sort(): void + { + $temp = new class extends PetsTableWithLivewireColumn + { + public function configure(): void + { + parent::configure(); + + $this->setDefaultSort('name', 'asc'); + + } + }; + Livewire::test($temp) + ->assertSeeHtmlInOrder([ + '
Name:Ben
Type:test
', + '
Name:Cartman
Type:test
', + ]); + } + + public function test_icon_column_renders_correctly_with_desc_sort(): void + { + $temp = new class extends PetsTableWithLivewireColumn + { + public function configure(): void + { + parent::configure(); + + $this->setDefaultSort('name', 'desc'); + + } + }; + Livewire::test($temp) + ->assertSeeHtmlInOrder([ + '
Name:Cartman
Type:test
', + '
Name:Ben
Type:test
', + ]); + } +} From 1a2c9cf72f417dcd2b59bc0755782ce89ecf12ef Mon Sep 17 00:00:00 2001 From: lrljoe <104938042+lrljoe@users.noreply.github.com> Date: Tue, 22 Apr 2025 02:40:08 +0000 Subject: [PATCH 09/10] Fix styling --- .../Livewire/PetsTableWithLivewireColumn.php | 15 ++++++------ .../LivewireComponentColumnVisualsTest.php | 24 +++++++++---------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/tests/Http/Livewire/PetsTableWithLivewireColumn.php b/tests/Http/Livewire/PetsTableWithLivewireColumn.php index 9fc2192df..5c0ba4d43 100644 --- a/tests/Http/Livewire/PetsTableWithLivewireColumn.php +++ b/tests/Http/Livewire/PetsTableWithLivewireColumn.php @@ -7,7 +7,6 @@ use Rappasoft\LaravelLivewireTables\Views\Column; use Rappasoft\LaravelLivewireTables\Views\Columns\LivewireComponentColumn; - class PetsTableWithLivewireColumn extends BaseTable { public $model = Pet::class; @@ -29,13 +28,13 @@ public function columns(): array ->sortable(), Column::make('Name', 'name') ->sortable(), - LivewireComponentColumn::make("LW","name") - ->component('test-livewire-column-component')->attributes(function ($columnValue, $row) { - return [ - 'type' => 'test', - 'name' => $row->name, - ]; - }), + LivewireComponentColumn::make('LW', 'name') + ->component('test-livewire-column-component')->attributes(function ($columnValue, $row) { + return [ + 'type' => 'test', + 'name' => $row->name, + ]; + }), ]; } } diff --git a/tests/Visuals/Columns/LivewireComponentColumnVisualsTest.php b/tests/Visuals/Columns/LivewireComponentColumnVisualsTest.php index ce3b3060f..28868f8a4 100644 --- a/tests/Visuals/Columns/LivewireComponentColumnVisualsTest.php +++ b/tests/Visuals/Columns/LivewireComponentColumnVisualsTest.php @@ -19,10 +19,10 @@ final class LivewireComponentColumnVisualsTest extends TestCase public function test_icon_column_renders_correctly(): void { Livewire::test(PetsTableWithLivewireColumn::class) - ->assertSeeHtmlInOrder([ - '
Name:Ben
Type:test
', - '
Name:Cartman
Type:test
', - ]); + ->assertSeeHtmlInOrder([ + '
Name:Ben
Type:test
', + '
Name:Cartman
Type:test
', + ]); } @@ -39,10 +39,10 @@ public function configure(): void } }; Livewire::test($temp) - ->assertSeeHtmlInOrder([ - '
Name:Ben
Type:test
', - '
Name:Cartman
Type:test
', - ]); + ->assertSeeHtmlInOrder([ + '
Name:Ben
Type:test
', + '
Name:Cartman
Type:test
', + ]); } public function test_icon_column_renders_correctly_with_desc_sort(): void @@ -58,9 +58,9 @@ public function configure(): void } }; Livewire::test($temp) - ->assertSeeHtmlInOrder([ - '
Name:Cartman
Type:test
', - '
Name:Ben
Type:test
', - ]); + ->assertSeeHtmlInOrder([ + '
Name:Cartman
Type:test
', + '
Name:Ben
Type:test
', + ]); } } From 9ab0b113e5b50dcc032d23affefeecc3f51b9e84 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Tue, 22 Apr 2025 03:47:06 +0100 Subject: [PATCH 10/10] Add getContents Test --- tests/Unit/Views/Columns/LivewireComponentColumnTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/Unit/Views/Columns/LivewireComponentColumnTest.php b/tests/Unit/Views/Columns/LivewireComponentColumnTest.php index ca7b6acd7..4c8f48929 100644 --- a/tests/Unit/Views/Columns/LivewireComponentColumnTest.php +++ b/tests/Unit/Views/Columns/LivewireComponentColumnTest.php @@ -160,4 +160,13 @@ public function test_can_get_html_string_correctly(): void $this->assertStringContainsString('
Name:Cartman
Type:test
', $temp->pubGetHtmlString($temp->pubRetrieveAttributes($row), $key)); } + + public function test_can_get_contents_correctly(): void + { + $row = Pet::find(1); + $temp = self::setup_with_public_methods(); + $key = 'test-table-'.$row->{$row->getKeyName()}; + + $this->assertStringContainsString('
Name:Cartman
Type:test
', $temp->getContents($row)); + } }