Skip to content

Commit

Permalink
Move the static::all() helper to AbstractPage
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Jun 5, 2022
1 parent d866293 commit c726ad7
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 34 deletions.
13 changes: 13 additions & 0 deletions src/Contracts/AbstractPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Hyde\Framework\Contracts;

use Hyde\Framework\Concerns\HasPageMetadata;
use Hyde\Framework\Services\CollectionService;
use Illuminate\Support\Collection;

/**
* To ensure compatability with the Hyde Framework,
Expand All @@ -24,4 +26,15 @@ public function getCurrentPagePath(): string
{
return $this->slug;
}

public static function all(): Collection
{
$collection = new Collection();

foreach (CollectionService::getSourceFileListForModel(static::class) as $filepath) {
$collection->push((new static::$parserClass(basename($filepath, static::$fileExtension)))->get());
}

return $collection;
}
}
6 changes: 0 additions & 6 deletions src/Models/BladePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,4 @@ public function getCurrentPagePath(): string
{
return $this->view;
}

public static function all(): Collection
{
// TODO: Implement all() method.
return new Collection();
}
}
6 changes: 0 additions & 6 deletions src/Models/MarkdownDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,4 @@ public function __construct(array $matter, string $body, string $title = '', str
$this->title = $title;
$this->slug = $slug;
}

public static function all(): Collection
{
// TODO: Implement all() method.
return new Collection();
}
}
6 changes: 0 additions & 6 deletions src/Models/MarkdownPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,4 @@ class MarkdownPage extends MarkdownDocument
{
public static string $sourceDirectory = '_pages';
public static string $parserClass = MarkdownPageParser::class;

public static function all(): Collection
{
// TODO: Implement all() method.
return new Collection();
}
}
13 changes: 0 additions & 13 deletions src/Models/MarkdownPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use Hyde\Framework\Concerns\HasFeaturedImage;
use Hyde\Framework\Hyde;
use Hyde\Framework\Models\Parsers\MarkdownPostParser;
use Hyde\Framework\Services\CollectionService;
use Illuminate\Support\Collection;

class MarkdownPost extends MarkdownDocument
{
Expand Down Expand Up @@ -52,15 +50,4 @@ public function getPostDescription(): string
{
return $this->matter['description'] ?? substr($this->body, 0, 125).'...';
}

public static function all(): Collection
{
$collection = new Collection();

foreach (CollectionService::getSourceFileListForModel(static::class) as $filepath) {
$collection->push((new static::$parserClass(basename($filepath, static::$fileExtension)))->get());
}

return $collection;
}
}
39 changes: 36 additions & 3 deletions tests/Unit/PageModelGetHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,39 @@
namespace Tests\Unit;

use Hyde\Framework\Hyde;
use Hyde\Framework\Models\BladePage;
use Hyde\Framework\Models\DocumentationPage;
use Hyde\Framework\Models\MarkdownPage;
use Hyde\Framework\Models\MarkdownPost;
use Illuminate\Support\Collection;
use Tests\TestCase;

/**
* @covers \Hyde\Framework\Contracts\PageContract::all
* @covers \Hyde\Framework\Concerns\AbstractPage::all
*/
class PageModelGetHelperTest extends TestCase
{
/**
* @covers \Hyde\Framework\Models\MarkdownPost::all
*/
public function test_blade_page_get_helper_returns_blade_page_collection()
{
$collection = BladePage::all();
$this->assertCount(2, $collection);
$this->assertInstanceOf(Collection::class, $collection);
$this->assertContainsOnlyInstancesOf(BladePage::class, $collection);
}

public function test_markdown_page_get_helper_returns_markdown_page_collection()
{
touch(Hyde::path('_pages/test-page.md'));

$collection = MarkdownPage::all();
$this->assertCount(1, $collection);
$this->assertInstanceOf(Collection::class, $collection);
$this->assertContainsOnlyInstancesOf(MarkdownPage::class, $collection);

unlink(Hyde::path('_pages/test-page.md'));
}

public function test_markdown_post_get_helper_returns_markdown_post_collection()
{
touch(Hyde::path('_posts/test-post.md'));
Expand All @@ -26,4 +47,16 @@ public function test_markdown_post_get_helper_returns_markdown_post_collection()

unlink(Hyde::path('_posts/test-post.md'));
}

public function test_documentation_page_get_helper_returns_documentation_page_collection()
{
touch(Hyde::path('_docs/test-page.md'));

$collection = DocumentationPage::all();
$this->assertCount(1, $collection);
$this->assertInstanceOf(Collection::class, $collection);
$this->assertContainsOnlyInstancesOf(DocumentationPage::class, $collection);

unlink(Hyde::path('_docs/test-page.md'));
}
}

0 comments on commit c726ad7

Please sign in to comment.