DrupalKernelSiteTest.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace Drupal\KernelTests\Core\DrupalKernel;
  3. use Drupal\Core\Site\Settings;
  4. use Drupal\KernelTests\KernelTestBase;
  5. /**
  6. * Tests site-specific service overrides.
  7. *
  8. * @group DrupalKernel
  9. */
  10. class DrupalKernelSiteTest extends KernelTestBase {
  11. /**
  12. * Tests services.yml in site directory.
  13. */
  14. public function testServicesYml() {
  15. $container_yamls = Settings::get('container_yamls');
  16. $container_yamls[] = $this->siteDirectory . '/services.yml';
  17. $this->setSetting('container_yamls', $container_yamls);
  18. $this->assertFalse($this->container->has('site.service.yml'));
  19. // A service provider class always has precedence over services.yml files.
  20. // KernelTestBase::buildContainer() swaps out many services with in-memory
  21. // implementations already, so those cannot be tested.
  22. $this->assertIdentical(get_class($this->container->get('cache.backend.database')), 'Drupal\Core\Cache\DatabaseBackendFactory');
  23. $class = __CLASS__;
  24. $doc = <<<EOD
  25. services:
  26. # Add a new service.
  27. site.service.yml:
  28. class: $class
  29. # Swap out a core service.
  30. cache.backend.database:
  31. class: Drupal\Core\Cache\MemoryBackendFactory
  32. EOD;
  33. file_put_contents($this->siteDirectory . '/services.yml', $doc);
  34. // Rebuild the container.
  35. $this->container->get('kernel')->rebuildContainer();
  36. $this->assertTrue($this->container->has('site.service.yml'));
  37. $this->assertIdentical(get_class($this->container->get('cache.backend.database')), 'Drupal\Core\Cache\MemoryBackendFactory');
  38. }
  39. }