Repository navigation
Expand file tree
/
Copy pathInteractsWithContainer.php
More file actions
116 lines (101 loc) · 2.85 KB
/
Copy pathInteractsWithContainer.php
File metadata and controls
116 lines (101 loc) · 2.85 KB
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
112
113
114
115
116
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Testing\Concerns;
use Closure;
use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\ApplicationInterface;
use Hyperf\Di\Container;
use Hyperf\Di\Definition\DefinitionSourceFactory;
use Mockery;
use Mockery\MockInterface;
use Psr\Container\ContainerInterface;
trait InteractsWithContainer
{
/**
* @var null|Container|ContainerInterface
*/
protected ?ContainerInterface $container = null;
/**
* 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)
{
/* @phpstan-ignore-next-line */
$this->container->set($abstract, $instance);
return $instance;
}
/**
* Mock an instance of an object in the container.
*
* @param string $abstract
* @return 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
* @return 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
* @return MockInterface
*/
protected function spy($abstract, ?Closure $mock = null)
{
return $this->instance($abstract, Mockery::spy(...array_filter(func_get_args())));
}
protected function createContainer(): ContainerInterface
{
return new Container((new DefinitionSourceFactory())());
}
protected function getContainer(): ContainerInterface
{
return $this->container;
}
protected function flushContainer(): void
{
$this->container = null;
}
protected function setContainer(ContainerInterface $container): void
{
$this->container = $container;
}
protected function refreshContainer(): void
{
$this->container = ApplicationContext::setContainer($this->createContainer());
$this->container->get(ApplicationInterface::class);
}
}