Skip to content

Commit

Permalink
Refactor test to mock the filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Sep 9, 2024
1 parent cd883e8 commit 06a2bd8
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions packages/framework/tests/Unit/Pages/PageModelGetHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Hyde\Framework\Testing\Unit\Pages;

use Hyde\Facades\Filesystem;
use Hyde\Hyde;
use Hyde\Pages\BladePage;
use Hyde\Pages\DocumentationPage;
use Hyde\Pages\MarkdownPage;
Expand All @@ -19,52 +19,66 @@ class PageModelGetHelperTest extends UnitTestCase
{
protected static bool $needsConfig = true;

/** @var \Illuminate\Filesystem\Filesystem&\Mockery\MockInterface */
protected $filesystem;

protected function setUp(): void
{
self::setupKernel();

$this->filesystem = $this->mockFilesystemStrict()
->shouldReceive('glob')->once()->with(Hyde::path('_pages/{*,**/*}.html'), GLOB_BRACE)->andReturn([])->byDefault()
->shouldReceive('glob')->once()->with(Hyde::path('_pages/{*,**/*}.blade.php'), GLOB_BRACE)->andReturn([])->byDefault()
->shouldReceive('glob')->once()->with(Hyde::path('_pages/{*,**/*}.md'), GLOB_BRACE)->andReturn([])->byDefault()
->shouldReceive('glob')->once()->with(Hyde::path('_posts/{*,**/*}.md'), GLOB_BRACE)->andReturn([])->byDefault()
->shouldReceive('glob')->once()->with(Hyde::path('_docs/{*,**/*}.md'), GLOB_BRACE)->andReturn([])->byDefault();
}

public function testBladePageGetHelperReturnsBladePageCollection()
{
$this->filesystem->shouldReceive('glob')->once()->with(Hyde::path('_pages/{*,**/*}.blade.php'), GLOB_BRACE)->andReturn(['_pages/test-page.blade.php']);
$this->filesystem->shouldReceive('missing')->once()->with(Hyde::path('_pages/test-page.blade.php'))->andReturnFalse();
$this->filesystem->shouldReceive('get')->once()->with(Hyde::path('_pages/test-page.blade.php'))->andReturn('content');

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

public function testMarkdownPageGetHelperReturnsMarkdownPageCollection()
{
Filesystem::touch('_pages/test-page.md');
$this->filesystem->shouldReceive('glob')->once()->with(Hyde::path('_pages/{*,**/*}.md'), GLOB_BRACE)->andReturn(['_pages/test-page.md']);
$this->filesystem->shouldReceive('missing')->once()->with(Hyde::path('_pages/test-page.md'))->andReturnFalse();
$this->filesystem->shouldReceive('get')->once()->with(Hyde::path('_pages/test-page.md'))->andReturn('content');

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

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

public function testMarkdownPostGetHelperReturnsMarkdownPostCollection()
{
Filesystem::touch('_posts/test-post.md');
$this->filesystem->shouldReceive('glob')->once()->with(Hyde::path('_posts/{*,**/*}.md'), GLOB_BRACE)->andReturn(['_posts/test-post.md']);
$this->filesystem->shouldReceive('missing')->once()->with(Hyde::path('_posts/test-post.md'))->andReturnFalse();
$this->filesystem->shouldReceive('get')->once()->with(Hyde::path('_posts/test-post.md'))->andReturn('content');

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

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

public function testDocumentationPageGetHelperReturnsDocumentationPageCollection()
{
Filesystem::touch('_docs/test-page.md');
$this->filesystem->shouldReceive('glob')->once()->with(Hyde::path('_docs/{*,**/*}.md'), GLOB_BRACE)->andReturn(['_docs/test-page.md']);
$this->filesystem->shouldReceive('missing')->once()->with(Hyde::path('_docs/test-page.md'))->andReturnFalse();
$this->filesystem->shouldReceive('get')->once()->with(Hyde::path('_docs/test-page.md'))->andReturn('content');

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

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

0 comments on commit 06a2bd8

Please sign in to comment.