From d3dade5a552e196a2d771a25372cbfbd67eb73bb Mon Sep 17 00:00:00 2001 From: Claudio La Barbera Date: Thu, 30 May 2024 01:07:54 +0200 Subject: [PATCH] feat: converted `MarkdownWithDateModel` abstract class to a trait `WithDate` + tests refactoring - Implement `WithDate` trait - Remove `MarkdownWithDateModel` abstract class - Moved tests fixtures to a dedicated folder - Create a `Fixture` support class to get fixtures' filesystem for tests - Moved fake classes (`TestModel` and `TestWithDateModel`) outside tests files - Update README.md --- README.md | 23 ++++++++++---- .../WithDate.php} | 4 +-- tests/Support/Fixture.php | 23 ++++++++++++++ tests/Support/Models/TestModel.php | 13 ++++++++ tests/Support/Models/TestWithDateModel.php | 16 ++++++++++ tests/{ => Unit}/Models/MarkdownModelTest.php | 22 ++++--------- .../Models/Traits/WithDateTest.php} | 31 ++++++------------- .../content/blog/2024-05-22-test.md | 0 .../content/blog/2024-05-24-another-test.md | 0 .../blog/2024-05-26-yet-another-test.md | 0 .../content/pages/folder/test.md | 0 tests/{ => fixtures}/content/pages/test.md | 0 tests/{ => fixtures}/content/pages/test2.md | 0 tests/{ => fixtures}/content/pages/test3.md | 0 14 files changed, 87 insertions(+), 45 deletions(-) rename src/Models/{MarkdownWithDateModel.php => Traits/WithDate.php} (88%) create mode 100644 tests/Support/Fixture.php create mode 100644 tests/Support/Models/TestModel.php create mode 100644 tests/Support/Models/TestWithDateModel.php rename tests/{ => Unit}/Models/MarkdownModelTest.php (83%) rename tests/{Models/MarkdownWithDateModelTest.php => Unit/Models/Traits/WithDateTest.php} (59%) rename tests/{ => fixtures}/content/blog/2024-05-22-test.md (100%) rename tests/{ => fixtures}/content/blog/2024-05-24-another-test.md (100%) rename tests/{ => fixtures}/content/blog/2024-05-26-yet-another-test.md (100%) rename tests/{ => fixtures}/content/pages/folder/test.md (100%) rename tests/{ => fixtures}/content/pages/test.md (100%) rename tests/{ => fixtures}/content/pages/test2.md (100%) rename tests/{ => fixtures}/content/pages/test3.md (100%) diff --git a/README.md b/README.md index d8c6d57..3452504 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ GitHub Workflow Status

-Easily manage Markdown files with YAML Front Matter section using Eloquent Models. +Easily manage Markdown files with a YAML Front Matter section using Eloquent models in your Laravel application. ## Installation @@ -50,7 +50,9 @@ Easily manage Markdown files with YAML Front Matter section using Eloquent Model Create a new model that extends `TheBatClaudio\EloquentMarkdown\Models\MarkdownModel`: ```php -class Page extends TheBatClaudio\EloquentMarkdown\Models\MarkdownModel +use TheBatClaudio\EloquentMarkdown\Models\MarkdownModel; + +class Page extends MarkdownModel { } ``` @@ -124,11 +126,15 @@ $newHomepage->save(); ### Using Markdown with dates (e.g. `YYYY-MM-DD-your-markdown.md`) -Create a new model that extends `TheBatClaudio\EloquentMarkdown\Models\MarkdownWithDateModel`: +Create a new model that extends `TheBatClaudio\EloquentMarkdown\Models\MarkdownModel` and uses `TheBatClaudio\EloquentMarkdown\Models\Traits\WithDate` trait: ```php -class Article extends TheBatClaudio\EloquentMarkdown\Models\MarkdownWithDateModel +use TheBatClaudio\EloquentMarkdown\Models\MarkdownModel; +use TheBatClaudio\EloquentMarkdown\Models\Traits\WithDate; + +class Article extends MarkdownModel { + use WithDate; } ``` @@ -141,15 +147,20 @@ You will find two new attributes inside your model: You can extend `getContentPath` inside your model to use different paths for different models: ```php -class Article extends TheBatClaudio\EloquentMarkdown\Models\MarkdownWithDateModel +use TheBatClaudio\EloquentMarkdown\Models\MarkdownModel; +use TheBatClaudio\EloquentMarkdown\Models\Traits\WithDate; + +class Article extends MarkdownModel { + use WithDate; + protected static function getContentPath(): string { return 'articles'; } } -class Page extends TheBatClaudio\EloquentMarkdown\Models\MarkdownModel +class Page extends MarkdownModel { protected static function getContentPath(): string { diff --git a/src/Models/MarkdownWithDateModel.php b/src/Models/Traits/WithDate.php similarity index 88% rename from src/Models/MarkdownWithDateModel.php rename to src/Models/Traits/WithDate.php index b5fae80..b79b92e 100644 --- a/src/Models/MarkdownWithDateModel.php +++ b/src/Models/Traits/WithDate.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace TheBatClaudio\EloquentMarkdown\Models; +namespace TheBatClaudio\EloquentMarkdown\Models\Traits; use Carbon\Carbon; use Illuminate\Support\Str; @@ -13,7 +13,7 @@ * @property Carbon $date * @property string $slug */ -abstract class MarkdownWithDateModel extends MarkdownModel +trait WithDate { public static function extractDate(string $filePath): Carbon { diff --git a/tests/Support/Fixture.php b/tests/Support/Fixture.php new file mode 100644 index 0000000..41b2bf9 --- /dev/null +++ b/tests/Support/Fixture.php @@ -0,0 +1,23 @@ + 'local', + 'root' => './tests/fixtures/content/', + ]); + } + + return static::$filesystem; + } +} diff --git a/tests/Support/Models/TestModel.php b/tests/Support/Models/TestModel.php new file mode 100644 index 0000000..9f3095c --- /dev/null +++ b/tests/Support/Models/TestModel.php @@ -0,0 +1,13 @@ +createLocalDriver([ - 'root' => __DIR__.'/../content', - ]) + Fixture::getFilesystem() ); }); @@ -86,7 +76,7 @@ protected static function getContentPath(): string $markdown->save(); - $filepath = __DIR__.'/../content/'.'pages'.'/'.$fileId.MarkdownModel::FILE_EXTENSION; + $filepath = Fixture::getFilesystem()->path("/pages/$fileId".MarkdownModel::FILE_EXTENSION); expect(file_exists($filepath)) ->toBeTrue(); @@ -106,7 +96,7 @@ protected static function getContentPath(): string $markdown->save(); - $filepath = __DIR__.'/../content/'.'pages'.'/'.$fileId.MarkdownModel::FILE_EXTENSION; + $filepath = Fixture::getFilesystem()->path("/pages/$fileId".MarkdownModel::FILE_EXTENSION); expect(file_exists($filepath)) ->toBeTrue(); @@ -119,7 +109,7 @@ protected static function getContentPath(): string it('should update file', function () { $fileId = 'update-test'; - $filepath = __DIR__.'/../content/'.'pages'.'/'.$fileId.MarkdownModel::FILE_EXTENSION; + $filepath = Fixture::getFilesystem()->path("/pages/$fileId".MarkdownModel::FILE_EXTENSION); $markdown = new TestModel(); diff --git a/tests/Models/MarkdownWithDateModelTest.php b/tests/Unit/Models/Traits/WithDateTest.php similarity index 59% rename from tests/Models/MarkdownWithDateModelTest.php rename to tests/Unit/Models/Traits/WithDateTest.php index 1904e3e..4b8fe77 100644 --- a/tests/Models/MarkdownWithDateModelTest.php +++ b/tests/Unit/Models/Traits/WithDateTest.php @@ -3,27 +3,16 @@ declare(strict_types=1); use Carbon\Carbon; -use Illuminate\Filesystem\FilesystemManager; -use TheBatClaudio\EloquentMarkdown\Models\MarkdownWithDateModel; - -class TestWithDateModel extends MarkdownWithDateModel -{ - protected static function getContentPath(): string - { - return 'blog'; - } -} +use TheBatClaudio\EloquentMarkdown\Tests\Support\Fixture; +use TheBatClaudio\EloquentMarkdown\Tests\Support\Models\TestWithDateModel; beforeEach(function () { - TestModel::setFilesystem( - (new FilesystemManager(null)) - ->createLocalDriver([ - 'root' => __DIR__.'/../content', - ]) + TestWithDateModel::setFilesystem( + Fixture::getFilesystem() ); }); -it('returns all files calling all method', function () { +it('returns all files with dates calling all method', function () { $markdowns = TestWithDateModel::all()->toArray(); expect($markdowns) @@ -42,13 +31,13 @@ protected static function getContentPath(): string expect($markdown) ->not->toBeNull() - ->and($markdown->toArray()) + ->and($markdown?->toArray()) ->toHaveKey('date') - ->and($markdown->date) + ->and($markdown?->date) ->toBeInstanceOf(Carbon::class) ->and( (new Carbon('2024-05-26')) - ->equalTo($markdown->date) + ->equalTo($markdown?->date) ) ->toBeTrue(); }); @@ -58,8 +47,8 @@ protected static function getContentPath(): string expect($markdown) ->not->toBeNull() - ->and($markdown->toArray()) + ->and($markdown?->toArray()) ->toHaveKey('slug') - ->and($markdown->slug) + ->and($markdown?->slug) ->toBe('yet-another-test'); }); diff --git a/tests/content/blog/2024-05-22-test.md b/tests/fixtures/content/blog/2024-05-22-test.md similarity index 100% rename from tests/content/blog/2024-05-22-test.md rename to tests/fixtures/content/blog/2024-05-22-test.md diff --git a/tests/content/blog/2024-05-24-another-test.md b/tests/fixtures/content/blog/2024-05-24-another-test.md similarity index 100% rename from tests/content/blog/2024-05-24-another-test.md rename to tests/fixtures/content/blog/2024-05-24-another-test.md diff --git a/tests/content/blog/2024-05-26-yet-another-test.md b/tests/fixtures/content/blog/2024-05-26-yet-another-test.md similarity index 100% rename from tests/content/blog/2024-05-26-yet-another-test.md rename to tests/fixtures/content/blog/2024-05-26-yet-another-test.md diff --git a/tests/content/pages/folder/test.md b/tests/fixtures/content/pages/folder/test.md similarity index 100% rename from tests/content/pages/folder/test.md rename to tests/fixtures/content/pages/folder/test.md diff --git a/tests/content/pages/test.md b/tests/fixtures/content/pages/test.md similarity index 100% rename from tests/content/pages/test.md rename to tests/fixtures/content/pages/test.md diff --git a/tests/content/pages/test2.md b/tests/fixtures/content/pages/test2.md similarity index 100% rename from tests/content/pages/test2.md rename to tests/fixtures/content/pages/test2.md diff --git a/tests/content/pages/test3.md b/tests/fixtures/content/pages/test3.md similarity index 100% rename from tests/content/pages/test3.md rename to tests/fixtures/content/pages/test3.md