Skip to content

Commit

Permalink
Merge pull request #89 from musimana/feature/AddTypeMethodToBlockFactory
Browse files Browse the repository at this point in the history
feat: Add type Method to BlockFactory
  • Loading branch information
musimana authored Jun 2, 2024
2 parents 324aa7d + 526ddad commit 35f3626
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
16 changes: 12 additions & 4 deletions database/factories/BlockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]));
}
}
37 changes: 12 additions & 25 deletions tests/Datasets/Blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,56 +15,43 @@
. '<li><a href="https://tailwindcss.com/docs" target="_blank" rel="noopener noreferrer" class="group inline-flex items-center hover:text-gray-900 dark:hover:text-gray-50 focus:outline focus:outline-2 focus:rounded-sm focus:outline-gray-100"><span style="display: inline-block;" class="font-mono w-48"><strong>T</strong>ailwind v3.x</span></a><em>CSS framework</em></li>'
. '</ul><p>Running on PHP v' . $php_version . '</p>';

$tabs = [BlockType::TABS->value => ['Tab One', 'Tab Two']];
$tabs = ['tabs' => ['Tab One', 'Tab Two'], 'tabContents' => ['<p>Tab one content.</p>', '<p>Tab two content.</p>']];
$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],
['html' => $stack_html, 'type' => BlockType::STACK->value],
],
],
'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],
Expand Down

0 comments on commit 35f3626

Please sign in to comment.