Skip to content

Commit

Permalink
Extract trait for shared testing code
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Dec 6, 2024
1 parent ef50028 commit 8b30543
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 58 deletions.
32 changes: 3 additions & 29 deletions packages/framework/tests/Unit/ColoredBlockquoteShortcodesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,15 @@

use Hyde\Markdown\Processing\ColoredBlockquotes;
use Hyde\Testing\UnitTestCase;
use Illuminate\Contracts\View\Factory as FactoryContract;
use Illuminate\Events\Dispatcher;
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Engines\CompilerEngine;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\Factory;
use Illuminate\View\FileViewFinder;
use Hyde\Testing\UsesRealBladeInUnitTests;

/**
* @covers \Hyde\Markdown\Processing\ColoredBlockquotes
*/
class ColoredBlockquoteShortcodesTest extends UnitTestCase
{
use UsesRealBladeInUnitTests;

protected static bool $needsKernel = true;
protected static bool $needsConfig = true;

Expand Down Expand Up @@ -116,25 +111,4 @@ public static function blockquoteProvider(): array
],
];
}

// Todo: Extract trait for this and MarkdownHeadingRendererUnitTest
protected function createRealBladeCompilerEnvironment(): void
{
$resolver = new EngineResolver();
$filesystem = new Filesystem();

$resolver->register('blade', function () use ($filesystem) {
return new CompilerEngine(
new BladeCompiler($filesystem, sys_get_temp_dir())
);
});

$finder = new FileViewFinder($filesystem, [realpath(__DIR__.'/../../resources/views')]);
$finder->addNamespace('hyde', realpath(__DIR__.'/../../resources/views'));

$view = new Factory($resolver, $finder, new Dispatcher());

app()->instance('view', $view);
app()->instance(FactoryContract::class, $view);
}
}
31 changes: 3 additions & 28 deletions packages/framework/tests/Unit/HeadingRendererUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@
use Hyde\Pages\DocumentationPage;
use Hyde\Pages\MarkdownPage;
use Hyde\Testing\UnitTestCase;
use Illuminate\Contracts\View\Factory as FactoryContract;
use Illuminate\Events\Dispatcher;
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Engines\CompilerEngine;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\Factory;
use Illuminate\View\FileViewFinder;
use Hyde\Testing\UsesRealBladeInUnitTests;
use InvalidArgumentException;
use League\CommonMark\Extension\CommonMark\Node\Block\Heading;
use League\CommonMark\Node\Node;
Expand All @@ -29,6 +22,8 @@
*/
class HeadingRendererUnitTest extends UnitTestCase
{
use UsesRealBladeInUnitTests;

protected static bool $needsConfig = true;
protected static ?array $cachedConfig = null;

Expand Down Expand Up @@ -238,26 +233,6 @@ public function testPostProcessHandlesNoHeadingTags()
$this->assertSame('<p>Paragraph</p>', (new HeadingRenderer())->postProcess($html));
}

protected function createRealBladeCompilerEnvironment(): void
{
$resolver = new EngineResolver();
$filesystem = new Filesystem();

$resolver->register('blade', function () use ($filesystem) {
return new CompilerEngine(
new BladeCompiler($filesystem, sys_get_temp_dir())
);
});

$finder = new FileViewFinder($filesystem, [realpath(__DIR__.'/../../resources/views')]);
$finder->addNamespace('hyde', realpath(__DIR__.'/../../resources/views'));

$view = new Factory($resolver, $finder, new Dispatcher());

app()->instance('view', $view);
app()->instance(FactoryContract::class, $view);
}

protected function mockChildNodeRenderer(string $contents = 'Test Heading'): ChildNodeRendererInterface
{
$childRenderer = Mockery::mock(ChildNodeRendererInterface::class);
Expand Down
29 changes: 28 additions & 1 deletion packages/testing/src/UsesRealBladeInUnitTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,34 @@

namespace Hyde\Testing;

use Illuminate\Contracts\View\Factory as FactoryContract;
use Illuminate\Events\Dispatcher;
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Engines\CompilerEngine;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\Factory;
use Illuminate\View\FileViewFinder;

trait UsesRealBladeInUnitTests
{
//
protected function createRealBladeCompilerEnvironment(): void
{
$resolver = new EngineResolver();
$filesystem = new Filesystem();

$resolver->register('blade', function () use ($filesystem) {
return new CompilerEngine(
new BladeCompiler($filesystem, sys_get_temp_dir())
);
});

$finder = new FileViewFinder($filesystem, [realpath(__DIR__.'/../../resources/views')]);
$finder->addNamespace('hyde', realpath(__DIR__.'/../../resources/views'));

$view = new Factory($resolver, $finder, new Dispatcher());

app()->instance('view', $view);
app()->instance(FactoryContract::class, $view);
}
}

0 comments on commit 8b30543

Please sign in to comment.