Skip to content

Commit

Permalink
Refactor test to be proper unit test with filesystem mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Sep 10, 2024
1 parent 13444a3 commit 66f6a21
Showing 1 changed file with 39 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,85 @@

namespace Hyde\Framework\Testing\Unit\Pages;

use Hyde\Facades\Filesystem;
use Hyde\Hyde;
use Hyde\Pages\BladePage;
use Hyde\Testing\UnitTestCase;
use Hyde\Pages\DocumentationPage;
use Hyde\Pages\MarkdownPage;
use Hyde\Pages\MarkdownPost;
use Hyde\Testing\TestCase;
use Mockery\ExpectationInterface;

/**
* @see \Hyde\Pages\Concerns\HydePage::files()
*/
class PageModelGetAllFilesHelperTest extends TestCase
class PageModelGetAllFilesHelperTest 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('missing')->withAnyArgs()->andReturn(false)->byDefault()
->shouldReceive('get')->withAnyArgs()->andReturn('foo')->byDefault()
->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();
}

protected function tearDown(): void
{
$this->verifyMockeryExpectations();
}

public function testBladePageGetHelperReturnsBladePageArray()
{
$this->shouldReceiveGlob('_pages/{*,**/*}.blade.php')->andReturn(['_pages/test-page.blade.php']);

$array = BladePage::files();
$this->assertCount(2, $array);
$this->assertCount(1, $array);
$this->assertIsArray($array);
$this->assertEquals(['404', 'index'], $array);
$this->assertEquals(['test-page'], $array);
}

public function testMarkdownPageGetHelperReturnsMarkdownPageArray()
{
Filesystem::touch('_pages/test-page.md');
$this->shouldReceiveGlob('_pages/{*,**/*}.md')->andReturn(['_pages/test-page.md']);

$array = MarkdownPage::files();
$this->assertCount(1, $array);
$this->assertIsArray($array);
$this->assertEquals(['test-page'], $array);

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

public function testMarkdownPostGetHelperReturnsMarkdownPostArray()
{
Filesystem::touch('_posts/test-post.md');
$this->shouldReceiveGlob('_posts/{*,**/*}.md')->andReturn(['_posts/test-post.md']);

$array = MarkdownPost::files();
$this->assertCount(1, $array);
$this->assertIsArray($array);
$this->assertEquals(['test-post'], $array);

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

public function testDocumentationPageGetHelperReturnsDocumentationPageArray()
{
Filesystem::touch('_docs/test-page.md');
$this->shouldReceiveGlob('_docs/{*,**/*}.md')->andReturn(['_docs/test-page.md']);

$array = DocumentationPage::files();
$this->assertCount(1, $array);
$this->assertIsArray($array);
$this->assertEquals(['test-page'], $array);
}

Filesystem::unlink('_docs/test-page.md');
protected function shouldReceiveGlob(string $withPath): ExpectationInterface
{
return $this->filesystem->shouldReceive('glob')->once()->with(Hyde::path($withPath), GLOB_BRACE);
}
}

0 comments on commit 66f6a21

Please sign in to comment.