From 47349a76e9a04f012d03ca27ed3f075ccbcf9b2e Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Sun, 22 Dec 2019 09:05:13 -0500 Subject: [PATCH] Add withoutMix and withMix test helpers --- .../Concerns/InteractsWithContainer.php | 40 +++++++++++++++++++ .../Concerns/InteractsWithContainerTest.php | 29 ++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php index a1bd516b844c..c84852e0040c 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php @@ -3,10 +3,18 @@ namespace Illuminate\Foundation\Testing\Concerns; use Closure; +use Illuminate\Foundation\Mix; use Mockery; trait InteractsWithContainer { + /** + * The original Laravel Mix handler. + * + * @var \Illuminate\Foundation\Mix|null + */ + protected $originalMix; + /** * Register an instance of an object in the container. * @@ -68,4 +76,36 @@ protected function spy($abstract, Closure $mock = null) { return $this->instance($abstract, Mockery::spy(...array_filter(func_get_args()))); } + + /** + * Register an empty handler for Laravel Mix in the container. + * + * @return $this + */ + protected function withoutMix() + { + if ($this->originalMix == null) { + $this->originalMix = app(Mix::class); + } + + $this->swap(Mix::class, function () { + return ''; + }); + + return $this; + } + + /** + * Register an empty handler for Laravel Mix in the container. + * + * @return $this + */ + protected function withMix() + { + if ($this->originalMix) { + $this->app->instance(Mix::class, $this->originalMix); + } + + return $this; + } } diff --git a/tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php b/tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php new file mode 100644 index 000000000000..cfe4460ad07d --- /dev/null +++ b/tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php @@ -0,0 +1,29 @@ +withoutMix(); + + $this->assertSame('', mix('path/to/asset.png')); + $this->assertSame($this, $instance); + } + + public function testWithMixRestoresOriginalHandlerAndReturnsInstance() + { + $handler = new \stdClass(); + $this->app->instance(Mix::class, $handler); + + $this->withoutMix(); + $instance = $this->withMix(); + + $this->assertSame($handler, resolve(Mix::class)); + $this->assertSame($this, $instance); + } +}