You are here

protected function ContainerTest::getMockContainerDefinition in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Component/DependencyInjection/ContainerTest.php \Drupal\Tests\Component\DependencyInjection\ContainerTest::getMockContainerDefinition()

Gets a mock container definition.

Return value

array Associated array with parameters and services.

3 calls to ContainerTest::getMockContainerDefinition()
ContainerTest::setUp in core/tests/Drupal/Tests/Component/DependencyInjection/ContainerTest.php
ContainerTest::testConstruct in core/tests/Drupal/Tests/Component/DependencyInjection/ContainerTest.php
Tests that passing a non-supported format throws an InvalidArgumentException.
PhpArrayContainerTest::setUp in core/tests/Drupal/Tests/Component/DependencyInjection/PhpArrayContainerTest.php

File

core/tests/Drupal/Tests/Component/DependencyInjection/ContainerTest.php, line 719
Contains \Drupal\Tests\Component\DependencyInjection\ContainerTest.

Class

ContainerTest
@coversDefaultClass \Drupal\Component\DependencyInjection\Container @group DependencyInjection

Namespace

Drupal\Tests\Component\DependencyInjection

Code

protected function getMockContainerDefinition() {
  $fake_service = new \stdClass();
  $parameters = array();
  $parameters['some_parameter_class'] = get_class($fake_service);
  $parameters['some_private_config'] = 'really_private_lama';
  $parameters['some_config'] = 'foo';
  $parameters['some_other_config'] = 'lama';
  $parameters['factory_service_class'] = get_class($fake_service);

  // Also test alias resolving.
  $parameters['service_from_parameter'] = $this
    ->getServiceCall('service.provider_alias');
  $services = array();
  $services['service_container'] = array(
    'class' => '\\Drupal\\service_container\\DependencyInjection\\Container',
  );
  $services['other.service'] = array(
    'class' => get_class($fake_service),
  );
  $services['non_shared_service'] = array(
    'class' => get_class($fake_service),
    'shared' => FALSE,
  );
  $services['other.service_class_from_parameter'] = array(
    'class' => $this
      ->getParameterCall('some_parameter_class'),
  );
  $services['late.service'] = array(
    'class' => get_class($fake_service),
  );
  $services['service.provider'] = array(
    'class' => '\\Drupal\\Tests\\Component\\DependencyInjection\\MockService',
    'arguments' => $this
      ->getCollection(array(
      $this
        ->getServiceCall('other.service'),
      $this
        ->getParameterCall('some_config'),
    )),
    'properties' => $this
      ->getCollection(array(
      '_someProperty' => 'foo',
    )),
    'calls' => array(
      array(
        'setContainer',
        $this
          ->getCollection(array(
          $this
            ->getServiceCall('service_container'),
        )),
      ),
      array(
        'setOtherConfigParameter',
        $this
          ->getCollection(array(
          $this
            ->getParameterCall('some_other_config'),
        )),
      ),
    ),
    'priority' => 0,
  );

  // Test private services.
  $private_service = array(
    'class' => '\\Drupal\\Tests\\Component\\DependencyInjection\\MockService',
    'arguments' => $this
      ->getCollection(array(
      $this
        ->getServiceCall('other.service'),
      $this
        ->getParameterCall('some_private_config'),
    )),
    'public' => FALSE,
  );
  $services['service_using_private'] = array(
    'class' => '\\Drupal\\Tests\\Component\\DependencyInjection\\MockService',
    'arguments' => $this
      ->getCollection(array(
      $this
        ->getPrivateServiceCall(NULL, $private_service),
      $this
        ->getParameterCall('some_config'),
    )),
  );
  $services['another_service_using_private'] = array(
    'class' => '\\Drupal\\Tests\\Component\\DependencyInjection\\MockService',
    'arguments' => $this
      ->getCollection(array(
      $this
        ->getPrivateServiceCall(NULL, $private_service),
      $this
        ->getParameterCall('some_config'),
    )),
  );

  // Test shared private services.
  $id = 'private_service_shared_1';
  $services['service_using_shared_private'] = array(
    'class' => '\\Drupal\\Tests\\Component\\DependencyInjection\\MockService',
    'arguments' => $this
      ->getCollection(array(
      $this
        ->getPrivateServiceCall($id, $private_service, TRUE),
      $this
        ->getParameterCall('some_config'),
    )),
  );
  $services['another_service_using_shared_private'] = array(
    'class' => '\\Drupal\\Tests\\Component\\DependencyInjection\\MockService',
    'arguments' => $this
      ->getCollection(array(
      $this
        ->getPrivateServiceCall($id, $private_service, TRUE),
      $this
        ->getParameterCall('some_config'),
    )),
  );

  // Tests service with invalid argument.
  $services['invalid_argument_service'] = array(
    'class' => '\\Drupal\\Tests\\Component\\DependencyInjection\\MockService',
    'arguments' => $this
      ->getCollection(array(
      1,
      // Test passing non-strings, too.
      (object) array(
        'type' => 'invalid',
      ),
    )),
  );
  $services['invalid_arguments_service'] = array(
    'class' => '\\Drupal\\Tests\\Component\\DependencyInjection\\MockService',
    'arguments' => (object) array(
      'type' => 'invalid',
    ),
  );

  // Test service that needs deep-traversal.
  $services['service_using_array'] = array(
    'class' => '\\Drupal\\Tests\\Component\\DependencyInjection\\MockService',
    'arguments' => $this
      ->getCollection(array(
      $this
        ->getCollection(array(
        $this
          ->getServiceCall('other.service'),
      )),
      $this
        ->getParameterCall('some_private_config'),
    )),
  );
  $services['service_with_optional_dependency'] = array(
    'class' => '\\Drupal\\Tests\\Component\\DependencyInjection\\MockService',
    'arguments' => $this
      ->getCollection(array(
      $this
        ->getServiceCall('service.does_not_exist', ContainerInterface::NULL_ON_INVALID_REFERENCE),
      $this
        ->getParameterCall('some_private_config'),
    )),
  );
  $services['factory_service'] = array(
    'class' => '\\Drupal\\service_container\\ServiceContainer\\ControllerInterface',
    'factory' => array(
      $this
        ->getServiceCall('service.provider'),
      'getFactoryMethod',
    ),
    'arguments' => $this
      ->getCollection(array(
      $this
        ->getParameterCall('factory_service_class'),
    )),
  );
  $services['factory_class'] = array(
    'class' => '\\Drupal\\service_container\\ServiceContainer\\ControllerInterface',
    'factory' => '\\Drupal\\Tests\\Component\\DependencyInjection\\MockService::getFactoryMethod',
    'arguments' => array(
      '\\Drupal\\Tests\\Component\\DependencyInjection\\MockService',
      array(
        NULL,
        'bar',
      ),
    ),
    'calls' => array(
      array(
        'setContainer',
        $this
          ->getCollection(array(
          $this
            ->getServiceCall('service_container'),
        )),
      ),
    ),
  );
  $services['wrong_factory'] = array(
    'class' => '\\Drupal\\service_container\\ServiceContainer\\ControllerInterface',
    'factory' => (object) array(
      'I am not a factory, but I pretend to be.',
    ),
  );
  $services['circular_dependency'] = array(
    'class' => '\\Drupal\\Tests\\Component\\DependencyInjection\\MockService',
    'arguments' => $this
      ->getCollection(array(
      $this
        ->getServiceCall('circular_dependency'),
    )),
  );
  $services['synthetic'] = array(
    'synthetic' => TRUE,
  );

  // The file could have been named as a .php file. The reason it is a .data
  // file is that SimpleTest tries to load it. SimpleTest does not like such
  // fixtures and hence we use a neutral name like .data.
  $services['container_test_file_service_test'] = array(
    'class' => '\\stdClass',
    'file' => __DIR__ . '/Fixture/container_test_file_service_test_service_function.data',
  );

  // Test multiple arguments.
  $args = array();
  for ($i = 0; $i < 12; $i++) {
    $services['service_test_instantiation_' . $i] = array(
      'class' => '\\Drupal\\Tests\\Component\\DependencyInjection\\MockInstantiationService',
      // Also test a collection that does not need resolving.
      'arguments' => $this
        ->getCollection($args, FALSE),
    );
    $args[] = 'arg_' . $i;
  }
  $services['service_parameter_not_exists'] = array(
    'class' => '\\Drupal\\Tests\\Component\\DependencyInjection\\MockService',
    'arguments' => $this
      ->getCollection(array(
      $this
        ->getServiceCall('service.provider'),
      $this
        ->getParameterCall('not_exists'),
    )),
  );
  $services['service_dependency_not_exists'] = array(
    'class' => '\\Drupal\\Tests\\Component\\DependencyInjection\\MockService',
    'arguments' => $this
      ->getCollection(array(
      $this
        ->getServiceCall('service_not_exists'),
      $this
        ->getParameterCall('some_config'),
    )),
  );
  $services['service_with_parameter_service'] = array(
    'class' => '\\Drupal\\Tests\\Component\\DependencyInjection\\MockService',
    'arguments' => $this
      ->getCollection(array(
      $this
        ->getParameterCall('service_from_parameter'),
      // Also test deep collections that don't need resolving.
      $this
        ->getCollection(array(
        1,
      ), FALSE),
    )),
  );

  // To ensure getAlternatives() finds something.
  $services['service_not_exists_similar'] = array(
    'synthetic' => TRUE,
  );

  // Test configurator.
  $services['configurator'] = array(
    'synthetic' => TRUE,
  );
  $services['configurable_service'] = array(
    'class' => '\\Drupal\\Tests\\Component\\DependencyInjection\\MockService',
    'arguments' => array(),
    'configurator' => array(
      $this
        ->getServiceCall('configurator'),
      'configureService',
    ),
  );
  $services['configurable_service_exception'] = array(
    'class' => '\\Drupal\\Tests\\Component\\DependencyInjection\\MockService',
    'arguments' => array(),
    'configurator' => 'configurator_service_test_does_not_exist',
  );
  $aliases = array();
  $aliases['service.provider_alias'] = 'service.provider';
  $aliases['late.service_alias'] = 'late.service';
  return array(
    'aliases' => $aliases,
    'parameters' => $parameters,
    'services' => $services,
    'frozen' => TRUE,
    'machine_format' => $this->machineFormat,
  );
}