Skip to content

Commit

Permalink
meta tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Feb 24, 2024
1 parent 30b7ed8 commit 3a31f57
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 121 deletions.
130 changes: 9 additions & 121 deletions src/Fields/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(string $label, Closure|string|null $modelAttribute =

parent::__construct($label, $modelAttribute, $relation);

$this->asText();
$this->as(Text::class);
}

/**
Expand All @@ -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.
*/
Expand All @@ -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}
*/
Expand Down Expand Up @@ -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}
*/
Expand Down
74 changes: 74 additions & 0 deletions tests/Fields/MetaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Cone\Root\Tests\Fields;

use Cone\Root\Fields\Meta;
use Cone\Root\Fields\Number;
use Cone\Root\Fields\Text;
use Cone\Root\Tests\TestCase;
use Cone\Root\Tests\User;

class MetaTest extends TestCase
{
protected Meta $field;

public function setUp(): void
{
parent::setUp();

$this->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)),
);
}
}

0 comments on commit 3a31f57

Please sign in to comment.