Skip to content

Commit

Permalink
feat: converted MarkdownWithDateModel abstract class to a trait `Wi…
Browse files Browse the repository at this point in the history
…thDate` + 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
  • Loading branch information
thebatclaudio committed May 29, 2024
1 parent 43c9227 commit d3dade5
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 45 deletions.
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<a href="https://img.shields.io/github/actions/workflow/status/thebatclaudio/laravel-eloquent-markdown/tests.yml?branch=main&label=tests&style=flat-square"><img src="https://img.shields.io/github/actions/workflow/status/thebatclaudio/laravel-eloquent-markdown/tests.yml?branch=main&label=tests&style=flat-square" alt="GitHub Workflow Status" /></a>
</p>

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

Expand Down Expand Up @@ -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
{
}
```
Expand Down Expand Up @@ -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;
}
```

Expand All @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace TheBatClaudio\EloquentMarkdown\Models;
namespace TheBatClaudio\EloquentMarkdown\Models\Traits;

use Carbon\Carbon;
use Illuminate\Support\Str;
Expand All @@ -13,7 +13,7 @@
* @property Carbon $date
* @property string $slug
*/
abstract class MarkdownWithDateModel extends MarkdownModel
trait WithDate
{
public static function extractDate(string $filePath): Carbon
{
Expand Down
23 changes: 23 additions & 0 deletions tests/Support/Fixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace TheBatClaudio\EloquentMarkdown\Tests\Support;

use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Facades\Storage;

class Fixture
{
protected static ?Filesystem $filesystem = null;

public static function getFilesystem(): Filesystem
{
if (! static::$filesystem) {
static::$filesystem = Storage::build([
'driver' => 'local',
'root' => './tests/fixtures/content/',
]);
}

return static::$filesystem;
}
}
13 changes: 13 additions & 0 deletions tests/Support/Models/TestModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace TheBatClaudio\EloquentMarkdown\Tests\Support\Models;

use TheBatClaudio\EloquentMarkdown\Models\MarkdownModel;

class TestModel extends MarkdownModel
{
protected static function getContentPath(): string
{
return 'pages';
}
}
16 changes: 16 additions & 0 deletions tests/Support/Models/TestWithDateModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace TheBatClaudio\EloquentMarkdown\Tests\Support\Models;

use TheBatClaudio\EloquentMarkdown\Models\MarkdownModel;
use TheBatClaudio\EloquentMarkdown\Models\Traits\WithDate;

class TestWithDateModel extends MarkdownModel
{
use WithDate;

protected static function getContentPath(): string
{
return 'blog';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,13 @@

declare(strict_types=1);

use Illuminate\Filesystem\FilesystemManager;
use TheBatClaudio\EloquentMarkdown\Models\MarkdownModel;

class TestModel extends MarkdownModel
{
protected static function getContentPath(): string
{
return 'pages';
}
}
use TheBatClaudio\EloquentMarkdown\Tests\Support\Fixture;
use TheBatClaudio\EloquentMarkdown\Tests\Support\Models\TestModel;

beforeEach(function () {
TestModel::setFilesystem(
(new FilesystemManager(null))
->createLocalDriver([
'root' => __DIR__.'/../content',
])
Fixture::getFilesystem()
);
});

Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();
});
Expand All @@ -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');
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit d3dade5

Please sign in to comment.