hasContainer example

/** * Magic method implementation to unserialize the extension object. */
  public function __wakeup() {
    // Get the app root from the container. While compiling the container we     // have to discover all the extension service files in     // \Drupal\Core\DrupalKernel::initializeServiceProviders(). This results in     // creating extension objects before the container has the kernel.     // Specifically, this occurs during the call to     // \Drupal\Core\Extension\ExtensionDiscovery::scanDirectory().     $container = \Drupal::hasContainer() ? \Drupal::getContainer() : FALSE;
    $this->root = $container && $container->hasParameter('app.root') ? $container->getParameter('app.root') : DRUPAL_ROOT;
  }

  /** * Checks if an extension is marked as experimental. * * @return bool * TRUE if an extension is marked as experimental, FALSE otherwise. */
  public function isExperimental(): bool {
    // Currently, this function checks for both the key/value pairs
$this->accessManager->expects($this->any())
      ->method('checkNamedRoute')
      ->willReturnMap($access_manager_map);

    $this->factory->expects($this->any())
      ->method('createInstance')
      ->willReturnMap($map);
  }

  protected function setupNullCacheabilityMetadataValidation() {
    $container = \Drupal::hasContainer() ? \Drupal::getContainer() : new ContainerBuilder();

    $cache_context_manager = $this->prophesize(CacheContextsManager::class);

    foreach ([NULL, ['user.permissions']['route']['route', 'context.example1']['context.example1', 'route']['route', 'context.example1', 'context.example2']['context.example1', 'context.example2', 'route']['route', 'context.example1', 'context.example2', 'user.permissions']] as $argument) {
      $cache_context_manager->assertValidTokens($argument)->willReturn(TRUE);
    }

    $container->set('cache_contexts_manager', $cache_context_manager->reveal());
    \Drupal::setContainer($container);
  }

}
/** * Indicates if a service is defined in the container. * * @param string $id * The ID of the service to check. * * @return bool * TRUE if the specified service exists, FALSE otherwise. */
  public static function hasService($id) {
    // Check hasContainer() first in order to always return a Boolean.     return static::hasContainer() && static::getContainer()->has($id);
  }

  /** * Gets the app root. * * @return string */
  public static function root() {
    return static::getContainer()->getParameter('app.root');
  }

  
'executed' => $this->executed,
    ];
    return ['serializationData'];
  }

  /** * Magic method implementation to unserialize the view executable. */
  public function __wakeup() {
    // There are cases, like in testing where we don't have a container     // available.     if (\Drupal::hasContainer() && !empty($this->serializationData)) {
      // Load and reference the storage.       $this->storage = \Drupal::entityTypeManager()->getStorage('view')
        ->load($this->serializationData['storage']);
      $this->storage->set('executable', $this);

      // Attach all necessary services.       $this->user = \Drupal::currentUser();
      $this->viewsData = \Drupal::service('views.views_data');
      $this->routeProvider = \Drupal::service('router.route_provider');

      // Restore the state of this executable.
/** * @covers ::id * @covers ::setInitialAccountId */
  public function testId() {
    $dispatcher = $this->prophesize(EventDispatcherInterface::class);
    $dispatcher->dispatch(Argument::any(), Argument::any())->willReturn(new Event());
    $account_proxy = new AccountProxy($dispatcher->reveal());
    $this->assertSame(0, $account_proxy->id());
    $account_proxy->setInitialAccountId(1);
    $this->assertFalse(\Drupal::hasContainer());
    // If the following call loaded the user entity it would call     // AccountProxy::loadUserEntity() which would fail because the container     // does not exist.     $this->assertSame(1, $account_proxy->id());
    $current_user = $this->prophesize(AccountInterface::class);
    $current_user->id()->willReturn(2);
    $account_proxy->setAccount($current_user->reveal());
    $this->assertSame(2, $account_proxy->id());
  }

  /** * @covers ::setInitialAccountId */
return array_keys($vars);
  }

  /** * {@inheritdoc} */
  #[\ReturnTypeWillChange]   public function __wakeup() {
    // Tests in isolation potentially unserialize in the parent process.     $phpunit_bootstrap = isset($GLOBALS['__PHPUNIT_BOOTSTRAP']);
    if ($phpunit_bootstrap && !\Drupal::hasContainer()) {
      return;
    }
    $container = \Drupal::getContainer();
    foreach ($this->_serviceIds as $key => $service_id) {
      // In rare cases, when test data is serialized in the parent process,       // there is a service container but it doesn't contain all expected       // services. To avoid fatal errors during the wrap-up of failing tests, we       // check for this case, too.       if ($phpunit_bootstrap && !$container->has($service_id)) {
        continue;
      }
      
$this->assertEquals(10, $query->getCacheMaxAge());
  }

  /** * Sets up the views data in the container. * * @param \Drupal\views\ViewsData $views_data * The views data. */
  protected function setupViewsData(ViewsData $views_data) {
    $container = \Drupal::hasContainer() ? \Drupal::getContainer() : new ContainerBuilder();
    $container->set('views.views_data', $views_data);
    \Drupal::setContainer($container);
  }

  /** * Sets up the entity type manager in the container. * * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. */
  protected function setupEntityTypeManager(EntityTypeManagerInterface $entity_type_manager) {
    
Home | Imprint | This part of the site doesn't use cookies.