1111
1212namespace CodeIgniter ;
1313
14+ use CodeIgniter \Autoloader \FileLocator ;
1415use CodeIgniter \Config \Services ;
1516use CodeIgniter \Test \CIUnitTestCase ;
1617use CodeIgniter \Test \Mock \MockSecurity ;
@@ -42,6 +43,17 @@ public function testSingleServiceWithNoParamsSupplied(string $service): void
4243 */
4344 public function testSingleServiceWithAtLeastOneParamSupplied (string $ service ): void
4445 {
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+
4557 $ params = [];
4658 $ method = new ReflectionMethod (Services::class, $ service );
4759
@@ -52,6 +64,10 @@ public function testSingleServiceWithAtLeastOneParamSupplied(string $service): v
5264
5365 $ this ->assertSame (get_class ($ service1 ), get_class ($ service2 ));
5466 $ this ->assertNotSame ($ service1 , $ service2 );
67+
68+ if ($ service === 'commands ' ) {
69+ $ this ->resetServices ();
70+ }
5571 }
5672
5773 public function testSingleServiceWithAllParamsSupplied (): void
@@ -76,25 +92,33 @@ public function testSingleServiceWithGibberishGiven(): void
7692
7793 public static function serviceNamesProvider (): iterable
7894 {
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 ];
95117 }
96118
97- yield $ name => [ $ name ] ;
119+ ksort ( $ services ) ;
98120 }
121+
122+ yield from $ services ;
99123 }
100124}
0 commit comments