@@ -7,22 +7,36 @@ class UnitMockRegistry
7
7
/**
8
8
* @var array
9
9
*/
10
- private static $ mocks = [];
10
+ private $ mocks = [];
11
11
12
- public static function has ( string $ unit ): bool
12
+ public function __construct ()
13
13
{
14
- return isset (self ::$ mocks [$ unit ]);
14
+ // there should only be one instance of the registry,
15
+ // so we register ourselves onto the application to be reused.
16
+ // this is necessary in order to have a clean registry at the beginning of each test method run,
17
+ // otherwise, with a singleton mocks will be carried on across test runs within the same class.
18
+ app ()->instance (static ::class, $ this );
15
19
}
16
20
17
- public static function get (string $ unit ): ? UnitMock
21
+ public function has (string $ unit ): bool
18
22
{
19
- if (!self ::has ($ unit )) return null ;
23
+ return isset ($ this ->mocks [$ unit ]);
24
+ }
25
+
26
+ public function get (string $ unit ): ?UnitMock
27
+ {
28
+ if (!$ this ->has ($ unit )) return null ;
20
29
21
- return self ::$ mocks [$ unit ];
30
+ return $ this ->mocks [$ unit ];
31
+ }
32
+
33
+ public function register (string $ unit , UnitMock $ mock )
34
+ {
35
+ $ this ->mocks [$ unit ] = $ mock ;
22
36
}
23
37
24
- public static function register ( string $ unit , UnitMock $ mock )
38
+ public function count ( )
25
39
{
26
- self :: $ mocks [ $ unit ] = $ mock ;
40
+ return count ( $ this -> mocks ) ;
27
41
}
28
42
}
0 commit comments