You are here

public function ServiceContainerAnnotationDiscoveryIntegrationTest::testDoctrinePlugin in Service Container 7.2

Same name and namespace in other branches
  1. 7 modules/providers/service_container_annotation_discovery/lib/Drupal/service_container_annotation_discovery/Tests/ServiceContainerAnnotationDiscoveryIntegrationTest.php \Drupal\service_container_annotation_discovery\Tests\ServiceContainerAnnotationDiscoveryIntegrationTest::testDoctrinePlugin()

Tests if plugins with annotations are available as services.

File

modules/providers/service_container_annotation_discovery/lib/Drupal/service_container_annotation_discovery/Tests/ServiceContainerAnnotationDiscoveryIntegrationTest.php, line 42
Contains \Drupal\service_container_annotation_discovery\Tests\ServiceContainerBlockIntegrationTest.

Class

ServiceContainerAnnotationDiscoveryIntegrationTest

Namespace

Drupal\service_container_annotation_discovery\Tests

Code

public function testDoctrinePlugin() {
  $plugins = array(
    array(
      'owner' => 'sc_doctrine_test',
      'type' => 'Plugin1',
      'name' => 'Plugin1A',
    ),
    array(
      'owner' => 'sc_doctrine_test',
      'type' => 'Plugin2',
      'name' => 'Plugin2B',
    ),
  );
  foreach ($plugins as $plugin) {
    $plugin_manager = $this->container
      ->get($plugin['owner'] . '.' . $plugin['type']);
    $this
      ->assertTrue($plugin_manager
      ->hasDefinition($plugin['name']));
    $object = $plugin_manager
      ->createInstance($plugin['name']);
    $this
      ->assertTrue($object instanceof PluginBase);
  }
  $plugin = array(
    'owner' => 'sc_doctrine_test',
    'type' => 'Plugin3',
    'name' => 'Plugin1C',
  );
  try {
    $this->container
      ->get($plugin['owner'] . '.' . $plugin['type']);
  } catch (\RuntimeException $e) {
    $this
      ->pass('Bad input correctly threw an exception');
  }
  $plugin = array(
    'owner' => 'sc_doctrine_test',
    'type' => 'Plugin1',
    'name' => 'Plugin3A',
  );
  $plugin_manager = $this->container
    ->get($plugin['owner'] . '.' . $plugin['type']);
  $this
    ->assertFalse($plugin_manager
    ->hasDefinition($plugin['name']));
  $plugin = array(
    'owner' => 'sc_doctrine_test',
    'type' => 'Plugin3',
    'name' => 'Plugin3A',
  );
  $plugin_manager = $this->container
    ->get($plugin['owner'] . '.' . $plugin['type']);
  $this
    ->assertTrue($plugin_manager
    ->hasDefinition($plugin['name']));
  $object = $plugin_manager
    ->createInstance($plugin['name']);
  $this
    ->assertEqual($object
    ->getData(), 'Hello world!');
  $plugin = array(
    'owner' => 'sc_doctrine_test',
    'type' => 'Plugin4',
    'name' => 'Plugin4A',
  );
  $plugin_manager = $this->container
    ->get($plugin['owner'] . '.' . $plugin['type']);
  $this
    ->assertTrue($plugin_manager
    ->hasDefinition($plugin['name']));
  $object = $plugin_manager
    ->createInstance($plugin['name']);
  $this
    ->assertTrue($object
    ->getMessenger() instanceof MessengerInterface);
}