Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Add withoutMix and withMix test helpers #30900

Merged
merged 1 commit into from Dec 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}
}