DefaultFactoryTest.php 2.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

/**
 * @file
 * Contains \Drupal\Tests\Component\Plugin\DefaultFactoryTest.
 */

namespace Drupal\Tests\Component\Plugin;

use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Tests\UnitTestCase;

/**
14
 * @coversDefaultClass \Drupal\Component\Plugin\Factory\DefaultFactory
15
 * @group Plugin
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
 */
class DefaultFactoryTest extends UnitTestCase {

  /**
   * Tests getPluginClass() with a valid plugin.
   */
  public function testGetPluginClassWithValidPlugin() {
    $plugin_class = 'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry';
    $class = DefaultFactory::getPluginClass('cherry', ['class' => $plugin_class]);

    $this->assertEquals($plugin_class, $class);
  }

  /**
   * Tests getPluginClass() with a missing class definition.
   *
   * @expectedException \Drupal\Component\Plugin\Exception\PluginException
   * @expectedExceptionMessage The plugin (cherry) did not specify an instance class.
   */
  public function testGetPluginClassWithMissingClass() {
    DefaultFactory::getPluginClass('cherry', []);
  }

  /**
   * Tests getPluginClass() with a not existing class definition.
   *
   * @expectedException \Drupal\Component\Plugin\Exception\PluginException
   * @expectedExceptionMessage Plugin (kiwifruit) instance class "\Drupal\plugin_test\Plugin\plugin_test\fruit\Kiwifruit" does not exist.
   */
  public function testGetPluginClassWithNotExistingClass() {
    DefaultFactory::getPluginClass('kiwifruit', ['class' => '\Drupal\plugin_test\Plugin\plugin_test\fruit\Kiwifruit']);
  }

  /**
   * Tests getPluginClass() with a required interface.
   */
  public function testGetPluginClassWithInterface() {
    $plugin_class = 'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry';
    $class = DefaultFactory::getPluginClass('cherry', ['class' => $plugin_class], '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');

    $this->assertEquals($plugin_class, $class);
  }

  /**
   * Tests getPluginClass() with a required interface but no implementation.
   *
   * @expectedException \Drupal\Component\Plugin\Exception\PluginException
63
   * @expectedExceptionMessage Plugin "cherry" (Drupal\plugin_test\Plugin\plugin_test\fruit\Kale) must implement interface \Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface.
64 65 66 67 68 69 70 71
   */
  public function testGetPluginClassWithInterfaceAndInvalidClass() {
    $plugin_class = 'Drupal\plugin_test\Plugin\plugin_test\fruit\Kale';
    DefaultFactory::getPluginClass('cherry', ['class' => $plugin_class, 'provider' => 'core'], '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
  }

}