diff --git a/tests/system/CommonSingleServiceTest.php b/tests/system/CommonSingleServiceTest.php index ede9d9b0445e..bc2f4a1f60c8 100644 --- a/tests/system/CommonSingleServiceTest.php +++ b/tests/system/CommonSingleServiceTest.php @@ -11,6 +11,7 @@ namespace CodeIgniter; +use CodeIgniter\Autoloader\FileLocator; use CodeIgniter\Config\Services; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\Mock\MockSecurity; @@ -42,6 +43,17 @@ public function testSingleServiceWithNoParamsSupplied(string $service): void */ public function testSingleServiceWithAtLeastOneParamSupplied(string $service): void { + if ($service === 'commands') { + $locator = $this->getMockBuilder(FileLocator::class) + ->setConstructorArgs([Services::autoloader()]) + ->onlyMethods(['listFiles']) + ->getMock(); + + // `Commands::discoverCommand()` is an expensive operation + $locator->method('listFiles')->with('Commands/')->willReturn([]); + Services::injectMock('locator', $locator); + } + $params = []; $method = new ReflectionMethod(Services::class, $service); @@ -52,6 +64,10 @@ public function testSingleServiceWithAtLeastOneParamSupplied(string $service): v $this->assertSame(get_class($service1), get_class($service2)); $this->assertNotSame($service1, $service2); + + if ($service === 'commands') { + $this->resetServices(); + } } public function testSingleServiceWithAllParamsSupplied(): void @@ -76,25 +92,33 @@ public function testSingleServiceWithGibberishGiven(): void public static function serviceNamesProvider(): iterable { - $methods = (new ReflectionClass(Services::class))->getMethods(ReflectionMethod::IS_PUBLIC); - - foreach ($methods as $method) { - $name = $method->getName(); - $excl = [ - '__callStatic', - 'serviceExists', - 'reset', - 'resetSingle', - 'injectMock', - 'encrypter', // Encrypter needs a starter key - 'session', // Headers already sent - ]; - - if (in_array($name, $excl, true)) { - continue; + static $services = []; + static $excl = [ + '__callStatic', + 'serviceExists', + 'reset', + 'resetSingle', + 'injectMock', + 'encrypter', // Encrypter needs a starter key + 'session', // Headers already sent + ]; + + if ($services === []) { + $methods = (new ReflectionClass(Services::class))->getMethods(ReflectionMethod::IS_PUBLIC); + + foreach ($methods as $method) { + $name = $method->getName(); + + if (in_array($name, $excl, true)) { + continue; + } + + $services[$name] = [$name]; } - yield $name => [$name]; + ksort($services); } + + yield from $services; } }