function AutowireTest::testCoreAutowiring
Tests that core services have aliases correctly defined where possible.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ DependencyInjection/ AutowireTest.php, line 187
Class
- AutowireTest
- Tests auto-wiring services.
Namespace
Drupal\KernelTests\Core\DependencyInjectionCode
public function testCoreAutowiring() : void {
$services = [];
$aliases = [];
foreach ($this->getCoreServiceFiles() as $filename) {
foreach (Yaml::decode(file_get_contents($filename))['services'] ?? [] as $id => $service) {
if (is_string($service)) {
$aliases[$id] = substr($service, 1);
}
elseif (isset($service['class']) && isset($service['arguments'])) {
if ($filename === 'core/core.services.yml') {
// @todo Remove this skip in https://www.drupal.org/i/3295751
continue;
}
$services[$id] = $service;
}
}
}
$autowire = [];
foreach ($services as $id => $service) {
if (!method_exists($service['class'], '__construct')) {
continue;
}
$constructor = new \ReflectionMethod($service['class'], '__construct');
foreach ($constructor->getParameters() as $pos => $parameter) {
$interface = (string) $parameter->getType();
if (!isset($aliases[$interface])) {
// There is no service to autowire.
continue 2;
}
if ($aliases[$interface] !== substr($service['arguments'][$pos], 1)) {
// The service is different.
continue 2;
}
}
$autowire[] = $id;
}
$this->assertEmpty($autowire, 'The following core services can be autowired. Remove their arguments from the services.yml file:' . PHP_EOL . implode(PHP_EOL, $autowire));
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.