11
11
12
12
namespace CodeIgniter ;
13
13
14
+ use CodeIgniter \Autoloader \FileLocator ;
14
15
use CodeIgniter \Config \Services ;
15
16
use CodeIgniter \Test \CIUnitTestCase ;
16
17
use CodeIgniter \Test \Mock \MockSecurity ;
@@ -42,6 +43,17 @@ public function testSingleServiceWithNoParamsSupplied(string $service): void
42
43
*/
43
44
public function testSingleServiceWithAtLeastOneParamSupplied (string $ service ): void
44
45
{
46
+ if ($ service === 'commands ' ) {
47
+ $ locator = $ this ->getMockBuilder (FileLocator::class)
48
+ ->setConstructorArgs ([Services::autoloader ()])
49
+ ->onlyMethods (['listFiles ' ])
50
+ ->getMock ();
51
+
52
+ // `Commands::discoverCommand()` is an expensive operation
53
+ $ locator ->method ('listFiles ' )->with ('Commands/ ' )->willReturn ([]);
54
+ Services::injectMock ('locator ' , $ locator );
55
+ }
56
+
45
57
$ params = [];
46
58
$ method = new ReflectionMethod (Services::class, $ service );
47
59
@@ -52,6 +64,10 @@ public function testSingleServiceWithAtLeastOneParamSupplied(string $service): v
52
64
53
65
$ this ->assertSame (get_class ($ service1 ), get_class ($ service2 ));
54
66
$ this ->assertNotSame ($ service1 , $ service2 );
67
+
68
+ if ($ service === 'commands ' ) {
69
+ $ this ->resetServices ();
70
+ }
55
71
}
56
72
57
73
public function testSingleServiceWithAllParamsSupplied (): void
@@ -76,25 +92,33 @@ public function testSingleServiceWithGibberishGiven(): void
76
92
77
93
public static function serviceNamesProvider (): iterable
78
94
{
79
- $ methods = (new ReflectionClass (Services::class))->getMethods (ReflectionMethod::IS_PUBLIC );
80
-
81
- foreach ($ methods as $ method ) {
82
- $ name = $ method ->getName ();
83
- $ excl = [
84
- '__callStatic ' ,
85
- 'serviceExists ' ,
86
- 'reset ' ,
87
- 'resetSingle ' ,
88
- 'injectMock ' ,
89
- 'encrypter ' , // Encrypter needs a starter key
90
- 'session ' , // Headers already sent
91
- ];
92
-
93
- if (in_array ($ name , $ excl , true )) {
94
- continue ;
95
+ static $ services = [];
96
+ static $ excl = [
97
+ '__callStatic ' ,
98
+ 'serviceExists ' ,
99
+ 'reset ' ,
100
+ 'resetSingle ' ,
101
+ 'injectMock ' ,
102
+ 'encrypter ' , // Encrypter needs a starter key
103
+ 'session ' , // Headers already sent
104
+ ];
105
+
106
+ if ($ services === []) {
107
+ $ methods = (new ReflectionClass (Services::class))->getMethods (ReflectionMethod::IS_PUBLIC );
108
+
109
+ foreach ($ methods as $ method ) {
110
+ $ name = $ method ->getName ();
111
+
112
+ if (in_array ($ name , $ excl , true )) {
113
+ continue ;
114
+ }
115
+
116
+ $ services [$ name ] = [$ name ];
95
117
}
96
118
97
- yield $ name => [ $ name ] ;
119
+ ksort ( $ services ) ;
98
120
}
121
+
122
+ yield from $ services ;
99
123
}
100
124
}
0 commit comments