Skip to content

Commit

Permalink
Fix ListSynth getter method (#2044)
Browse files Browse the repository at this point in the history
  • Loading branch information
shepard153 authored Jan 17, 2025
1 parent d2ad394 commit bdb8680
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/admin/src/Support/Synthesizers/ListSynth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
97 changes: 97 additions & 0 deletions tests/admin/Unit/Livewire/Synthesizers/ListSynthTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

use Lunar\Admin\Support\Synthesizers\ListSynth;
use Lunar\FieldTypes\ListField;

uses(\Lunar\Tests\Admin\Unit\Livewire\TestCase::class)
->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);
});
});

0 comments on commit bdb8680

Please sign in to comment.