Skip to content

Commit

Permalink
Granular mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Dec 1, 2024
1 parent a6bcaa1 commit 92ef8fd
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions packages/framework/tests/Unit/IncludesFacadeUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Hyde\Framework\Testing\Unit;

use Illuminate\Contracts\View\Factory;
use Mockery;
use Closure;
use Hyde\Hyde;
Expand Down Expand Up @@ -34,16 +35,6 @@ protected function setUp(): void
{
Blade::swap(Mockery::mock());

$viewFactory = Mockery::mock(\Illuminate\Contracts\View\Factory::class);
$viewFactory->shouldReceive('make')
->with('hyde::components.markdown-heading', Mockery::any())
->andReturn(Mockery::mock([
'render' => '<h1>Mocked Heading</h1>'
]));

app()->instance('view', $viewFactory);
app()->instance(\Illuminate\Contracts\View\Factory::class, $viewFactory);

$this->setupTestKernel()->setRoutes(collect());
}

Expand Down Expand Up @@ -160,6 +151,8 @@ public function testMarkdownReturnsRenderedPartial()
$filesystem->shouldReceive('get')->with($this->includesPath($filename))->andReturn($content);
});

$this->mockViewFactory($expected);

$this->assertHtmlStringIsSame($expected, Includes::markdown($filename));
}

Expand All @@ -173,6 +166,8 @@ public function testMarkdownReturnsRenderedDefaultValueWhenNotFound()
$filesystem->shouldReceive('exists')->with($this->includesPath($filename))->andReturn(false);
});

$this->mockViewFactory($expected);

$this->assertNull(Includes::markdown($filename));
$this->assertHtmlStringIsSame($expected, Includes::markdown($filename, $default));
}
Expand All @@ -189,6 +184,8 @@ public function testMarkdownWithAndWithoutExtension()
$filesystem->shouldReceive('get')->with($this->includesPath($filename))->andReturn($content);
});

$this->mockViewFactory($expected);

$this->assertHtmlStringIsSame($expected, Includes::markdown('foo.md'));
$this->assertHtmlStringIsSame(Includes::markdown('foo.md'), Includes::markdown('foo'));
$this->assertHtmlStringIsSame(Includes::markdown('foo.md'), Includes::markdown('foo.md'));
Expand Down Expand Up @@ -252,7 +249,19 @@ protected function mockFilesystemFromClosure(Closure $config): void
app()->instance(Filesystem::class, $filesystem);
}

protected function includesPath(string $filename): string

protected function mockViewFactory(string $result): void
{
$viewFactory = Mockery::mock(Factory::class);
$viewFactory->shouldReceive('make')
->andReturn(Mockery::mock([
'render' => $result
]))->byDefault();

app()->instance('view', $viewFactory);
app()->instance(Factory::class, $viewFactory);
}
protected function includesPath(string $filename): string
{
return Hyde::path('resources/includes/'.$filename);
}
Expand Down

0 comments on commit 92ef8fd

Please sign in to comment.