From adb961b8a07f6e6e44e1538b9de1664a4bb99e0f Mon Sep 17 00:00:00 2001 From: Graham Aitken Date: Sat, 1 Jun 2024 14:42:23 +0100 Subject: [PATCH 1/2] Add type method to BlockFactory --- database/factories/BlockFactory.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/database/factories/BlockFactory.php b/database/factories/BlockFactory.php index 317616dd..23c2acfb 100644 --- a/database/factories/BlockFactory.php +++ b/database/factories/BlockFactory.php @@ -21,16 +21,24 @@ public function definition(): array { $type = fake()->randomElement(Block::typeValues()); $page = Page::inRandomOrder()->first() ?? Page::factory()->create(); - $blocks_count = $page->blocks->count(); - $display_order = $blocks_count ? $blocks_count + 1 : 0; - $data = $type === BlockType::TABS->value ? json_encode(['tabs' => ['Tab One', 'Tab Two']]) : null; + $display_order = $page->blocks->count(); return [ 'type' => $type, 'parent_type' => Page::class, 'parent_id' => $page->id, 'display_order' => $display_order, - 'data' => $data, + 'data' => null, ]; } + + /** Indicate the type for the block model. */ + public function type(BlockType $block_type, ?string $json = null): static + { + return $this->state(fn (array $attributes) => array_merge([ + ...$attributes, + 'type' => $block_type->value, + 'data' => $json, + ])); + } } From 526ddadac3fd9d7aeeeb9e0ef76b01bd581d2433 Mon Sep 17 00:00:00 2001 From: Graham Aitken Date: Sun, 2 Jun 2024 12:46:35 +0100 Subject: [PATCH 2/2] Update blocks dataset to use BlockFactory type method --- tests/Datasets/Blocks.php | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/tests/Datasets/Blocks.php b/tests/Datasets/Blocks.php index e9a4c17a..cb488839 100644 --- a/tests/Datasets/Blocks.php +++ b/tests/Datasets/Blocks.php @@ -15,18 +15,19 @@ . '
  • Tailwind v3.xCSS framework
  • ' . '

    Running on PHP v' . $php_version . '

    '; -$tabs = [BlockType::TABS->value => ['Tab One', 'Tab Two']]; +$tabs = ['tabs' => ['Tab One', 'Tab Two'], 'tabContents' => ['

    Tab one content.

    ', '

    Tab two content.

    ']]; +$tabs_json = json_encode($tabs) ?: ''; -dataset('blocks', function () use ($stack_html, $tabs) { +dataset('blocks', function () use ($stack_html, $tabs, $tabs_json) { return [ 'stack block' => [ - fn () => collect([Block::factory()->create(['type' => BlockType::STACK->value])]), + fn () => collect([Block::factory()->type(BlockType::STACK)->create()]), [['html' => $stack_html, 'type' => BlockType::STACK->value]], ], 'stack blocks' => [ fn () => collect([ - Block::factory()->create(['type' => BlockType::STACK->value]), - Block::factory()->create(['type' => BlockType::STACK->value]), + Block::factory()->type(BlockType::STACK)->create(), + Block::factory()->type(BlockType::STACK)->create(), ]), [ ['html' => $stack_html, 'type' => BlockType::STACK->value], @@ -34,37 +35,23 @@ ], ], 'tabs block' => [ - fn () => collect([ - Block::factory()->create([ - 'type' => BlockType::TABS->value, - 'data' => json_encode($tabs), - ]), - ]), + fn () => collect([Block::factory()->type(BlockType::TABS, $tabs_json)->create()]), [$tabs], ], 'tabs blocks' => [ fn () => collect([ - Block::factory()->create([ - 'type' => BlockType::TABS->value, - 'data' => json_encode($tabs), - ]), - Block::factory()->create([ - 'type' => BlockType::TABS->value, - 'data' => json_encode($tabs), - ]), + Block::factory()->type(BlockType::TABS, $tabs_json)->create(), + Block::factory()->type(BlockType::TABS, $tabs_json)->create(), ]), [ $tabs, $tabs, ], ], - 'mixed blocks' => [ + 'all blocks' => [ fn () => collect([ - Block::factory()->create(['type' => BlockType::STACK->value]), - Block::factory()->create([ - 'type' => BlockType::TABS->value, - 'data' => json_encode($tabs), - ]), + Block::factory()->type(BlockType::STACK)->create(), + Block::factory()->type(BlockType::TABS, $tabs_json)->create(), ]), [ ['html' => $stack_html, 'type' => BlockType::STACK->value],