You are here

protected function ContainerTest::getMockContainerDefinition in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Component/DependencyInjection/ContainerTest.php \Drupal\Tests\Component\DependencyInjection\ContainerTest::getMockContainerDefinition()
  2. 10 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 694
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 = [];
  $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 = [];
  $services['service_container'] = [
    'class' => '\\Drupal\\service_container\\DependencyInjection\\Container',
  ];
  $services['other.service'] = [
    'class' => get_class($fake_service),
  ];
  $services['non_shared_service'] = [
    'class' => get_class($fake_service),
    'shared' => FALSE,
  ];
  $services['other.service_class_from_parameter'] = [
    'class' => $this
      ->getParameterCall('some_parameter_class'),
  ];
  $services['late.service'] = [
    'class' => get_class($fake_service),
  ];
  $services['service.provider'] = [
    'class' => '\\Drupal\\Tests\\Component\\DependencyInjection\\MockService',
    'arguments' => $this
      ->getCollection([
      $this
        ->getServiceCall('other.service'),
      $this
        ->getParameterCall('some_config'),
    ]),
    'properties' => $this
      ->getCollection([
      '_someProperty' => 'foo',
    ]),
    'calls' => [
      [
        'setContainer',
        $this
          ->getCollection([
          $this
            ->getServiceCall('service_container'),
        ]),
      ],
      [
        'setOtherConfigParameter',
        $this
          ->getCollection([
          $this
            ->getParameterCall('some_other_config'),
        ]),
      ],
    ],
    'priority' => 0,
  ];

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

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

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

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

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

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

  // Test configurator.
  $services['configurator'] = [
    'synthetic' => TRUE,
  ];
  $services['configurable_service'] = [
    'class' => '\\Drupal\\Tests\\Component\\DependencyInjection\\MockService',
    'arguments' => [],
    'configurator' => [
      $this
        ->getServiceCall('configurator'),
      'configureService',
    ],
  ];
  $services['configurable_service_exception'] = [
    'class' => '\\Drupal\\Tests\\Component\\DependencyInjection\\MockService',
    'arguments' => [],
    'configurator' => 'configurator_service_test_does_not_exist',
  ];

  // Raw argument
  $services['service_with_raw_argument'] = [
    'class' => '\\Drupal\\Tests\\Component\\DependencyInjection\\MockInstantiationService',
    'arguments' => $this
      ->getCollection([
      $this
        ->getRaw('ccc'),
    ]),
  ];
  $aliases = [];
  $aliases['service.provider_alias'] = 'service.provider';
  $aliases['late.service_alias'] = 'late.service';
  return [
    'aliases' => $aliases,
    'parameters' => $parameters,
    'services' => $services,
    'frozen' => TRUE,
    'machine_format' => $this->machineFormat,
  ];
}