Skip to content

Commit

Permalink
Add withoutMix and withMix test helpers (#30900)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary authored and taylorotwell committed Dec 23, 2019
1 parent 6279233 commit f32662a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
}
29 changes: 29 additions & 0 deletions tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php
@@ -0,0 +1,29 @@
<?php

namespace Illuminate\Tests\Foundation\Testing\Concerns;

use Illuminate\Foundation\Mix;
use Orchestra\Testbench\TestCase;

class InteractsWithContainerTest extends TestCase
{
public function testWithoutMixBindsEmptyHandlerAndReturnsInstance()
{
$instance = $this->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);
}
}

0 comments on commit f32662a

Please sign in to comment.