You are here

public function OptimizedPhpArrayDumperTest::getDefinitionsDataProvider in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Component/DependencyInjection/Dumper/OptimizedPhpArrayDumperTest.php \Drupal\Tests\Component\DependencyInjection\Dumper\OptimizedPhpArrayDumperTest::getDefinitionsDataProvider()

Data provider for testGetServiceDefinitions().

Return value

array[] Returns data-set elements with:

  • parameters as returned by ContainerBuilder.
  • parameters as expected in the container definition.
  • frozen value

File

core/tests/Drupal/Tests/Component/DependencyInjection/Dumper/OptimizedPhpArrayDumperTest.php, line 246
Contains \Drupal\Tests\Component\DependencyInjection\Dumper\OptimizedPhpArrayDumperTest.

Class

OptimizedPhpArrayDumperTest
@coversDefaultClass \Drupal\Component\DependencyInjection\Dumper\OptimizedPhpArrayDumper @group DependencyInjection

Namespace

Drupal\Tests\Component\DependencyInjection\Dumper

Code

public function getDefinitionsDataProvider() {
  $base_service_definition = [
    'class' => '\\stdClass',
    'public' => TRUE,
    'file' => FALSE,
    'synthetic' => FALSE,
    'lazy' => FALSE,
    'arguments' => [],
    'arguments_count' => 0,
    'properties' => [],
    'calls' => [],
    'shared' => TRUE,
    'factory' => FALSE,
    'configurator' => FALSE,
  ];

  // Test basic flags.
  $service_definitions[] = [] + $base_service_definition;
  $service_definitions[] = [
    'public' => FALSE,
  ] + $base_service_definition;
  $service_definitions[] = [
    'file' => 'test_include.php',
  ] + $base_service_definition;
  $service_definitions[] = [
    'synthetic' => TRUE,
  ] + $base_service_definition;
  $service_definitions[] = [
    'shared' => FALSE,
  ] + $base_service_definition;
  $service_definitions[] = [
    'lazy' => TRUE,
  ] + $base_service_definition;

  // Test a basic public Reference.
  $service_definitions[] = [
    'arguments' => [
      'foo',
      new Reference('bar'),
    ],
    'arguments_count' => 2,
    'arguments_expected' => $this
      ->getCollection([
      'foo',
      $this
        ->getServiceCall('bar'),
    ]),
  ] + $base_service_definition;

  // Test a public reference that should not throw an Exception.
  $reference = new Reference('bar', ContainerInterface::NULL_ON_INVALID_REFERENCE);
  $service_definitions[] = [
    'arguments' => [
      $reference,
    ],
    'arguments_count' => 1,
    'arguments_expected' => $this
      ->getCollection([
      $this
        ->getServiceCall('bar', ContainerInterface::NULL_ON_INVALID_REFERENCE),
    ]),
  ] + $base_service_definition;

  // Test a private shared service, denoted by having a Reference.
  $private_definition = [
    'class' => '\\stdClass',
    'public' => FALSE,
    'arguments_count' => 0,
  ];
  $service_definitions[] = [
    'arguments' => [
      'foo',
      new Reference('private_definition'),
    ],
    'arguments_count' => 2,
    'arguments_expected' => $this
      ->getCollection([
      'foo',
      $this
        ->getPrivateServiceCall('private_definition', $private_definition, TRUE),
    ]),
  ] + $base_service_definition;

  // Test a private non-shared service, denoted by having a Definition.
  $private_definition_object = new Definition('\\stdClass');
  $private_definition_object
    ->setPublic(FALSE);
  $service_definitions[] = [
    'arguments' => [
      'foo',
      $private_definition_object,
    ],
    'arguments_count' => 2,
    'arguments_expected' => $this
      ->getCollection([
      'foo',
      $this
        ->getPrivateServiceCall(NULL, $private_definition),
    ]),
  ] + $base_service_definition;

  // Test a deep collection without a reference.
  $service_definitions[] = [
    'arguments' => [
      [
        [
          'foo',
        ],
      ],
    ],
    'arguments_count' => 1,
  ] + $base_service_definition;

  // Test a deep collection with a reference to resolve.
  $service_definitions[] = [
    'arguments' => [
      [
        new Reference('bar'),
      ],
    ],
    'arguments_count' => 1,
    'arguments_expected' => $this
      ->getCollection([
      $this
        ->getCollection([
        $this
          ->getServiceCall('bar'),
      ]),
    ]),
  ] + $base_service_definition;

  // Test a collection with a variable to resolve.
  $service_definitions[] = [
    'arguments' => [
      new Parameter('llama_parameter'),
    ],
    'arguments_count' => 1,
    'arguments_expected' => $this
      ->getCollection([
      $this
        ->getParameterCall('llama_parameter'),
    ]),
  ] + $base_service_definition;

  // Test objects that have _serviceId property.
  $drupal_service = new \stdClass();
  $drupal_service->_serviceId = 'bar';
  $service_definitions[] = [
    'arguments' => [
      $drupal_service,
    ],
    'arguments_count' => 1,
    'arguments_expected' => $this
      ->getCollection([
      $this
        ->getServiceCall('bar'),
    ]),
  ] + $base_service_definition;

  // Test getMethodCalls.
  $calls = [
    [
      'method',
      $this
        ->getCollection([]),
    ],
    [
      'method2',
      $this
        ->getCollection([]),
    ],
  ];
  $service_definitions[] = [
    'calls' => $calls,
  ] + $base_service_definition;
  $service_definitions[] = [
    'shared' => FALSE,
  ] + $base_service_definition;

  // Test factory.
  $service_definitions[] = [
    'factory' => [
      new Reference('bar'),
      'factoryMethod',
    ],
    'factory_expected' => [
      $this
        ->getServiceCall('bar'),
      'factoryMethod',
    ],
  ] + $base_service_definition;

  // Test invalid factory - needed to test deep dumpValue().
  $service_definitions[] = [
    'factory' => [
      [
        'foo',
        'llama',
      ],
      'factoryMethod',
    ],
  ] + $base_service_definition;

  // Test properties.
  $service_definitions[] = [
    'properties' => [
      '_value' => 'llama',
    ],
  ] + $base_service_definition;

  // Test configurator.
  $service_definitions[] = [
    'configurator' => [
      new Reference('bar'),
      'configureService',
    ],
    'configurator_expected' => [
      $this
        ->getServiceCall('bar'),
      'configureService',
    ],
  ] + $base_service_definition;
  $services_provided = [];
  $services_provided[] = [
    [],
    [],
  ];
  foreach ($service_definitions as $service_definition) {
    $definition = $this
      ->prophesize('\\Symfony\\Component\\DependencyInjection\\Definition');
    $definition
      ->getClass()
      ->willReturn($service_definition['class']);
    $definition
      ->isPublic()
      ->willReturn($service_definition['public']);
    $definition
      ->getFile()
      ->willReturn($service_definition['file']);
    $definition
      ->isSynthetic()
      ->willReturn($service_definition['synthetic']);
    $definition
      ->isLazy()
      ->willReturn($service_definition['lazy']);
    $definition
      ->getArguments()
      ->willReturn($service_definition['arguments']);
    $definition
      ->getProperties()
      ->willReturn($service_definition['properties']);
    $definition
      ->getMethodCalls()
      ->willReturn($service_definition['calls']);
    $definition
      ->isShared()
      ->willReturn($service_definition['shared']);
    $definition
      ->getDecoratedService()
      ->willReturn(NULL);
    $definition
      ->getFactory()
      ->willReturn($service_definition['factory']);
    $definition
      ->getConfigurator()
      ->willReturn($service_definition['configurator']);

    // Preserve order.
    $filtered_service_definition = [];
    foreach ($base_service_definition as $key => $value) {
      $filtered_service_definition[$key] = $service_definition[$key];
      unset($service_definition[$key]);
      if ($key == 'class' || $key == 'arguments_count') {
        continue;
      }
      if ($filtered_service_definition[$key] === $base_service_definition[$key]) {
        unset($filtered_service_definition[$key]);
      }
    }

    // Add remaining properties.
    $filtered_service_definition += $service_definition;

    // Allow to set _expected values.
    foreach ([
      'arguments',
      'factory',
      'configurator',
    ] as $key) {
      $expected = $key . '_expected';
      if (isset($filtered_service_definition[$expected])) {
        $filtered_service_definition[$key] = $filtered_service_definition[$expected];
        unset($filtered_service_definition[$expected]);
      }
    }
    if (isset($filtered_service_definition['public']) && $filtered_service_definition['public'] === FALSE) {
      $services_provided[] = [
        [
          'foo_service' => $definition
            ->reveal(),
        ],
        [],
      ];
      continue;
    }
    $services_provided[] = [
      [
        'foo_service' => $definition
          ->reveal(),
      ],
      [
        'foo_service' => $this
          ->serializeDefinition($filtered_service_definition),
      ],
    ];
  }
  return $services_provided;
}