From 948b82f5dcbc94176178b8bc59f55538a877cdfc Mon Sep 17 00:00:00 2001 From: Kurt Friars Date: Wed, 22 Nov 2023 00:04:15 +0700 Subject: [PATCH] Add testing for new layouts paradigm --- config/contentable.php | 11 +- tests/Feature/LayoutSyncTest.php | 86 +++++++ tests/Feature/LayoutsTest.php | 215 +++++++++++++++--- tests/Helper/Blade/sync/default.blade.php | 0 tests/Helper/Blade/sync/holidays.blade.php | 0 tests/Helper/Blade/sync/pages/index.blade.php | 0 .../Helper/Blade/sync/pages/landing.blade.php | 0 tests/Helper/Blade/sync/pages/show.blade.php | 0 .../Blade/sync/posts/featured.blade.php | 0 tests/Helper/Blade/sync/posts/index.blade.php | 0 tests/Helper/Blade/sync/posts/show.blade.php | 0 .../Blade/sync/products/index.blade.php | 0 .../Blade/sync/products/promo.blade.php | 0 .../Helper/Blade/sync/products/show.blade.php | 0 .../Database/Factories/LayoutFactory.php | 3 +- .../Helper/Database/Factories/PostFactory.php | 26 +++ .../Database/Factories/ProductFactory.php | 20 ++ .../Migrations/create_layouts_table.php | 5 +- .../Migrations/create_pages_table.php | 2 +- .../Migrations/create_posts_table.php | 24 ++ .../Migrations/create_products_table.php | 25 ++ tests/Helper/Inertia/Sync/Default.jsx | 0 tests/Helper/Inertia/Sync/Holidays.jsx | 0 tests/Helper/Inertia/Sync/Pages/Index.jsx | 0 tests/Helper/Inertia/Sync/Pages/Landing.jsx | 0 tests/Helper/Inertia/Sync/Pages/Show.jsx | 0 tests/Helper/Inertia/Sync/Posts/Featured.jsx | 0 tests/Helper/Inertia/Sync/Posts/Index.jsx | 0 tests/Helper/Inertia/Sync/Posts/Show.jsx | 0 tests/Helper/Inertia/Sync/Products/Index.jsx | 0 tests/Helper/Inertia/Sync/Products/Promo.jsx | 0 tests/Helper/Inertia/Sync/Products/Show.jsx | 0 tests/Helper/Models/Layout.php | 2 + tests/Helper/Models/Page.php | 15 +- tests/Helper/Models/Post.php | 21 ++ tests/Helper/Models/Product.php | 19 ++ tests/Pest.php | 60 +++++ 37 files changed, 487 insertions(+), 47 deletions(-) create mode 100644 tests/Feature/LayoutSyncTest.php create mode 100644 tests/Helper/Blade/sync/default.blade.php create mode 100644 tests/Helper/Blade/sync/holidays.blade.php create mode 100644 tests/Helper/Blade/sync/pages/index.blade.php create mode 100644 tests/Helper/Blade/sync/pages/landing.blade.php create mode 100644 tests/Helper/Blade/sync/pages/show.blade.php create mode 100644 tests/Helper/Blade/sync/posts/featured.blade.php create mode 100644 tests/Helper/Blade/sync/posts/index.blade.php create mode 100644 tests/Helper/Blade/sync/posts/show.blade.php create mode 100644 tests/Helper/Blade/sync/products/index.blade.php create mode 100644 tests/Helper/Blade/sync/products/promo.blade.php create mode 100644 tests/Helper/Blade/sync/products/show.blade.php create mode 100644 tests/Helper/Database/Factories/PostFactory.php create mode 100644 tests/Helper/Database/Factories/ProductFactory.php create mode 100644 tests/Helper/Database/Migrations/create_posts_table.php create mode 100644 tests/Helper/Database/Migrations/create_products_table.php create mode 100644 tests/Helper/Inertia/Sync/Default.jsx create mode 100644 tests/Helper/Inertia/Sync/Holidays.jsx create mode 100644 tests/Helper/Inertia/Sync/Pages/Index.jsx create mode 100644 tests/Helper/Inertia/Sync/Pages/Landing.jsx create mode 100644 tests/Helper/Inertia/Sync/Pages/Show.jsx create mode 100644 tests/Helper/Inertia/Sync/Posts/Featured.jsx create mode 100644 tests/Helper/Inertia/Sync/Posts/Index.jsx create mode 100644 tests/Helper/Inertia/Sync/Posts/Show.jsx create mode 100644 tests/Helper/Inertia/Sync/Products/Index.jsx create mode 100644 tests/Helper/Inertia/Sync/Products/Promo.jsx create mode 100644 tests/Helper/Inertia/Sync/Products/Show.jsx create mode 100644 tests/Helper/Models/Post.php create mode 100644 tests/Helper/Models/Product.php diff --git a/config/contentable.php b/config/contentable.php index fce35c3..786baae 100644 --- a/config/contentable.php +++ b/config/contentable.php @@ -1,12 +1,15 @@ \Plank\Contentable\Models\Content::class, - 'cache' => [ - 'ttl' => 10800, + 'content' => [ + 'model' => \Plank\Contentable\Models\Content::class, ], 'layouts' => [ + 'folder' => 'layouts', + 'mode' => \Plank\Contentable\Enums\LayoutMode::Blade, 'model' => \Plank\Contentable\Models\Layout::class, ], + 'cache' => [ + 'ttl' => 10800, + ], ]; diff --git a/tests/Feature/LayoutSyncTest.php b/tests/Feature/LayoutSyncTest.php new file mode 100644 index 0000000..a5e5a3b --- /dev/null +++ b/tests/Feature/LayoutSyncTest.php @@ -0,0 +1,86 @@ +assertExitCode(0); + + expect($layout = Layout::where('key', 'default')->first())->not->toBeNull(); + expect($layout->name)->toBe('Default'); + expect($layout->type)->toBe(LayoutType::Global); + expect($layout->layoutable)->toBeNull(); + + expect($layout = Layout::where('key', 'holidays')->first())->not->toBeNull(); + expect($layout->name)->toBe('Holidays'); + expect($layout->type)->toBe(LayoutType::Global); + expect($layout->layoutable)->toBeNull(); + + expect($layout = Layout::where('key', 'pages.index')->first())->not->toBeNull(); + expect($layout->name)->toBe('Page Index'); + expect($layout->type)->toBe(LayoutType::Index); + expect($layout->layoutable)->toBe(Page::layoutKey()); + + expect($layout = Layout::where('key', 'pages.show')->first())->not->toBeNull(); + expect($layout->name)->toBe('Page Details'); + expect($layout->type)->toBe(LayoutType::Show); + expect($layout->layoutable)->toBe(Page::layoutKey()); + + expect($layout = Layout::where('key', 'pages.landing')->first())->not->toBeNull(); + expect($layout->name)->toBe('Landing Page'); + expect($layout->type)->toBe(LayoutType::Custom); + expect($layout->layoutable)->toBe(Page::layoutKey()); + + expect($layout = Layout::where('key', 'products.index')->first())->not->toBeNull(); + expect($layout->name)->toBe('Product Index'); + expect($layout->type)->toBe(LayoutType::Index); + expect($layout->layoutable)->toBe(Product::layoutKey()); + + expect($layout = Layout::where('key', 'products.show')->first())->not->toBeNull(); + expect($layout->name)->toBe('Product Details'); + expect($layout->type)->toBe(LayoutType::Show); + expect($layout->layoutable)->toBe(Product::layoutKey()); + + expect($layout = Layout::where('key', 'products.promo')->first())->not->toBeNull(); + expect($layout->name)->toBe('Promo Product'); + expect($layout->type)->toBe(LayoutType::Custom); + expect($layout->layoutable)->toBe(Product::layoutKey()); + }); + + it('does not delete missing global layouts on sync', function () { + Layout::factory()->create([ + 'key' => 'custom.global', + 'name' => 'Custom Global', + 'type' => LayoutType::Custom, + ]); + + artisan('contentable:sync') + ->assertExitCode(0); + + expect(Layout::where('key', 'custom.global')->exists())->toBeTrue(); + }); + + it('does not delete missing layoutable layouts on sync', function () { + Layout::factory()->create([ + 'key' => 'pages.y2k', + 'name' => 'Y2K Page', + 'type' => LayoutType::Custom, + 'layoutable' => Page::layoutKey(), + ]); + + artisan('contentable:sync') + ->assertExitCode(0); + + expect(Layout::where('key', 'pages.y2k')->exists())->toBeTrue(); + }); +}); diff --git a/tests/Feature/LayoutsTest.php b/tests/Feature/LayoutsTest.php index a887be8..13d1d1d 100644 --- a/tests/Feature/LayoutsTest.php +++ b/tests/Feature/LayoutsTest.php @@ -1,45 +1,202 @@ create([ - 'identifier' => 'pages.show', - ]); +use function Pest\Laravel\artisan; - Layout::factory()->create([ - 'identifier' => 'pages.index', - ]); +describe('It throws errors when layouts do not exist', function () { + it('throws an error when the Detail layout doesnt exist', function () { + Product::factory()->create()->layout(); + })->throws(MissingLayoutException::class); - Layout::factory()->create([ - 'identifier' => 'promotions', - ]); + it('throws an error when the Index layout doesnt exist', function () { + Product::indexLayout(); + })->throws(MissingLayoutException::class); }); -it('finds the show layout using the default key', function () { - $page = Page::factory()->create(); +describe('It returns Blade Layouts for Layoutables', function () { + beforeEach(function () { + setBladePath('sync'); + artisan('contentable:sync')->assertExitCode(0); + }); - expect($layout = $page->layout())->toBeInstanceOf(Layout::class); - expect($layout->layoutKey())->toBe('pages.show'); - expect($layout->bladeTemplate())->toBe('layouts.pages.show'); - expect($layout->inertiaComponent())->toBe('Pages/Show'); + it('can return a global Blade Layout', function () { + $layout = Layout::query() + ->where('key', 'default') + ->first(); + + $page = Page::factory()->create([ + 'layout_id' => $layout->id, + ]); + + expect($layout = $page->layout())->not->toBeNull(); + expect($layout->name)->toBe('Default'); + expect($layout->type)->toBe(LayoutType::Global); + expect($layout->layoutable)->toBeNull(); + }); + + it('can return a layoutable Blade Layout', function () { + $layout = Layout::query() + ->where('key', 'pages.landing') + ->first(); + + $page = Page::factory()->create([ + 'layout_id' => $layout->id, + ]); + + expect($layout = $page->layout())->not->toBeNull(); + expect($layout->name)->toBe('Landing Page'); + expect($layout->type)->toBe(LayoutType::Custom); + expect($layout->layoutable)->toBe(Page::layoutKey()); + }); + + it('can return an index Blade layout', function () { + expect($layout = Product::indexLayout())->not->toBeNull(); + expect($layout->name)->toBe('Product Index'); + expect($layout->type)->toBe(LayoutType::Index); + expect($layout->layoutable)->toBe(Product::layoutKey()); + }); }); -it('finds the index layout using the default key', function () { - expect($layout = Page::indexLayout())->toBeInstanceOf(Layout::class); - expect($layout->layoutKey())->toBe('pages.index'); - expect($layout->bladeTemplate())->toBe('layouts.pages.index'); - expect($layout->inertiaComponent())->toBe('Pages/Index'); +describe('It returns Inertia Layouts for Layoutables', function () { + beforeEach(function () { + config()->set('contentable.layouts.mode', LayoutMode::InertiaJsx); + setInertiaPath('Sync'); + + artisan('contentable:sync')->assertExitCode(0); + }); + + it('can return a global Inertia Layout', function () { + $layout = Layout::query() + ->where('key', 'Default') + ->first(); + + + $page = Page::factory()->create([ + 'layout_id' => $layout->id, + ]); + + expect($layout = $page->layout())->not->toBeNull(); + expect($layout->name)->toBe('Default'); + expect($layout->type)->toBe(LayoutType::Global); + expect($layout->layoutable)->toBeNull(); + }); + + it('can return a layoutable Inertia Layout', function () { + $layout = Layout::query() + ->where('key', 'Pages/Landing') + ->first(); + + $page = Page::factory()->create([ + 'layout_id' => $layout->id, + ]); + + expect($layout = $page->layout())->not->toBeNull(); + expect($layout->name)->toBe('Landing Page'); + expect($layout->type)->toBe(LayoutType::Custom); + expect($layout->layoutable)->toBe(Page::layoutKey()); + }); + + it('can return an index Inertia layout', function () { + expect($layout = Product::indexLayout())->not->toBeNull(); + expect($layout->name)->toBe('Product Index'); + expect($layout->type)->toBe(LayoutType::Index); + expect($layout->layoutable)->toBe(Product::layoutKey()); + }); }); -it('finds the show layout using a custom key', function () { - $page = Page::factory()->create([ - 'title' => 'Promotions', - ]); +describe('It returns layout options for blade', function () { + beforeEach(function () { + setBladePath('sync'); + artisan('contentable:sync')->assertExitCode(0); + }); + + it('returns all but excluded layouts for models that include global layouts', function () { + $page = Page::factory()->create(); + + expect($layouts = $page->layouts())->toHaveCount(4); + + expect($layout = $layouts->where('key', 'holidays')->first())->toBeNull(); + + expect($layout = $layouts->where('key', 'default')->first())->not->toBeNull(); + expect($layout->name)->toBe('Default'); + expect($layout->type)->toBe(LayoutType::Global); + expect($layout->layoutable)->toBeNull(); + + expect($layout = $layouts->where('key', 'pages.index')->first())->not->toBeNull(); + expect($layout->name)->toBe('Page Index'); + expect($layout->type)->toBe(LayoutType::Index); + expect($layout->layoutable)->toBe(Page::layoutKey()); + + expect($layout = $layouts->where('key', 'pages.show')->first())->not->toBeNull(); + expect($layout->name)->toBe('Page Details'); + expect($layout->type)->toBe(LayoutType::Show); + expect($layout->layoutable)->toBe(Page::layoutKey()); + + expect($layout = $layouts->where('key', 'pages.landing')->first())->not->toBeNull(); + expect($layout->name)->toBe('Landing Page'); + expect($layout->type)->toBe(LayoutType::Custom); + expect($layout->layoutable)->toBe(Page::layoutKey()); + }); + + it('excludes all global layouts for layoutables which should not use them', function () { + $post = Post::factory()->create(); + + expect($layouts = $post->layouts())->toHaveCount(3); + + expect($layout = $layouts->where('key', 'holidays')->first())->toBeNull(); + expect($layout = $layouts->where('key', 'default')->first())->toBeNull(); + + expect($layout = $layouts->where('key', 'posts.index')->first())->not->toBeNull(); + expect($layout->name)->toBe('Post Index'); + expect($layout->type)->toBe(LayoutType::Index); + expect($layout->layoutable)->toBe(Post::layoutKey()); + + expect($layout = $layouts->where('key', 'posts.show')->first())->not->toBeNull(); + expect($layout->name)->toBe('Post Details'); + expect($layout->type)->toBe(LayoutType::Show); + expect($layout->layoutable)->toBe(Post::layoutKey()); + + expect($layout = $layouts->where('key', 'posts.featured')->first())->not->toBeNull(); + expect($layout->name)->toBe('Featured Post'); + expect($layout->type)->toBe(LayoutType::Custom); + expect($layout->layoutable)->toBe(Post::layoutKey()); + }); + + it('shows all layouts when global layouts are available and non are excluded', function () { + $product = Product::factory()->create(); + + expect($layouts = $product->layouts())->toHaveCount(5); + + expect($layout = $layouts->where('key', 'holidays')->first())->not->toBeNull(); + expect($layout->name)->toBe('Holidays'); + expect($layout->type)->toBe(LayoutType::Global); + expect($layout->layoutable)->toBeNull(); + + expect($layout = $layouts->where('key', 'default')->first())->not->toBeNull(); + expect($layout->name)->toBe('Default'); + expect($layout->type)->toBe(LayoutType::Global); + expect($layout->layoutable)->toBeNull(); + + expect($layout = $layouts->where('key', 'products.index')->first())->not->toBeNull(); + expect($layout->name)->toBe('Product Index'); + expect($layout->type)->toBe(LayoutType::Index); + expect($layout->layoutable)->toBe(Product::layoutKey()); + + expect($layout = $layouts->where('key', 'products.show')->first())->not->toBeNull(); + expect($layout->name)->toBe('Product Details'); + expect($layout->type)->toBe(LayoutType::Show); + expect($layout->layoutable)->toBe(Product::layoutKey()); - expect($layout = $page->layout())->toBeInstanceOf(Layout::class); - expect($layout->layoutKey())->toBe('promotions'); - expect($layout->bladeTemplate())->toBe('layouts.promotions'); - expect($layout->inertiaComponent())->toBe('Promotions'); + expect($layout = $layouts->where('key', 'products.promo')->first())->not->toBeNull(); + expect($layout->name)->toBe('Promo Product'); + expect($layout->type)->toBe(LayoutType::Custom); + expect($layout->layoutable)->toBe(Product::layoutKey()); + }); }); diff --git a/tests/Helper/Blade/sync/default.blade.php b/tests/Helper/Blade/sync/default.blade.php new file mode 100644 index 0000000..e69de29 diff --git a/tests/Helper/Blade/sync/holidays.blade.php b/tests/Helper/Blade/sync/holidays.blade.php new file mode 100644 index 0000000..e69de29 diff --git a/tests/Helper/Blade/sync/pages/index.blade.php b/tests/Helper/Blade/sync/pages/index.blade.php new file mode 100644 index 0000000..e69de29 diff --git a/tests/Helper/Blade/sync/pages/landing.blade.php b/tests/Helper/Blade/sync/pages/landing.blade.php new file mode 100644 index 0000000..e69de29 diff --git a/tests/Helper/Blade/sync/pages/show.blade.php b/tests/Helper/Blade/sync/pages/show.blade.php new file mode 100644 index 0000000..e69de29 diff --git a/tests/Helper/Blade/sync/posts/featured.blade.php b/tests/Helper/Blade/sync/posts/featured.blade.php new file mode 100644 index 0000000..e69de29 diff --git a/tests/Helper/Blade/sync/posts/index.blade.php b/tests/Helper/Blade/sync/posts/index.blade.php new file mode 100644 index 0000000..e69de29 diff --git a/tests/Helper/Blade/sync/posts/show.blade.php b/tests/Helper/Blade/sync/posts/show.blade.php new file mode 100644 index 0000000..e69de29 diff --git a/tests/Helper/Blade/sync/products/index.blade.php b/tests/Helper/Blade/sync/products/index.blade.php new file mode 100644 index 0000000..e69de29 diff --git a/tests/Helper/Blade/sync/products/promo.blade.php b/tests/Helper/Blade/sync/products/promo.blade.php new file mode 100644 index 0000000..e69de29 diff --git a/tests/Helper/Blade/sync/products/show.blade.php b/tests/Helper/Blade/sync/products/show.blade.php new file mode 100644 index 0000000..e69de29 diff --git a/tests/Helper/Database/Factories/LayoutFactory.php b/tests/Helper/Database/Factories/LayoutFactory.php index 151f8a1..062f4ae 100644 --- a/tests/Helper/Database/Factories/LayoutFactory.php +++ b/tests/Helper/Database/Factories/LayoutFactory.php @@ -18,7 +18,8 @@ public function definition() } return [ - 'identifier' => implode('.', $this->faker->words(2)), + 'key' => $key = implode('.', $this->faker->words(2)), + 'name' => (string) str($key)->replace('.', ' ')->title(), 'meta' => $meta, ]; } diff --git a/tests/Helper/Database/Factories/PostFactory.php b/tests/Helper/Database/Factories/PostFactory.php new file mode 100644 index 0000000..9fe52ff --- /dev/null +++ b/tests/Helper/Database/Factories/PostFactory.php @@ -0,0 +1,26 @@ + $title = $this->faker->sentence, + 'slug' => str($title)->slug(), + ]; + } + + public function configure() + { + return $this->afterMaking(function (Post $post) { + $post->slug = (string) str($post->title)->slug(); + }); + } +} diff --git a/tests/Helper/Database/Factories/ProductFactory.php b/tests/Helper/Database/Factories/ProductFactory.php new file mode 100644 index 0000000..c5cad5b --- /dev/null +++ b/tests/Helper/Database/Factories/ProductFactory.php @@ -0,0 +1,20 @@ + $this->faker->words(3, true), + 'code' => $this->faker->unique()->numberBetween(10000, 99999), + 'price_in_cents' => $this->faker->numberBetween(99, 99999), + ]; + } +} diff --git a/tests/Helper/Database/Migrations/create_layouts_table.php b/tests/Helper/Database/Migrations/create_layouts_table.php index c5f8b94..d9723bd 100644 --- a/tests/Helper/Database/Migrations/create_layouts_table.php +++ b/tests/Helper/Database/Migrations/create_layouts_table.php @@ -10,7 +10,10 @@ public function up() { Schema::create('layouts', function (Blueprint $table) { $table->id(); - $table->string('identifier')->unique()->nullable(); + $table->string('key')->unique(); + $table->string('name')->unique(); + $table->string('layoutable')->nullable(); + $table->string('type'); $table->json('meta')->nullable(); $table->timestamps(); }); diff --git a/tests/Helper/Database/Migrations/create_pages_table.php b/tests/Helper/Database/Migrations/create_pages_table.php index 93b4937..07c812d 100644 --- a/tests/Helper/Database/Migrations/create_pages_table.php +++ b/tests/Helper/Database/Migrations/create_pages_table.php @@ -12,7 +12,7 @@ public function up() $table->id(); $table->string('title'); $table->string('slug'); - $table->boolean('paywall')->default(false); + $table->foreignId('layout_id')->nullable()->constrained(); $table->timestamps(); }); } diff --git a/tests/Helper/Database/Migrations/create_posts_table.php b/tests/Helper/Database/Migrations/create_posts_table.php new file mode 100644 index 0000000..69ca608 --- /dev/null +++ b/tests/Helper/Database/Migrations/create_posts_table.php @@ -0,0 +1,24 @@ +id(); + $table->string('title'); + $table->string('slug'); + $table->foreignId('layout_id')->nullable()->constrained(); + $table->timestamps(); + }); + } + + public function down() + { + Schema::dropIfExists('posts'); + } +}; diff --git a/tests/Helper/Database/Migrations/create_products_table.php b/tests/Helper/Database/Migrations/create_products_table.php new file mode 100644 index 0000000..eda13d7 --- /dev/null +++ b/tests/Helper/Database/Migrations/create_products_table.php @@ -0,0 +1,25 @@ +id(); + $table->string('title'); + $table->string('code'); + $table->integer('price_in_cents'); + $table->foreignId('layout_id')->nullable()->constrained(); + $table->timestamps(); + }); + } + + public function down() + { + Schema::dropIfExists('products'); + } +}; diff --git a/tests/Helper/Inertia/Sync/Default.jsx b/tests/Helper/Inertia/Sync/Default.jsx new file mode 100644 index 0000000..e69de29 diff --git a/tests/Helper/Inertia/Sync/Holidays.jsx b/tests/Helper/Inertia/Sync/Holidays.jsx new file mode 100644 index 0000000..e69de29 diff --git a/tests/Helper/Inertia/Sync/Pages/Index.jsx b/tests/Helper/Inertia/Sync/Pages/Index.jsx new file mode 100644 index 0000000..e69de29 diff --git a/tests/Helper/Inertia/Sync/Pages/Landing.jsx b/tests/Helper/Inertia/Sync/Pages/Landing.jsx new file mode 100644 index 0000000..e69de29 diff --git a/tests/Helper/Inertia/Sync/Pages/Show.jsx b/tests/Helper/Inertia/Sync/Pages/Show.jsx new file mode 100644 index 0000000..e69de29 diff --git a/tests/Helper/Inertia/Sync/Posts/Featured.jsx b/tests/Helper/Inertia/Sync/Posts/Featured.jsx new file mode 100644 index 0000000..e69de29 diff --git a/tests/Helper/Inertia/Sync/Posts/Index.jsx b/tests/Helper/Inertia/Sync/Posts/Index.jsx new file mode 100644 index 0000000..e69de29 diff --git a/tests/Helper/Inertia/Sync/Posts/Show.jsx b/tests/Helper/Inertia/Sync/Posts/Show.jsx new file mode 100644 index 0000000..e69de29 diff --git a/tests/Helper/Inertia/Sync/Products/Index.jsx b/tests/Helper/Inertia/Sync/Products/Index.jsx new file mode 100644 index 0000000..e69de29 diff --git a/tests/Helper/Inertia/Sync/Products/Promo.jsx b/tests/Helper/Inertia/Sync/Products/Promo.jsx new file mode 100644 index 0000000..e69de29 diff --git a/tests/Helper/Inertia/Sync/Products/Show.jsx b/tests/Helper/Inertia/Sync/Products/Show.jsx new file mode 100644 index 0000000..e69de29 diff --git a/tests/Helper/Models/Layout.php b/tests/Helper/Models/Layout.php index dcec15b..eda72bf 100644 --- a/tests/Helper/Models/Layout.php +++ b/tests/Helper/Models/Layout.php @@ -3,6 +3,7 @@ namespace Plank\Contentable\Tests\Helper\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; +use Plank\Contentable\Enums\LayoutType; use Plank\Contentable\Models\Layout as PackageLayout; class Layout extends PackageLayout @@ -14,6 +15,7 @@ class Layout extends PackageLayout protected $guarded = ['id']; protected $casts = [ + 'type' => LayoutType::class, 'meta' => 'json', ]; } diff --git a/tests/Helper/Models/Page.php b/tests/Helper/Models/Page.php index 2147809..d36fa47 100644 --- a/tests/Helper/Models/Page.php +++ b/tests/Helper/Models/Page.php @@ -13,18 +13,11 @@ class Page extends Model implements Contentable, Layoutable { use HasContent; use HasFactory; - use HasLayouts { - layoutKey as traitLayoutKey; - } + use HasLayouts; protected $guarded = ['id']; - public function layoutKey(): string - { - if ($this->title === 'Promotions') { - return 'promotions'; - } - - return $this->traitLayoutKey(); - } + protected array $excludedLayouts = [ + 'holidays', + ]; } diff --git a/tests/Helper/Models/Post.php b/tests/Helper/Models/Post.php new file mode 100644 index 0000000..4d1aaf7 --- /dev/null +++ b/tests/Helper/Models/Post.php @@ -0,0 +1,21 @@ +in(__DIR__); + +function setBladePath(string $path = ''): void +{ + $fixtures = str(realpath(__DIR__).'/Helper/Blade/') + ->append($path) + ->rtrim('/') + ->explode('/'); + + $folder = $fixtures->pop(); + $path = $fixtures->implode('/'); + + config()->set('view.paths', [$path]); + config()->set('contentable.layouts.folder', $folder); +} + +function setInertiaPath(string $path = ''): void +{ + $fixtures = str(realpath(__DIR__).'/Helper/Inertia/') + ->append($path) + ->rtrim('/') + ->explode('/'); + + $folder = $fixtures->pop(); + $path = $fixtures->implode('/'); + + $targetPath = resource_path('js'); + $targetFolder = $targetPath.'/Pages'; + + if (! file_exists($targetPath)) { + mkdir($targetPath, 0755, true); + } + + if (file_exists($targetFolder)) { + if (is_link($targetFolder)) { + unlink($targetFolder); + } else { + rmdir($targetFolder); + } + } + + symlink( + $path, + $targetFolder + ); + + config()->set('contentable.layouts.folder', $folder); +} + +function clearInertiaPath(): void +{ + $target = resource_path('js/Pages'); + + if (file_exists($target)) { + if (is_link($target)) { + unlink($target); + } else { + rmdir($target); + } + } +}