From f7db3b4e8fad34c56831c7aa44f9fb144f582f6b Mon Sep 17 00:00:00 2001 From: Willem Leuverink Date: Tue, 16 Jan 2024 22:58:33 +0100 Subject: [PATCH] add testing fake feature --- src/BundleManager.php | 16 +++++++++++++++- src/Components/Import.php | 3 +-- src/Contracts/BundleManager.php | 4 ++++ src/Traits/Constructable.php | 2 +- tests/Feature/IntegrationTest.php | 7 +++++++ 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/BundleManager.php b/src/BundleManager.php index 9a344ea..4cdb0e1 100644 --- a/src/BundleManager.php +++ b/src/BundleManager.php @@ -2,8 +2,10 @@ namespace Leuverink\Bundle; +use Mockery; use Throwable; use SplFileInfo; +use Mockery\MockInterface; use Illuminate\Http\Response; use Illuminate\Support\Facades\Storage; use Leuverink\Bundle\Traits\Constructable; @@ -49,7 +51,7 @@ public function bundle(string $script): SplFileInfo ); } catch (Throwable $e) { $this->cleanup($file); - throw $e; // TODO: Consider raising a browser console error or whoops in a dialog instead + throw $e; } finally { $this->cleanup($file); } @@ -121,4 +123,16 @@ private function cleanup($file) rmdir($this->tempDisk()->path('')); } } + + public static function fake(): MockInterface + { + $mock = Mockery::mock(BundleManagerContract::class, fn ($mock) => $mock + ->makePartial() + ->shouldReceive('bundle') + ->andReturn(new SplFileInfo('mock.js')) + ->atLeast()->once() + ); + + return app()->instance(BundleManagerContract::class, $mock); + } } diff --git a/src/Components/Import.php b/src/Components/Import.php index b03611d..c84e20e 100644 --- a/src/Components/Import.php +++ b/src/Components/Import.php @@ -11,13 +11,12 @@ class Import extends Component public function __construct( public string $module, public ?string $as = null, - public bool $inline = false // TODO: Implement this + public bool $inline = false ) { } public function render() { - // Bundle it up try { return $this->bundle(); } catch (BundlingFailedException $e) { diff --git a/src/Contracts/BundleManager.php b/src/Contracts/BundleManager.php index 4cbba70..34227e9 100644 --- a/src/Contracts/BundleManager.php +++ b/src/Contracts/BundleManager.php @@ -3,6 +3,7 @@ namespace Leuverink\Bundle\Contracts; use SplFileInfo; +use Mockery\MockInterface; use Illuminate\Http\Response; use Illuminate\Contracts\Filesystem\Filesystem; use Illuminate\Contracts\Config\Repository as RepositoryContract; @@ -31,4 +32,7 @@ public function bundleContents($fileName): Response; /** Hashes a given string */ public function hash($input, $length = 12): string; + + /** Mock for testing */ + public static function fake(): MockInterface; } diff --git a/src/Traits/Constructable.php b/src/Traits/Constructable.php index df9bd26..5e4f4b1 100644 --- a/src/Traits/Constructable.php +++ b/src/Traits/Constructable.php @@ -8,7 +8,7 @@ trait Constructable { - public static function new(): self + public static function new(): mixed { $interfaces = class_implements(static::class); diff --git a/tests/Feature/IntegrationTest.php b/tests/Feature/IntegrationTest.php index 363fe92..fe9dae3 100644 --- a/tests/Feature/IntegrationTest.php +++ b/tests/Feature/IntegrationTest.php @@ -1,6 +1,7 @@ skip('Code splitting not implemented'); +it('can be faked', function () { + BundleManager::fake(); + + Blade::renderComponent(new Import('~/foo', 'bar')); +}); + // Probably not possible. TODO: Create issue in Bun repo // it('imports from node_modules are chunked')->todo(); // it('imports from outside node_modules are inlined (due to issue with Bun)')->todo();