From 92ef8fda77728558065ac4d1725865f5230c6255 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 1 Dec 2024 21:37:00 +0100 Subject: [PATCH] Granular mocks --- .../tests/Unit/IncludesFacadeUnitTest.php | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/packages/framework/tests/Unit/IncludesFacadeUnitTest.php b/packages/framework/tests/Unit/IncludesFacadeUnitTest.php index 05da1556e13..4e0d85ed15f 100644 --- a/packages/framework/tests/Unit/IncludesFacadeUnitTest.php +++ b/packages/framework/tests/Unit/IncludesFacadeUnitTest.php @@ -4,6 +4,7 @@ namespace Hyde\Framework\Testing\Unit; +use Illuminate\Contracts\View\Factory; use Mockery; use Closure; use Hyde\Hyde; @@ -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' => '

Mocked Heading

' - ])); - - app()->instance('view', $viewFactory); - app()->instance(\Illuminate\Contracts\View\Factory::class, $viewFactory); - $this->setupTestKernel()->setRoutes(collect()); } @@ -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)); } @@ -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)); } @@ -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')); @@ -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); }