Skip to content

Commit 4671341

Browse files
authored
Merge pull request #5258 from paulbalandan/common-single-service
Speed up `CommonSingleServiceTest`
2 parents dbbdd2d + d7364cd commit 4671341

File tree

1 file changed

+41
-17
lines changed

1 file changed

+41
-17
lines changed

tests/system/CommonSingleServiceTest.php

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CodeIgniter;
1313

14+
use CodeIgniter\Autoloader\FileLocator;
1415
use CodeIgniter\Config\Services;
1516
use CodeIgniter\Test\CIUnitTestCase;
1617
use 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

Comments
 (0)