44
55namespace GeekCell \Facade ;
66
7- use Mockery ;
87use Psr \Container \ContainerExceptionInterface ;
98use Psr \Container \ContainerInterface ;
109use Psr \Container \NotFoundExceptionInterface ;
@@ -24,7 +23,7 @@ abstract class Facade
2423 /**
2524 * @var array<object>
2625 */
27- protected static array $ resolvedInstances = [];
26+ protected static array $ swappedInstances = [];
2827
2928 /**
3029 * Return the underlying instance behind the facade.
@@ -39,8 +38,8 @@ abstract class Facade
3938 public static function getFacadeRoot (): object
4039 {
4140 $ accessor = static ::getFacadeAccessor ();
42- if (isset (static ::$ resolvedInstances [$ accessor ])) {
43- return static ::$ resolvedInstances [$ accessor ];
41+ if (isset (static ::$ swappedInstances [$ accessor ])) {
42+ return static ::$ swappedInstances [$ accessor ];
4443 }
4544
4645 if (!isset (static ::$ container )) {
@@ -50,12 +49,10 @@ public static function getFacadeRoot(): object
5049 $ instance = static ::$ container ->get ($ accessor );
5150 if (!is_object ($ instance )) {
5251 throw new \UnexpectedValueException (
53- sprintf ('The entry for "%s" must return an object. ' , $ accessor ),
52+ sprintf ('The entry for "%s" must return an object. Got: %s ' , $ accessor, get_debug_type ( $ instance ) ),
5453 );
5554 }
5655
57- self ::$ resolvedInstances [$ accessor ] = $ instance ;
58-
5956 return $ instance ;
6057 }
6158
@@ -88,57 +85,22 @@ public static function __callStatic(string $method, array $args): mixed
8885 */
8986 public static function clear (): void
9087 {
91- static ::$ resolvedInstances = [];
88+ static ::$ swappedInstances = [];
9289 static ::$ container = null ;
9390 }
9491
9592 /**
96- * Return the class name for the underlying instance behind the facade.
97- *
98- * @return string
99- */
100- public static function getMockableClass (): string
101- {
102- $ instance = static ::getFacadeRoot ();
103- return get_class ($ instance );
104- }
105-
106- /**
107- * Return a Mockery mock instance of the underlying instance behind the facade.
108- *
109- * @return Mockery\MockInterface
110- *
111- * @throws NotFoundExceptionInterface
112- * @throws ContainerExceptionInterface
113- * @throws \LogicException
114- * @throws \UnexpectedValueException
115- */
116- public static function createMock (): Mockery \MockInterface
117- {
118- $ classToMock = static ::getMockableClass ();
119- return Mockery::mock ($ classToMock );
120- }
121-
122- /**
123- * Return a Mockery mock instance, but also swap the underlying instance behind the facade
124- * so consecutive calls to the facade will use the mock.
93+ * @template T of object
12594 *
126- * This behavior can be reset by calling Facade::clear().
127- *
128- * @return Mockery\MockInterface
129- *
130- * @throws NotFoundExceptionInterface
131- * @throws ContainerExceptionInterface
132- * @throws \LogicException
133- * @throws \UnexpectedValueException
95+ * @param T $newInstance
96+ * @return T
13497 */
135- public static function swapMock (): Mockery \ MockInterface
98+ public static function swap ( $ newInstance )
13699 {
137100 $ accessor = static ::getFacadeAccessor ();
138- $ mock = static ::createMock ();
139- static ::$ resolvedInstances [$ accessor ] = $ mock ;
101+ static ::$ swappedInstances [$ accessor ] = $ newInstance ;
140102
141- return $ mock ;
103+ return $ newInstance ;
142104 }
143105
144106 /**
0 commit comments