DefaultPluginBagTest.php 5.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
<?php

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

namespace Drupal\Tests\Component\Plugin;

use Drupal\Component\Plugin\DefaultPluginBag;
use Drupal\Tests\UnitTestCase;

/**
 * Tests the default plugin bag.
 *
 * @see \Drupal\Component\Plugin\DefaultPluginBag
 *
 * @group Drupal
 * @group Drupal_Plugin
 */
class DefaultPluginBagTest extends UnitTestCase {

  /**
   * The mocked plugin manager.
   *
   * @var \PHPUnit_Framework_MockObject_MockObject|\Drupal\Component\Plugin\PluginManagerInterface
   */
  protected $pluginManager;

  /**
   * The tested plugin bag.
   *
   * @var \PHPUnit_Framework_MockObject_MockObject|\Drupal\Component\Plugin\DefaultPluginBag
   */
  protected $defaultPluginBag;

  /**
   * Stores all setup plugin instances.
   *
   * @var array
   */
  protected $pluginInstances;

  /**
   * Contains the plugin configuration.
   *
   * @var array
   */
  protected $config = array(
    'banana' => array('id' => 'banana', 'key' => 'value'),
    'cherry' => array('id' => 'cherry', 'key' => 'value'),
    'apple' => array('id' => 'apple', 'key' => 'value'),
  );

  public static function getInfo() {
    return array(
      'name' => 'Default plugin bag',
      'description' => 'Tests the default plugin bag.',
      'group' => 'PHP Storage',
    );
  }

  protected function setUp() {
    $this->pluginManager = $this->getMock('Drupal\Component\Plugin\PluginManagerInterface');
    $definitions = $this->getPluginDefinitions();
    $this->pluginManager->expects($this->any())
      ->method('getDefinitions')
      ->will($this->returnValue($definitions));

    $this->pluginInstances = array();
    $map = array();
    foreach ($definitions as $plugin_id => $definition) {
      // Create a mock plugin instance.
      $mock = $this->getMock('Drupal\Component\Plugin\PluginInspectionInterface');
      $mock->expects($this->any())
        ->method('getPluginId')
        ->will($this->returnValue($plugin_id));
      $this->pluginInstances[$plugin_id] = $mock;

      $map[] = array($plugin_id, $this->config[$plugin_id], $this->pluginInstances[$plugin_id]);
    }
    $this->pluginManager->expects($this->any())
      ->method('createInstance')
      ->will($this->returnValueMap($map));

    $this->defaultPluginBag = new DefaultPluginBag($this->pluginManager, $this->config);
  }

  /**
   * Tests the has method.
   *
   * @see \Drupal\Component\Plugin\DefaultPluginBag::has()
   */
  public function testHas() {
    $definitions = $this->getPluginDefinitions();

    $this->assertFalse($this->defaultPluginBag->has($this->randomName()), 'Nonexistent plugin found.');

    foreach (array_keys($definitions) as $plugin_id) {
      $this->assertTrue($this->defaultPluginBag->has($plugin_id));
    }
  }

  /**
   * Tests the get method.
   *
   * @see \Drupal\Component\Plugin\DefaultPluginBag::get()
   */
  public function testGet() {
    $apple = $this->pluginInstances['apple'];

    $this->assertEquals($apple, $this->defaultPluginBag->get('apple'));
  }

  /**
   * Tests the get method with an non existing plugin ID.
   *
   * @expectedException \Drupal\Component\Plugin\Exception\PluginException
   */
  public function testGetNotExistingPlugin() {
    $this->defaultPluginBag->get('pear');
  }

  /**
   * Returns some example plugin definitions.
   *
   * @return array
   *   The example plugin definitions.
   */
  protected function getPluginDefinitions() {
    $definitions = array(
      'apple' => array(
        'id' => 'apple',
        'label' => 'Apple',
        'color' => 'green',
        'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Apple',
        'provider' => 'plugin_test',
      ),
      'banana' => array(
        'id' => 'banana',
        'label' => 'Banana',
        'color' => 'yellow',
        'uses' => array(
          'bread' => 'Banana bread',
        ),
        'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Banana',
        'provider' => 'plugin_test',
      ),
      'cherry' => array(
        'id' => 'cherry',
        'label' => 'Cherry',
        'color' => 'red',
        'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry',
        'provider' => 'plugin_test',
      ),
    );
    return $definitions;
  }

  /**
   * Provides test data for testSortHelper.
   *
   * @return array
   *   The test data.
   */
  public function providerTestSortHelper() {
    return array(
      array('apple', 'apple', 0),
      array('apple', 'cherry', -1),
      array('cherry', 'apple', 1),
      array('cherry', 'banana', 1),
    );
  }

  /**
   * Tests the sort helper.
   *
   * @param string $plugin_id_1
   *   The first plugin ID.
   * @param string $plugin_id_2
   *   The second plugin ID.
   * @param int $expected
   *   The expected result.
   *
   * @dataProvider providerTestSortHelper
   */
  public function testSortHelper($plugin_id_1, $plugin_id_2, $expected) {
    if ($expected != 0) {
      $expected = $expected > 0 ? 1 : -1;
    }
    $this->assertEquals($expected, $this->defaultPluginBag->sortHelper($plugin_id_1, $plugin_id_2));
  }

  /**
   * Tests the configuration getter method.
   *
   * @see \Drupal\Component\Plugin\DefaultPluginBag::getConfiguration()
   */
  public function testGetConfiguration() {
    // The expected order matches $this->config.
    $expected = array('banana', 'cherry', 'apple');

    $config = $this->defaultPluginBag->getConfiguration();
    $this->assertSame($expected, array_keys($config), 'The order of the configuration is unchanged.');

206
    $ids = $this->defaultPluginBag->getInstanceIds();
207 208 209 210 211 212
    $this->assertSame($expected, array_keys($ids), 'The order of the instances is unchanged.');

    $this->defaultPluginBag->sort();
    $config = $this->defaultPluginBag->getConfiguration();
    $this->assertSame($expected, array_keys($config), 'After sorting, the order of the configuration is unchanged.');

213
    $ids = $this->defaultPluginBag->getInstanceIds();
214 215 216 217 218
    sort($expected);
    $this->assertSame($expected, array_keys($ids), 'After sorting, the order of the instances is also sorted.');
  }

}