Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"require": {
"php": "^8.1",
"illuminate/support": "^9.0",
"illuminate/validation": "^9.0"
"illuminate/validation": "^9.0",
"fakerphp/faker": "^1.21"
},
"extra": {
"laravel": {
Expand Down
20 changes: 20 additions & 0 deletions src/Blocks/Attaches.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,24 @@ public function render(): string

return Helpers::renderNative(__DIR__ . '/../../resources/php/attaches.php', ['data' => $this->data]);
}

/**
* Generates fake data for the block.
*
* @param Generator $faker
*
* @return array
*/
public static function fake(\Faker\Generator $faker): array
{
return [
'title' => $faker->text(64),
'file' => [
'url' => $faker->url(),
'size' => $faker->numberBetween(250000, 10000000),
'name' => $faker->text(64),
'extension' => $faker->fileExtension(),
],
];
}
}
22 changes: 22 additions & 0 deletions src/Blocks/Checklist.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,26 @@ public function render(): string

return Helpers::renderNative(__DIR__ . '/../../resources/php/checklist.php', ['data' => $this->data]);
}

/**
* Generates fake data for the block.
*
* @param Generator $faker
*
* @return array
*/
public static function fake(\Faker\Generator $faker): array
{
$items = [];

foreach (range(0, $faker->numberBetween(1, 10)) as $index)
{
$items[] = [
'text' => $faker->text(48),
'checked' => $faker->boolean(),
];
}

return ['items' => $items];
}
}
12 changes: 12 additions & 0 deletions src/Blocks/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,16 @@ public function render(): string

return Helpers::renderNative(__DIR__ . '/../../resources/php/code.php', ['data' => $this->data]);
}

/**
* Generates fake data for the block.
*
* @param Generator $faker
*
* @return array
*/
public static function fake(\Faker\Generator $faker): array
{
return ['code' => $faker->text()];
}
}
12 changes: 12 additions & 0 deletions src/Blocks/Delimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,16 @@ public function render(): string

return Helpers::renderNative(__DIR__ . '/../../resources/php/delimiter.php', ['data' => $this->data]);
}

/**
* Generates fake data for the block.
*
* @param Generator $faker
*
* @return array
*/
public static function fake(\Faker\Generator $faker): array
{
return [];
}
}
19 changes: 19 additions & 0 deletions src/Blocks/Embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,23 @@ public function render(): string

return Helpers::renderNative(__DIR__ . '/../../resources/php/embed.php', ['data' => $this->data]);
}

/**
* Generates fake data for the block.
*
* @param Generator $faker
*
* @return array
*/
public static function fake(\Faker\Generator $faker): array
{
return [
'service' => $faker->text(32),
'source' => $faker->url(),
'embed' => $faker->url(),
'width' => $faker->numberBetween(64, 1024),
'height' => $faker->numberBetween(64, 1024),
'caption' => $faker->text(32),
];
}
}
15 changes: 15 additions & 0 deletions src/Blocks/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,19 @@ public function render(): string

return Helpers::renderNative(__DIR__ . '/../../resources/php/header.php', ['data' => $this->data]);
}

/**
* Generates fake data for the block.
*
* @param Generator $faker
*
* @return array
*/
public static function fake(\Faker\Generator $faker): array
{
return [
'text' => $faker->text(64),
'level' => $faker->numberBetween(1, 6),
];
}
}
18 changes: 18 additions & 0 deletions src/Blocks/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,22 @@ public function render(): string

return Helpers::renderNative(__DIR__ . '/../../resources/php/image.php', ['data' => $this->data]);
}

/**
* Generates fake data for the block.
*
* @param Generator $faker
*
* @return array
*/
public static function fake(\Faker\Generator $faker): array
{
return [
'file' => ['url' => $faker->imageUrl()],
'caption' => $faker->text(),
'withBorder' => $faker->boolean(),
'stretched' => $faker->boolean(),
'withBackground' => $faker->boolean(),
];
}
}
20 changes: 20 additions & 0 deletions src/Blocks/LinkTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,24 @@ public function render(): string

return Helpers::renderNative(__DIR__ . '/../../resources/php/linktool.php', ['data' => $this->data]);
}

/**
* Generates fake data for the block.
*
* @param Generator $faker
*
* @return array
*/
public static function fake(\Faker\Generator $faker): array
{
return [
'link' => $faker->url(),
'meta' => [
'title' => $faker->text(32),
'site_name' => $faker->text(32),
'description' => $faker->text(96),
'image' => ['url' => $faker->imageUrl()],
],
];
}
}
22 changes: 22 additions & 0 deletions src/Blocks/ListBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,26 @@ public function render(): string

return Helpers::renderNative(__DIR__ . '/../../resources/php/list.php', ['data' => $this->data]);
}

/**
* Generates fake data for the block.
*
* @param Generator $faker
*
* @return array
*/
public static function fake(\Faker\Generator $faker): array
{
$items = [];

foreach (range(0, $faker->numberBetween(1, 10)) as $index)
{
$items[] = $faker->text(64);
}

return [
'style' => $faker->randomElement(['ordered', 'unordered']),
'items' => $items,
];
}
}
12 changes: 12 additions & 0 deletions src/Blocks/Paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,16 @@ public function render(): string

return Helpers::renderNative(__DIR__ . '/../../resources/php/paragraph.php', ['data' => $this->data]);
}

/**
* Generates fake data for the block.
*
* @param Generator $faker
*
* @return array
*/
public static function fake(\Faker\Generator $faker): array
{
return ['text' => $faker->text(256)];
}
}
17 changes: 17 additions & 0 deletions src/Blocks/Personality.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,21 @@ public function render(): string

return Helpers::renderNative(__DIR__ . '/../../resources/php/personality.php', ['data' => $this->data]);
}

/**
* Generates fake data for the block.
*
* @param Generator $faker
*
* @return array
*/
public static function fake(\Faker\Generator $faker): array
{
return [
'name' => $faker->name(),
'description' => $faker->text(),
'link' => $faker->url(),
'photo' => $faker->imageUrl(),
];
}
}
16 changes: 16 additions & 0 deletions src/Blocks/Quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,20 @@ public function render(): string

return Helpers::renderNative(__DIR__ . '/../../resources/php/quote.php', ['data' => $this->data]);
}

/**
* Generates fake data for the block.
*
* @param Generator $faker
*
* @return array
*/
public static function fake(\Faker\Generator $faker): array
{
return [
'text' => $faker->text(),
'caption' => $faker->name(),
'alignment' => $faker->randomElement(['left', 'center']),
];
}
}
12 changes: 12 additions & 0 deletions src/Blocks/Raw.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,16 @@ public function render(): string
{
return $this->data->get('html', '');
}

/**
* Generates fake data for the block.
*
* @param Generator $faker
*
* @return array
*/
public static function fake(\Faker\Generator $faker): array
{
return ['html' => $faker->randomHtml()];
}
}
30 changes: 30 additions & 0 deletions src/Blocks/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,34 @@ public function render(): string

return Helpers::renderNative(__DIR__ . '/../../resources/php/table.php', ['data' => $this->data]);
}

/**
* Generates fake data for the block.
*
* @param Generator $faker
*
* @return array
*/
public static function fake(\Faker\Generator $faker): array
{
$content = [];
$width = $faker->numberBetween(2, 8);

foreach (range(0, $faker->numberBetween(1, 10)) as $index)
{
$row = [];

foreach (range(0, $width) as $index)
{
$row[] = $faker->text(64);
}

$content[] = $row;
}

return [
'withHeadings' => $faker->boolean(),
'content' => $content,
];
}
}
15 changes: 15 additions & 0 deletions src/Blocks/Warning.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,19 @@ public function render(): string

return Helpers::renderNative(__DIR__ . '/../../resources/php/warning.php', ['data' => $this->data]);
}

/**
* Generates fake data for the block.
*
* @param Generator $faker
*
* @return array
*/
public static function fake(\Faker\Generator $faker): array
{
return [
'title' => $faker->text(32),
'message' => $faker->text(),
];
}
}
30 changes: 30 additions & 0 deletions src/EditorPhp.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,34 @@ public function __toString(): string
{
return $this->render();
}

/**
* Generates fake instance.
*
* @param bool $instance
* @param int $minLength
* @param int $maxLength
*
* @return EditorPhp|string
*/
public static function fake(bool $instance = false, int $minLength = 8, int $maxLength = 30): EditorPhp|string
{
$faker = \Faker\Factory::create();
$blocks = array_filter(Parser::$blocks, fn (string $provider) => method_exists($provider, 'fake'));
$generatedBlocks = [];

foreach (range(0, fake()->numberBetween($minLength, $maxLength)) as $index)
{
$block = fake()->randomElement($blocks);
$generatedBlocks[] = (new ($block)($block::fake($faker)))->toArray();
}

$generated = json_encode([
'time' => (int) Carbon::now()->getPreciseTimestamp(3),
'blocks' => $generatedBlocks,
'version' => fake()->semver(),
]);

return $instance ? static::make($generated) : $generated;
}
}
10 changes: 10 additions & 0 deletions tests/EditorPhpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,13 @@ function() {
'Can be rendered via toHtml',
fn ($sample) => expect(EditorPhp::make($sample)->toHtml())->toBeString(),
)->with('valid');

test(
'Can be generate fake data as instance',
fn () => expect(EditorPhp::fake(true))->toBeInstanceOf(EditorPhp::class)
);

test(
'Can be generate fake data as json',
fn () => expect(EditorPhp::fake(false))->toBeJson()
);