Unverified Commit d84c44e5 authored by Alex Pott's avatar Alex Pott
Browse files

task: #3578486 Check aliases match interfaces in AutowireTest

By: longwave
By: smustgrave
(cherry picked from commit 4851e091)
parent 14807990
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ services:
  _defaults:
    autoconfigure: true
  block_content.uuid_lookup:
    class: \Drupal\block_content\BlockContentUuidLookup
    class: Drupal\block_content\BlockContentUuidLookup
    arguments: ['@cache.bootstrap', '@lock', '@entity_type.manager']
    tags:
      - { name: needs_destruction }
+39 −0
Original line number Diff line number Diff line
@@ -135,6 +135,45 @@ public function testCoreServiceAliases(): void {
    $this->assertSame($expected, array_intersect($expected, $aliases), sprintf('The following core services do not have map the class name to an alias. Add the following to core.services.yml in the appropriate place: %s%s%s', \PHP_EOL, \PHP_EOL, $formatted));
  }

  /**
   * Tests that core service aliases match the interfaces they map to.
   */
  public function testCoreServiceAliasInterfaces(): 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);
        }
        else {
          $services[$id] = $service['class'] ?? $id;
        }
      }
    }

    $mismatched = [];
    foreach ($aliases as $alias => $service) {
      $this->assertArrayHasKey($service, $services);

      if (interface_exists($alias)) {
        // The service class must implement the aliased interface.
        if (!is_subclass_of($services[$service], $alias)) {
          $mismatched[] = sprintf('%s aliases to @%s (class %s) which does not implement %s', $alias, $service, $services[$service], $alias);
        }
      }
      else {
        // The service class must be the aliased class or a subclass of it.
        if ($services[$service] !== $alias) {
          $mismatched[] = sprintf('%s aliases to @%s (class %s) which is not an instance of %s', $alias, $service, $services[$service], $alias);
        }
      }
    }

    $this->assertEmpty($mismatched, 'The following core service aliases do not match their interfaces:' . PHP_EOL . implode(PHP_EOL, $mismatched));
  }

  /**
   * Tests that core controllers are autowired where possible.
   */