diff --git a/src/Contracts/Models/Nodes/NodeContract.php b/src/Contracts/Models/Nodes/NodeContract.php index 207edb8..38c2c05 100644 --- a/src/Contracts/Models/Nodes/NodeContract.php +++ b/src/Contracts/Models/Nodes/NodeContract.php @@ -12,4 +12,12 @@ interface NodeContract extends ModelContract * @return \Illuminate\Database\Eloquent\Relations\MorphTo */ public function parent(); + + /** + * Set the node parent relation. + * + * @param mixed $parent + * @return void + */ + public function setParentRelation($parent): void; } diff --git a/src/Contracts/Repositories/NodeRepositoryContract.php b/src/Contracts/Repositories/NodeRepositoryContract.php index 5311fe2..910ec15 100644 --- a/src/Contracts/Repositories/NodeRepositoryContract.php +++ b/src/Contracts/Repositories/NodeRepositoryContract.php @@ -17,16 +17,6 @@ interface NodeRepositoryContract */ public function all(Model $parent, ?string $type = null): LazyCollection; - /** - * Add a node to the parent model. - * - * @param \Belvedere\FormMaker\Models\Model $parent - * @param string $type - * @param array $attributes - * @return \Belvedere\FormMaker\Models\Nodes\Node - */ - public function create(Model $parent, string $type, array $attributes = []): Node; - /** * Delete all nodes of the parent model. * @@ -46,11 +36,11 @@ public function delete(Model $parent); public function find(Model $parent, $nodeKey, array $columns): ?Node; /** - * Get the first node in list. + * Get a new instance of a node model. * * @param \Belvedere\FormMaker\Models\Model $parent - * @param string|null $type - * @return \Belvedere\FormMaker\Models\Nodes\Node|null + * @param string $type + * @return \Belvedere\FormMaker\Models\Nodes\Node */ - public function first(Model $parent, ?string $type = null): ?Node; + public function getInstanceOf(Model $parent, string $type): Node; } diff --git a/src/Contracts/Traits/Nodes/HasLabelContract.php b/src/Contracts/Traits/Nodes/HasLabelContract.php index 8b41396..53bc3e1 100644 --- a/src/Contracts/Traits/Nodes/HasLabelContract.php +++ b/src/Contracts/Traits/Nodes/HasLabelContract.php @@ -2,6 +2,7 @@ namespace Belvedere\FormMaker\Contracts\Traits\Nodes; +use Illuminate\Database\Eloquent\Relations\MorphOne; use Belvedere\FormMaker\Contracts\Traits\Rankings\HasRankingsContract; use Belvedere\FormMaker\Contracts\Models\Nodes\Siblings\Label\LabelerContract; @@ -16,9 +17,9 @@ interface HasLabelContract extends HasRankingsContract public function addLabel(string $text): LabelerContract; /** - * Get the node label. + * Get the label. * - * @return \Belvedere\FormMaker\Contracts\Models\Nodes\Siblings\Label\LabelerContract|null + * @return \Illuminate\Database\Eloquent\Relations\MorphOne */ - public function label(): ?LabelerContract; + public function label(): MorphOne; } diff --git a/src/Contracts/Traits/Nodes/HasOptionsContract.php b/src/Contracts/Traits/Nodes/HasOptionsContract.php index 90dde54..4e4c91d 100644 --- a/src/Contracts/Traits/Nodes/HasOptionsContract.php +++ b/src/Contracts/Traits/Nodes/HasOptionsContract.php @@ -9,15 +9,14 @@ interface HasOptionsContract extends HasRankingsContract { /** - * Add an option for the input. + * Add an option input to the parent model. * - * @param array $attributes * @return \Belvedere\FormMaker\Contracts\Models\Nodes\Inputs\Option\OptionerContract */ - public function addOption(array $attributes): OptionerContract; + public function addOption(): OptionerContract; /** - * Add options for the input. + * Add options to the parent model. * * @param array ...$options * @return array diff --git a/src/Contracts/Traits/Rankings/HasRankingsContract.php b/src/Contracts/Traits/Rankings/HasRankingsContract.php index 3a21496..aa93a69 100644 --- a/src/Contracts/Traits/Rankings/HasRankingsContract.php +++ b/src/Contracts/Traits/Rankings/HasRankingsContract.php @@ -2,10 +2,20 @@ namespace Belvedere\FormMaker\Contracts\Traits\Rankings; +use Belvedere\FormMaker\Models\Nodes\Node; use Illuminate\Database\Eloquent\Relations\MorphOne; interface HasRankingsContract { + /** + * Add a node in the ranking. + * + * @param \Belvedere\FormMaker\Models\Nodes\Node $node + * @return void + * @throws \Exception + */ + public function addInRanking(Node $node): void; + /** * Get the model rankings. * diff --git a/src/Http/Resources/Nodes/Inputs/Datalist/DatalistResourcer.php b/src/Http/Resources/Nodes/Inputs/Datalist/DatalistResourcer.php index f24961b..5b7602d 100644 --- a/src/Http/Resources/Nodes/Inputs/Datalist/DatalistResourcer.php +++ b/src/Http/Resources/Nodes/Inputs/Datalist/DatalistResourcer.php @@ -55,12 +55,10 @@ public function toArray($request): array */ protected function getLabelResource(string $inputId): ?LabelResourcerContract { - $label = $this->label(); - - if ($label) { - return $label->withHtmlAttributes(['for' => $inputId])->toApi(); + if ($this->label) { + return $this->label->withHtmlAttributes(['for' => $inputId])->toApi(); } - return $label; + return $this->label; } } diff --git a/src/Http/Resources/Nodes/Inputs/InputResourcer.php b/src/Http/Resources/Nodes/Inputs/InputResourcer.php index 962e736..b1a1c2b 100644 --- a/src/Http/Resources/Nodes/Inputs/InputResourcer.php +++ b/src/Http/Resources/Nodes/Inputs/InputResourcer.php @@ -50,12 +50,12 @@ public function toArray($request): array */ protected function getLabelResource(): ?LabelResourcerContract { - $label = $this->label(); + if ($this->label) { + $this->label->setParentRelation($this->resource); - if ($label) { - return $label->toApi(); + return $this->label->toApi(); } - return $label; + return $this->label; } } diff --git a/src/Listeners/AddNodeInRanking.php b/src/Listeners/AddNodeInRanking.php new file mode 100644 index 0000000..fe2f464 --- /dev/null +++ b/src/Listeners/AddNodeInRanking.php @@ -0,0 +1,43 @@ +node = $node; + + $this->handle(); + } + + /** + * Handle the event. + * + * @return void + */ + protected function handle(): void + { + if ($this->node->type === 'label' && $this->node->getRelation('parent') instanceof InputContract) { + return; + } + + $this->node->getRelation('parent')->addInRanking($this->node); + } +} diff --git a/src/Models/Nodes/Node.php b/src/Models/Nodes/Node.php index 1d5e61b..20e12f2 100644 --- a/src/Models/Nodes/Node.php +++ b/src/Models/Nodes/Node.php @@ -3,6 +3,7 @@ namespace Belvedere\FormMaker\Models\Nodes; use Belvedere\FormMaker\Models\Model; +use Belvedere\FormMaker\Listeners\AddNodeInRanking; use Illuminate\Database\Eloquent\Relations\MorphTo; use Belvedere\FormMaker\Listeners\RemoveFromRanking; use Belvedere\FormMaker\Contracts\Models\Nodes\NodeContract; @@ -15,6 +16,7 @@ class Node extends Model implements NodeContract * @var array */ protected $dispatchesEvents = [ + 'created' => AddNodeInRanking::class, 'deleted' => RemoveFromRanking::class, ]; @@ -30,6 +32,17 @@ public function __construct(array $attributes = []) $this->table = config('form-maker.database.form_nodes_table', 'form_nodes'); } + /** + * Set the node parent relation. + * + * @param mixed $parent + * @return void + */ + public function setParentRelation($parent): void + { + $this->setRelation('parent', $parent); + } + // ELOQUENT RELATIONSHIPS // ============================================================== diff --git a/src/Repositories/NodeRepository.php b/src/Repositories/NodeRepository.php index 3a98922..3403814 100644 --- a/src/Repositories/NodeRepository.php +++ b/src/Repositories/NodeRepository.php @@ -2,6 +2,7 @@ namespace Belvedere\FormMaker\Repositories; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\DB; use Belvedere\FormMaker\Models\Model; use Illuminate\Support\LazyCollection; @@ -64,33 +65,10 @@ public function all(Model $parent, ?string $type = null): LazyCollection } return $query->cursor()->groupBy('type')->map(function ($nodes, $key) { - return $this->resolve($nodes[0]->type)::hydrate($nodes->toArray()); + return $this->hydrate($nodes->toArray()); })->flatten(1); } - /** - * Add a node to the parent model. - * - * @param \Belvedere\FormMaker\Models\Model $parent - * @param string $type - * @param array $attributes - * @return \Belvedere\FormMaker\Models\Nodes\Node - */ - public function create(Model $parent, string $type, array $attributes = []): Node - { - $node = $this->resolve($type); - - $node->type = $type; - - if (count($attributes) > 0) { - $node->withHtmlAttributes($attributes); - } - - $parent->morphMany($node, 'nodable')->save($node); - - return $node; - } - /** * Delete all nodes of the parent model. * @@ -133,43 +111,57 @@ public function find(Model $parent, $nodeKey, array $columns): ?Node $node = $query->first(); - return (is_null($node)) ? $node : $this->resolve($node->type)::hydrate([$node])[0]; + return (is_null($node)) ? $node : $this->hydrate([$node])[0]; } /** - * Get the first node in list. + * Get a new instance of a node model. * * @param \Belvedere\FormMaker\Models\Model $parent - * @param string|null $type - * @return \Belvedere\FormMaker\Models\Nodes\Node|null + * @param string $type + * @return \Belvedere\FormMaker\Models\Nodes\Node */ - public function first(Model $parent, ?string $type = null): ?Node + public function getInstanceOf(Model $parent, string $type): Node { - $query = DB::table(config('form-maker.database.form_nodes_table', 'form_nodes')) - ->where('nodable_type', $parent->getMorphClass()) - ->where('nodable_id', $parent->getKey()); + $node = $this->resolve($type); - if (is_string($type)) { - $query->where('type', $type); + $node->type = $type; + $node->nodable_type = $parent->getMorphClass(); + $node->nodable_id = $parent->getKey(); + $node->setParentRelation($parent); + + return $node; + } + + /** + * Create a collection of nodes from plain arrays. + * + * @param array $nodes + * @return \Illuminate\Support\Collection + */ + protected function hydrate(array $nodes): Collection + { + if (count($nodes) === 0 || is_null($nodes[0]->type)) { + return collect([]); } - $node = $query->first(); + $type = $nodes[0]->type; - return (is_null($node)) ? $node : $this->resolve($node->type)::hydrate([$node])[0]; + return $this->resolve($type)::hydrate($nodes); } /** * Resolve the node out of the service container. * - * @param string $node + * @param string $type * @return \Belvedere\FormMaker\Models\Nodes\Node */ - protected function resolve(string $node): Node + protected function resolve(string $type): Node { - if (array_key_exists($node, self::NODES)) { - return resolve(self::NODES[$node]); + if (array_key_exists($type, self::NODES)) { + return resolve(self::NODES[$type]); } - return resolve(sprintf('form-maker.%s', $node)); + return resolve(sprintf('form-maker.%s', $type)); } } diff --git a/src/Traits/Nodes/HasLabel.php b/src/Traits/Nodes/HasLabel.php index a3a3015..3c6c0b8 100644 --- a/src/Traits/Nodes/HasLabel.php +++ b/src/Traits/Nodes/HasLabel.php @@ -2,6 +2,7 @@ namespace Belvedere\FormMaker\Traits\Nodes; +use Illuminate\Database\Eloquent\Relations\MorphOne; use Belvedere\FormMaker\Contracts\Repositories\NodeRepositoryContract; use Belvedere\FormMaker\Contracts\Models\Nodes\Siblings\Label\LabelerContract; @@ -17,7 +18,7 @@ public function addLabel(string $text): LabelerContract { $nodeRepository = resolve(NodeRepositoryContract::class); - $label = $nodeRepository->create($this, 'label'); + $label = $nodeRepository->getInstanceOf($this, 'label'); $label->withText($text)->save(); @@ -25,16 +26,12 @@ public function addLabel(string $text): LabelerContract } /** - * Get the node label. + * Get the label. * - * @return \Belvedere\FormMaker\Contracts\Models\Nodes\Siblings\Label\LabelerContract|null + * @return \Illuminate\Database\Eloquent\Relations\MorphOne */ - public function label(): ?LabelerContract + public function label(): MorphOne { - $nodeRepository = resolve(NodeRepositoryContract::class); - - $label = $nodeRepository->first($this, 'label'); - - return is_null($label) ? null : $label; + return $this->morphOne(resolve(LabelerContract::class), 'nodable'); } } diff --git a/src/Traits/Nodes/HasNodes.php b/src/Traits/Nodes/HasNodes.php index d95a0f0..72587aa 100644 --- a/src/Traits/Nodes/HasNodes.php +++ b/src/Traits/Nodes/HasNodes.php @@ -19,9 +19,7 @@ public function add(string $type): Node { $nodeRepository = resolve(NodeRepositoryContract::class); - $node = $nodeRepository->create($this, $type); - - $this->addInRanking($node); + $node = $nodeRepository->getInstanceOf($this, $type); return $node; } @@ -41,6 +39,7 @@ public function addAfter($afterNodeKey, string $type): Node $afterNode = $this->node($afterNodeKey); if ($afterNode) { + $node->save(); $this->ranking->move($node)->after($afterNode); } @@ -59,6 +58,8 @@ public function addAtRank(int $rank, string $type): Node { $node = $this->add($type); + $node->save(); + $this->ranking->move($node)->toRank($rank); return $node; @@ -79,6 +80,7 @@ public function addBefore($beforeNodeKey, string $type): Node $beforeNode = $this->node($beforeNodeKey); if ($beforeNode) { + $node->save(); $this->ranking->move($node)->before($beforeNode); } diff --git a/src/Traits/Nodes/HasOptions.php b/src/Traits/Nodes/HasOptions.php index 5afae89..3e06ff5 100644 --- a/src/Traits/Nodes/HasOptions.php +++ b/src/Traits/Nodes/HasOptions.php @@ -9,41 +9,39 @@ trait HasOptions { /** - * Add a node to the parent model. + * Add an option input to the parent model. * - * @param array $attributes * @return \Belvedere\FormMaker\Contracts\Models\Nodes\Inputs\Option\OptionerContract */ - public function addOption(array $attributes): OptionerContract + public function addOption(): OptionerContract { $nodeRepository = resolve(NodeRepositoryContract::class); - $option = $nodeRepository->create($this, 'option', $attributes); + $option = $nodeRepository->getInstanceOf($this, 'option'); - if (array_key_exists('text', $attributes)) { - $option->withText($attributes['text']); - } - - $this->addInRanking($option); - - return $option->saveAndFirst(); + return $option; } /** - * Add options for the input. + * Add options to the parent model. * * @param array ...$options * @return array */ public function addOptions(array ...$options): array { - $nodes = []; - - foreach ($options as $optionAttributes) { - $nodes[] = $this->addOption($optionAttributes); - } - - return $nodes; + return array_map(function ($attributes) { + $option = $this->addOption(); + + if (count($attributes) > 0) { + $option->withHtmlAttributes($attributes); + if (array_key_exists('text', $attributes)) { + $option->withText($attributes['text']); + } + } + + return $option->saveAndFirst(); + }, $options); } /** @@ -68,9 +66,7 @@ public function option($key): ?OptionerContract */ public function options(): LazyCollection { - $nodeRepository = resolve(NodeRepositoryContract::class); - - $options = $nodeRepository->all($this, 'option'); + $options = $this->morphMany(resolve(OptionerContract::class), 'nodable')->cursor(); if ($options->isEmpty()) { return $options; diff --git a/src/Traits/Rankings/HasRankings.php b/src/Traits/Rankings/HasRankings.php index d8c1a94..779047d 100644 --- a/src/Traits/Rankings/HasRankings.php +++ b/src/Traits/Rankings/HasRankings.php @@ -22,7 +22,7 @@ trait HasRankings * @return void * @throws \Exception */ - protected function addInRanking(Node $node): void + public function addInRanking(Node $node): void { if (is_null($this->ranking)) { $this->ranking()->save(resolve(RankerContract::class)); diff --git a/tests/Unit/Models/FormTest.php b/tests/Unit/Models/FormTest.php index b41ee9c..0e4e824 100644 --- a/tests/Unit/Models/FormTest.php +++ b/tests/Unit/Models/FormTest.php @@ -69,9 +69,9 @@ public function api_response_with_html_attributes_and_no_nodes() /** @test */ public function api_response_with_no_html_attributes_and_nodes() { - $this->form->add('text'); - $this->form->add('text'); - $this->form->add('text'); + $this->form->add('text')->save(); + $this->form->add('text')->save(); + $this->form->add('text')->save(); $apiResponse = $this->form->toApi()->toArray(null); $this->assertIsArray($apiResponse); @@ -94,9 +94,9 @@ public function api_response_with_no_html_attributes_and_nodes() public function api_response_with_html_attributes_and_nodes() { $this->form->withHtmlAttributes(['accept-charset' => ['utf8'], 'role' => 'form', 'id' => 'test'])->save(); - $this->form->add('text'); - $this->form->add('text'); - $this->form->add('text'); + $this->form->add('text')->save(); + $this->form->add('text')->save(); + $this->form->add('text')->save(); $apiResponse = $this->form->toApi()->toArray(null); $this->assertIsArray($apiResponse); @@ -122,9 +122,9 @@ public function api_response_with_html_attributes_and_nodes() /** @test */ public function disable_all_inputs() { - $this->form->add('text'); - $this->form->add('text'); - $this->form->add('text'); + $this->form->add('text')->save(); + $this->form->add('text')->save(); + $this->form->add('text')->save(); $this->form->disabled(); foreach ($this->form->nodes('inputs') as $input) { @@ -136,9 +136,9 @@ public function disable_all_inputs() /** @test */ public function enable_all_inputs() { - $this->form->add('text'); - $this->form->add('text'); - $this->form->add('text'); + $this->form->add('text')->save(); + $this->form->add('text')->save(); + $this->form->add('text')->save(); $this->form->enabled(); foreach ($this->form->nodes('inputs') as $input) { @@ -149,9 +149,9 @@ public function enable_all_inputs() /** @test */ public function enable_all_inputs_after_they_have_been_disabled() { - $this->form->add('text'); - $this->form->add('text'); - $this->form->add('text'); + $this->form->add('text')->save(); + $this->form->add('text')->save(); + $this->form->add('text')->save(); $this->form->disabled(); foreach ($this->form->nodes('inputs') as $input) { diff --git a/tests/Unit/Models/InputTest.php b/tests/Unit/Models/InputTest.php index 2ebcb7d..86fa84a 100644 --- a/tests/Unit/Models/InputTest.php +++ b/tests/Unit/Models/InputTest.php @@ -33,7 +33,7 @@ public function add_input_to_form() { Event::fake(); - $this->form->add('text'); + $this->form->add('text')->save(); Event::assertDispatched(AssignAttributes::class); } @@ -43,7 +43,7 @@ public function remove_input_from_form() { Event::fake(); - $input = $this->form->add('text'); + $input = $this->form->add('text')->saveAndFirst(); $input->delete(); diff --git a/tests/Unit/Traits/HasLabelTest.php b/tests/Unit/Traits/HasLabelTest.php index f1832e5..23dd5c3 100644 --- a/tests/Unit/Traits/HasLabelTest.php +++ b/tests/Unit/Traits/HasLabelTest.php @@ -2,7 +2,7 @@ namespace Belvedere\FormMaker\Tests\Unit\Traits; -use Belvedere\FormMaker\Models\Nodes\Inputs\Text\Texter; +use Belvedere\FormMaker\Models\Form\Form; use Belvedere\FormMaker\Tests\TestCase; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -16,11 +16,10 @@ public function setUp(): void { parent::setUp(); - $this->input = Texter::forceCreate([ - 'nodable_type' => 'test', - 'nodable_id' => 1, - 'type' => 'text' - ]); + $form = new Form(); + $form->fill(['name' => 'test'])->save(); + + $this->input = $form->add('text')->saveAndFirst(); } /** @test */ @@ -36,7 +35,7 @@ public function add_label_sibling_to_a_parent_model() public function get_label_sibling_from_a_parent_model() { $this->input->addLabel('Label'); - $label = $this->input->label(); + $label = $this->input->label; $this->assertEquals('label', $label->type); $this->assertEquals('Label', $label->text); @@ -45,7 +44,7 @@ public function get_label_sibling_from_a_parent_model() /** @test */ public function get_label_sibling_from_a_parent_model_when_label_doesnt_exist() { - $label = $this->input->label(); + $label = $this->input->label; $this->assertNull($label); } diff --git a/tests/Unit/Traits/HasNodesTest.php b/tests/Unit/Traits/HasNodesTest.php index e06295c..c7c1f64 100644 --- a/tests/Unit/Traits/HasNodesTest.php +++ b/tests/Unit/Traits/HasNodesTest.php @@ -10,6 +10,8 @@ class HasNodesTest extends TestCase { use RefreshDatabase; + protected $form; + public function setUp(): void { parent::setUp(); @@ -23,7 +25,7 @@ public function setUp(): void /** @test */ public function add_node_to_a_parent_model_when_node_is_input() { - $input = $this->form->add('text'); + $input = $this->form->add('text')->saveAndFirst(); $this->assertEquals(true, $this->form->ranking->inRanking($input)); $this->assertEquals('text', $input->type); @@ -44,9 +46,9 @@ public function add_node_to_a_parent_model_when_node_is_sibling() /** @test */ public function add_node_to_a_parent_model_after_an_existing_node() { - $afterInput = $this->form->add('text'); - $this->form->add('text'); - $this->form->add('text'); + $afterInput = $this->form->add('text')->saveAndFirst(); + $this->form->add('text')->save(); + $this->form->add('text')->save(); $afterInputRank = $this->form->ranking->rank($afterInput); $input = $this->form->addAfter($afterInput->html_attributes['id'], 'text'); $inputRank = $this->form->ranking->rank($input); @@ -60,9 +62,9 @@ public function add_node_to_a_parent_model_after_an_existing_node() /** @test */ public function add_node_to_a_parent_model_before_an_existing_node() { - $beforeInput = $this->form->add('text'); - $this->form->add('text'); - $this->form->add('text'); + $beforeInput = $this->form->add('text')->saveAndFirst(); + $this->form->add('text')->save(); + $this->form->add('text')->save(); $beforeInputRank = $this->form->ranking->rank($beforeInput); $input = $this->form->addBefore($beforeInput->html_attributes['id'], 'text'); $inputRank = $this->form->ranking->rank($input); @@ -76,9 +78,9 @@ public function add_node_to_a_parent_model_before_an_existing_node() /** @test */ public function add_node_to_a_parent_model_at_specific_rank() { - $this->form->add('text'); - $this->form->add('text'); - $this->form->add('text'); + $this->form->add('text')->save(); + $this->form->add('text')->save(); + $this->form->add('text')->save(); $input = $this->form->addAtRank(2, 'text'); $inputRank = $this->form->ranking->rank($input); @@ -91,9 +93,9 @@ public function add_node_to_a_parent_model_at_specific_rank() /** @test */ public function get_node_by_html_attribute_id() { - $this->form->add('text'); + $this->form->add('text')->save(); $this->form->add('text')->withHtmlAttributes(['id' => 'test'])->save(); - $this->form->add('paragraph'); + $this->form->add('paragraph')->save(); $node = $this->form->node('test'); $this->assertEquals('text', $node->type); @@ -105,9 +107,9 @@ public function get_node_by_html_attribute_id() /** @test */ public function get_node_by_html_attribute_name() { - $this->form->add('text'); + $this->form->add('text')->save(); $this->form->add('text')->withHtmlAttributes(['name' => 'test'])->save(); - $this->form->add('paragraph'); + $this->form->add('paragraph')->save(); $node = $this->form->node('test'); $this->assertEquals('text', $node->type); @@ -119,9 +121,9 @@ public function get_node_by_html_attribute_name() /** @test */ public function get_node_by_html_attribute_value() { - $this->form->add('text'); + $this->form->add('text')->save(); $this->form->add('text')->withHtmlAttributes(['value' => 'test'])->save(); - $this->form->add('paragraph'); + $this->form->add('paragraph')->save(); $node = $this->form->node('test'); $this->assertEquals('text', $node->type); @@ -134,9 +136,9 @@ public function get_node_by_html_attribute_value() /** @test */ public function get_node_when_doesnt_exist() { - $this->form->add('text'); - $this->form->add('text'); - $this->form->add('paragraph'); + $this->form->add('text')->save(); + $this->form->add('text')->save(); + $this->form->add('paragraph')->save(); $node = $this->form->node('test'); $this->assertNull($node); @@ -145,9 +147,9 @@ public function get_node_when_doesnt_exist() /** @test */ public function get_all_nodes_from_parent_model() { - $this->form->add('text'); - $this->form->add('text'); - $this->form->add('paragraph'); + $this->form->add('text')->save(); + $this->form->add('text')->save(); + $this->form->add('paragraph')->save(); $nodes = $this->form->nodes(); $this->assertEquals(3, $nodes->count()); @@ -166,9 +168,9 @@ public function get_all_nodes_from_parent_model() /** @test */ public function get_all_nodes_from_parent_model_filtered_by_type() { - $this->form->add('text'); - $this->form->add('text'); - $this->form->add('paragraph'); + $this->form->add('text')->save(); + $this->form->add('text')->save(); + $this->form->add('paragraph')->save(); $nodes = $this->form->nodes('text'); $this->assertEquals(2, $nodes->count()); diff --git a/tests/Unit/Traits/HasOptionsTest.php b/tests/Unit/Traits/HasOptionsTest.php index 349de76..4944054 100644 --- a/tests/Unit/Traits/HasOptionsTest.php +++ b/tests/Unit/Traits/HasOptionsTest.php @@ -2,7 +2,7 @@ namespace Belvedere\FormMaker\Tests\Unit\Traits; -use Belvedere\FormMaker\Models\Nodes\Inputs\Select\Selecter; +use Belvedere\FormMaker\Models\Form\Form; use Belvedere\FormMaker\Tests\TestCase; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -16,17 +16,18 @@ public function setUp(): void { parent::setUp(); - $this->select = Selecter::forceCreate([ - 'nodable_type' => 'test', - 'nodable_id' => 1, - 'type' => 'select' - ]); + $form = new Form(); + $form->fill(['name' => 'test'])->save(); + $this->select = $form->add('select')->saveAndFirst(); } /** @test */ public function add_option_input_to_a_parent_model() { - $option = $this->select->addOption(['title' => 'Cat', 'value' => 'cat', 'text' => 'Cat!']); + $option = $this->select->addOption() + ->withHtmlAttributes(['title' => 'Cat', 'value' => 'cat']) + ->withText('Cat!') + ->saveAndFirst(); $this->assertEquals(true, $this->select->ranking->inRanking($option)); $this->assertEquals('option', $option->type);