-
Notifications
You must be signed in to change notification settings - Fork 11k
/
InteractsWithContainer.php
111 lines (97 loc) · 2.56 KB
/
InteractsWithContainer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
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.
*
* @param string $abstract
* @param object $instance
* @return object
*/
protected function swap($abstract, $instance)
{
return $this->instance($abstract, $instance);
}
/**
* Register an instance of an object in the container.
*
* @param string $abstract
* @param object $instance
* @return object
*/
protected function instance($abstract, $instance)
{
$this->app->instance($abstract, $instance);
return $instance;
}
/**
* Mock an instance of an object in the container.
*
* @param string $abstract
* @param \Closure|null $mock
* @return \Mockery\MockInterface
*/
protected function mock($abstract, Closure $mock = null)
{
return $this->instance($abstract, Mockery::mock(...array_filter(func_get_args())));
}
/**
* Mock a partial instance of an object in the container.
*
* @param string $abstract
* @param \Closure|null $mock
* @return \Mockery\MockInterface
*/
protected function partialMock($abstract, Closure $mock = null)
{
return $this->instance($abstract, Mockery::mock(...array_filter(func_get_args()))->makePartial());
}
/**
* Spy an instance of an object in the container.
*
* @param string $abstract
* @param \Closure|null $mock
* @return \Mockery\MockInterface
*/
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;
}
}