From 3a31f57e06ae7ccc3eef6c8bb2aaebcb77a55698 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Sat, 24 Feb 2024 17:00:16 +0100 Subject: [PATCH] meta tests --- src/Fields/Meta.php | 130 +++----------------------------------- tests/Fields/MetaTest.php | 74 ++++++++++++++++++++++ 2 files changed, 83 insertions(+), 121 deletions(-) create mode 100644 tests/Fields/MetaTest.php diff --git a/src/Fields/Meta.php b/src/Fields/Meta.php index 405d2a7d..1d73e1f3 100644 --- a/src/Fields/Meta.php +++ b/src/Fields/Meta.php @@ -39,7 +39,7 @@ public function __construct(string $label, Closure|string|null $modelAttribute = parent::__construct($label, $modelAttribute, $relation); - $this->asText(); + $this->as(Text::class); } /** @@ -52,6 +52,14 @@ public function getRelationName(): string : $this->relation; } + /** + * Get the field instance. + */ + public function getField(): Field + { + return $this->field; + } + /** * Set the field class. */ @@ -70,118 +78,6 @@ public function as(string $field, ?Closure $callback = null): static return $this; } - /** - * Set the meta field as boolean. - */ - public function asBoolean(?Closure $callback = null): static - { - return $this->as(Boolean::class, $callback); - } - - /** - * Set the meta field as checkbox. - */ - public function asCheckbox(?Closure $callback = null): static - { - return $this->as(Checkbox::class, $callback); - } - - /** - * Set the meta field as color. - */ - public function asColor(?Closure $callback = null): static - { - return $this->as(Color::class, $callback); - } - - /** - * Set the meta field as date. - */ - public function asDate(?Closure $callback = null): static - { - return $this->as(Date::class, $callback); - } - - /** - * Set the meta field as editor. - */ - public function asEditor(?Closure $callback = null): static - { - return $this->as(Editor::class, $callback); - } - - /** - * Set the meta field as hidden. - */ - public function asHidden(?Closure $callback = null): static - { - return $this->as(Hidden::class, $callback); - } - - /** - * Set the meta field as number. - */ - public function asNumber(?Closure $callback = null): static - { - return $this->as(Number::class, $callback); - } - - /** - * Set the meta field as radio. - */ - public function asRadio(?Closure $callback = null): static - { - return $this->as(Radio::class, $callback); - } - - /** - * Set the meta field as range. - */ - public function asRange(?Closure $callback = null): static - { - return $this->as(Range::class, $callback); - } - - /** - * Set the meta field as select. - */ - public function asSelect(?Closure $callback = null): static - { - return $this->as(Select::class, $callback); - } - - /** - * Set the meta field as tag. - */ - public function asTag(?Closure $callback = null): static - { - return $this->as(Tag::class, $callback); - } - - /** - * Set the meta field as text. - */ - public function asText(?Closure $callback = null): static - { - return $this->as(Text::class, $callback); - } - - /** - * Set the meta field as textarea. - */ - public function asTextarea(?Closure $callback = null): static - { - return $this->as(Textarea::class, $callback); - } - - /** - * Set the meta field as URL. - */ - public function asUrl(?Closure $callback = null): static - { - return $this->as(URL::class, $callback); - } - /** * {@inheritdoc} */ @@ -245,14 +141,6 @@ public function resolveHydrate(Request $request, Model $model, mixed $value): vo parent::resolveHydrate($request, $model, $value); } - /** - * {@inheritdoc} - */ - public function toArray(): array - { - return $this->field->toArray(); - } - /** * {@inheritdoc} */ diff --git a/tests/Fields/MetaTest.php b/tests/Fields/MetaTest.php new file mode 100644 index 00000000..5defe2a9 --- /dev/null +++ b/tests/Fields/MetaTest.php @@ -0,0 +1,74 @@ +field = Meta::make('Price'); + } + + public function test_a_meta_field_has_relation_name(): void + { + $this->assertSame('__root_price', $this->field->getRelationName()); + } + + public function test_a_meta_filed_has_different_types(): void + { + $this->assertInstanceOf(Text::class, $this->field->getField()); + + $this->field->as(Number::class); + $this->assertInstanceOf(Number::class, $this->field->getField()); + } + + public function test_a_meta_field_has_options(): void + { + $model = new User(); + + $this->assertSame( + [], $this->field->resolveOptions($this->app['request'], $model) + ); + } + + public function test_a_meta_field_hydates_model(): void + { + $model = new User(); + + $this->assertFalse($model->relationLoaded('__root_price')); + + $this->field->resolveHydrate($this->app['request'], $model, 100); + + $this->assertSame(100, $model->getRelation('__root_price')->value); + } + + public function test_a_meta_field_has_display_representation(): void + { + $model = new User(); + + $this->assertSame( + $this->field->getField()->toDisplay($this->app['request'], $model), + $this->field->toDisplay($this->app['request'], $model), + ); + } + + public function test_a_meta_field_has_input_representation(): void + { + $model = new User(); + + $this->assertSame( + json_encode($this->field->getField()->toInput($this->app['request'], $model)), + json_encode($this->field->toInput($this->app['request'], $model)), + ); + } +}