From bdb8680b333acfa60aebeafdc0af35c4fb59b844 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Ko=C5=9Bmider?= <78200901+shepard153@users.noreply.github.com> Date: Fri, 17 Jan 2025 09:40:53 +0100 Subject: [PATCH] Fix `ListSynth` getter method (#2044) --- .../src/Support/Synthesizers/ListSynth.php | 5 + .../Livewire/Synthesizers/ListSynthTest.php | 97 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 tests/admin/Unit/Livewire/Synthesizers/ListSynthTest.php diff --git a/packages/admin/src/Support/Synthesizers/ListSynth.php b/packages/admin/src/Support/Synthesizers/ListSynth.php index 048d8a969c..e40eea33e9 100644 --- a/packages/admin/src/Support/Synthesizers/ListSynth.php +++ b/packages/admin/src/Support/Synthesizers/ListSynth.php @@ -15,6 +15,11 @@ public function dehydrate($target) return parent::dehydrate($target); // TODO: Change the autogenerated stub } + public function get(&$target, $key) + { + return (array) $target->getValue(); + } + public function set(&$target, $key, $value) { $fieldValue = (array) $target->getValue(); diff --git a/tests/admin/Unit/Livewire/Synthesizers/ListSynthTest.php b/tests/admin/Unit/Livewire/Synthesizers/ListSynthTest.php new file mode 100644 index 0000000000..db951a8b70 --- /dev/null +++ b/tests/admin/Unit/Livewire/Synthesizers/ListSynthTest.php @@ -0,0 +1,97 @@ +group('support.synthesizers'); + +describe('list field synthesizer', function () { + beforeEach(function () { + $this->listSynth = Mockery::mock(ListSynth::class)->makePartial(); + $this->listField = Mockery::mock(ListField::class)->makePartial(); + }); + + test('sets a value in the list field', function () { + $key = 'item1'; + $value = 'Test Value'; + + $this->listSynth->set($this->listField, $key, $value); + + $result = $this->listField->getValue(); + + expect($result)->toBeObject() + ->and($result)->toHaveKey($key, $value); + }); + + test('unsets a value from the list field', function () { + $key = 'item1'; + $value = 'Test Value'; + + $this->listField->setValue([$key => $value]); + + $this->listSynth->unset($this->listField, $key); + + $result = $this->listField->getValue(); + + expect($result)->toBeArray() + ->and($result)->not->toHaveKey($key); + }); + + test('gets values from the list field', function () { + $key = 'item1'; + $value = 'Test Value'; + $this->listField->setValue([$key => $value]); + + $result = $this->listSynth->get($this->listField, $key); + + expect($result)->toBeArray() + ->and($result)->toEqual((array) $this->listField->getValue()); + }); + + test('dehydrates the list field correctly', function () { + $this->listField->setValue(['item1' => 'Test Value']); + + $result = $this->listSynth->dehydrate($this->listField)[0]; + + expect($result)->toEqual($this->listField->getValue()); + }); + + test('handles empty keys and values', function () { + $key = ''; + $value = ''; + + $this->listSynth->set($this->listField, $key, $value); + + $result = $this->listField->getValue(); + + expect($result)->toBeObject() + ->and($result)->toHaveKey($key, $value); + + $this->listSynth->unset($this->listField, $key); + + $result = $this->listField->getValue(); + + expect($result)->toBeArray() + ->and($result)->not->toHaveKey($key); + }); + + test('handles keys and values with dot notation', function () { + $key = 'key.with.dots'; + $value = 'Dot.Notation.Value'; + + $this->listSynth->set($this->listField, $key, $value); + + $result = $this->listField->getValue(); + + expect($result)->toBeObject() + ->and($result)->toHaveKey($key, $value); + + $this->listSynth->unset($this->listField, $key); + + $result = $this->listField->getValue(); + + expect($result)->toBeArray() + ->and($result)->not->toHaveKey($key); + }); +});