Skip to content

Commit

Permalink
fix: Prepare for release
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-to committed Aug 19, 2024
1 parent e471f60 commit 7557b41
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 60 deletions.
33 changes: 13 additions & 20 deletions src/Casts/LayoutsCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,31 @@
namespace MoonShine\Layouts\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Casts\Json;
use Illuminate\Database\Eloquent\Model;
use MoonShine\Layouts\Collections\LayoutItemCollection;
use Throwable;

class LayoutsCast implements CastsAttributes
{
/**
* Cast the given value.
*
* @param array<string, mixed> $attributes
*/
public function get(Model $model, string $key, mixed $value, array $attributes): LayoutItemCollection
public function get(Model $model, string $key, mixed $value, array $attributes): ?LayoutItemCollection
{
if($value instanceof LayoutItemCollection) {
return $value;
if (! isset($attributes[$key])) {
return null;
}

return $this->_map($value);
}
$data = Json::decode($attributes[$key]);

/**
* Prepare the given value for storage.
*
* @param array<string, mixed> $attributes
*/
public function set(Model $model, string $key, mixed $value, array $attributes): LayoutItemCollection
{
if(! $value instanceof LayoutItemCollection) {
return $this->_map($value);
if($data instanceof LayoutItemCollection) {
return $data;
}

return $value;
return is_array($data) ? $this->_map($data) : null;
}

public function set(Model $model, string $key, mixed $value, array $attributes): array
{
return [$key => Json::encode($value)];
}

private function _map(mixed $value): LayoutItemCollection
Expand Down
121 changes: 83 additions & 38 deletions tests/Feature/LayoutsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,83 +5,128 @@
namespace MoonShine\Layouts\Tests\Feature;

use Illuminate\Http\UploadedFile;
use MoonShine\Layouts\Casts\LayoutItem;
use MoonShine\Layouts\Tests\Fixtures\TestModel;
use MoonShine\Layouts\Tests\TestCase;
use PHPUnit\Framework\Attributes\Test;

final class LayoutsTest extends TestCase
{
#[Test]
public function it_successful_save(): void
private function store(TestModel $model, array $data = []): void
{
$model = TestModel::query()->create();

$this->actingAs($this->adminUser, 'moonshine')
->put($this->resource->route('crud.update', $model))
->put($this->resource->route('crud.update', $model), $data)
->assertRedirect();

$model->refresh();
}

#[Test]
public function it_json_with_image(): void
{
$model = TestModel::query()->create();

$image = UploadedFile::fake()->image('image.jpg');

// simple store
$data = [
'data' => [
['_layout' => 'first', 'title' => 'First title', 'image' => $image, 'json' => [
['key' => 'key 1', 'value' => 'value 1'],
['key' => 'key 2', 'value' => 'value 2'],
]],
['_layout' => 'second', 'title' => 'Second title', 'images' => [$image], 'json' => [
['title' => 'Title 1', 'image' => $image],
]],
],
];

$this->store($model, $data);

$first = static fn(TestModel $model): array => $model->data->findByName('first')->get('json');
$second = static fn(TestModel $model): array => $model->data->findByName('second')->get('json');

$this->assertEquals(['key 1' => 'value 1', 'key 2' => 'value 2'], $first($model));
$this->assertEquals([['title' => 'Title 1', 'image' => $image->hashName()]], $second($model));

// stay images
$data = [
'data' => [
['_layout' => 'first', 'title' => 'First title'],
['_layout' => 'second', 'title' => 'Second title', 'json' => [
['title' => 'Title 1', 'hidden_image' => $image->hashName()],
]],
],
];

$this->store($model, $data);

$this->assertEquals([['title' => 'Title 1', 'image' => $image->hashName()]], $second($model));

// remove images
$data = [
'id' => $model->id,
'data' => [
['_layout' => 'first', 'title' => 'First title'],
['_layout' => 'second', 'title' => 'Second title', 'json' => [
['title' => 'Title 1'],
]],
],
];

$this->store($model, $data);

$this->assertEquals([['title' => 'Title 1', 'image' => null]], $second($model));
}

#[Test]
public function it_with_image(): void
{
$model = TestModel::query()->create();
$image = UploadedFile::fake()->image('image.jpg');

// simple store
$data = [
'data' => [
['_layout' => 'first', 'title' => 'First title', 'image' => $image],
['_layout' => 'second', 'title' => 'Second title', 'images' => [$image]],
],
];

$this->actingAs($this->adminUser, 'moonshine')
->put($this->resource->route('crud.update', $model), $data)
->assertRedirect();

$model->refresh();
$this->store($model, $data);

$first = $model->data->findByName('first');
$second = $model->data->findByName('second');
$first = static fn(TestModel $model): LayoutItem => $model->data->findByName('first');
$second = static fn(TestModel $model): LayoutItem => $model->data->findByName('second');

$this->assertEquals('First title', $first->get('title'));
$this->assertEquals('Second title', $second->get('title'));
$this->assertEquals($image->hashName(), $first->get('image'));
$this->assertEquals([$image->hashName()], $second->get('images'));
$this->assertEquals('First title', $first($model)->get('title'));
$this->assertEquals('Second title', $second($model)->get('title'));
$this->assertEquals($image->hashName(), $first($model)->get('image'));
$this->assertEquals([$image->hashName()], $second($model)->get('images'));

// stay images
$data = [
'id' => $model->id,
'data' => [
['_layout' => 'first', 'title' => 'First title', 'hidden_image' => $image->hashName()],
['_layout' => 'second', 'title' => 'Second title', 'hidden_images' => [$image->hashName()]],
],
];

$this->actingAs($this->adminUser, 'moonshine')
->put($this->resource->route('crud.update', $model), $data)
->assertRedirect();

$model->refresh();

$first = $model->data->findByName('first');
$second = $model->data->findByName('second');
$this->store($model, $data);

$this->assertEquals($image->hashName(), $first->get('image'));
$this->assertEquals([$image->hashName()], $second->get('images'));
$this->assertEquals($image->hashName(), $first($model)->get('image'));
$this->assertEquals([$image->hashName()], $second($model)->get('images'));

// remove images
$data = [
'id' => $model->id,
'data' => [
['_layout' => 'first', 'title' => 'First title'],
['_layout' => 'second', 'title' => 'Second title'],
],
];

$this->actingAs($this->adminUser, 'moonshine')
->put($this->resource->route('crud.update', $model), $data)
->assertRedirect();

$model->refresh();

$first = $model->data->findByName('first');
$second = $model->data->findByName('second');
$this->store($model, $data);

$this->assertEquals(null, $first->get('image'));
$this->assertEquals([], $second->get('images'));
$this->assertEquals(null, $first($model)->get('image'));
$this->assertEquals([], $second($model)->get('images'));
}
}
18 changes: 16 additions & 2 deletions tests/Fixtures/TestResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
namespace MoonShine\Layouts\Tests\Fixtures;

use Illuminate\Database\Eloquent\Model;
use MoonShine\Decorations\Column;
use MoonShine\Decorations\Grid;
use MoonShine\Fields\ID;
use MoonShine\Fields\Image;
use MoonShine\Fields\Json;
use MoonShine\Fields\Text;
use MoonShine\Layouts\Fields\Layouts;
use MoonShine\Resources\ModelResource;
Expand All @@ -20,11 +23,22 @@ public function fields(): array
return [
ID::make(),
Layouts::make('Data')->addLayout('first', 'first', [
Text::make('Title'),
Image::make('Image')->removable(),
Grid::make([
Column::make([
Text::make('Title'),
]),
Column::make([
Image::make('Image')->removable(),
])
]),
Json::make('Json')->keyValue(),
])->addLayout('second', 'second', [
Text::make('Title'),
Image::make('Images')->multiple()->removable(),
Json::make('Json')->fields([
Text::make('Title'),
Image::make('Image')->removable(),
]),
]),
];
}
Expand Down

0 comments on commit 7557b41

Please sign in to comment.