DerivativeDiscoveryDecoratorTest.php 8.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Contains \Drupal\Tests\Core\Plugin\Discovery\DerivativeDiscoveryDecoratorTest.
 */

namespace Drupal\Tests\Core\Plugin\Discovery;

use Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator;
11
use Drupal\Component\Plugin\Exception\InvalidDeriverException;
12 13 14 15
use Drupal\Tests\UnitTestCase;

/**
 * Unit tests for the derivative discovery decorator.
16 17
 *
 * @group Plugin
18 19 20
 */
class DerivativeDiscoveryDecoratorTest extends UnitTestCase {

21 22 23 24 25 26 27 28 29 30
  /**
   * The mock main discovery object.
   *
   * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $discoveryMain;

  /**
   * {@inheritdoc}
   */
31
  protected function setUp() {
32 33 34
    $this->discoveryMain = $discovery_main = $this->getMock('Drupal\Component\Plugin\Discovery\DiscoveryInterface');
  }

35 36 37 38 39 40 41 42 43
  /**
   * Tests the getDerivativeFetcher method.
   *
   * @see  \Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator::getDerivativeFetcher().
   */
  public function testGetDerivativeFetcher() {
    $definitions = array();
    $definitions['non_container_aware_discovery'] = array(
      'id' => 'non_container_aware_discovery',
44
      'deriver' => '\Drupal\Tests\Core\Plugin\Discovery\TestDerivativeDiscovery',
45 46
    );

47
    $this->discoveryMain->expects($this->any())
48 49 50
      ->method('getDefinitions')
      ->will($this->returnValue($definitions));

51
    $discovery = new DerivativeDiscoveryDecorator($this->discoveryMain);
52 53 54 55
    $definitions = $discovery->getDefinitions();

    // Ensure that both test derivatives got added.
    $this->assertEquals(2, count($definitions));
56
    $this->assertEquals('non_container_aware_discovery', $definitions['non_container_aware_discovery:test_discovery_0']['id']);
57
    $this->assertEquals('\Drupal\Tests\Core\Plugin\Discovery\TestDerivativeDiscovery', $definitions['non_container_aware_discovery:test_discovery_0']['deriver']);
58 59

    $this->assertEquals('non_container_aware_discovery', $definitions['non_container_aware_discovery:test_discovery_1']['id']);
60
    $this->assertEquals('\Drupal\Tests\Core\Plugin\Discovery\TestDerivativeDiscovery', $definitions['non_container_aware_discovery:test_discovery_1']['deriver']);
61 62 63 64 65 66 67 68 69
  }

  /**
   * Tests the getDerivativeFetcher method with objects instead of arrays.
   */
  public function testGetDerivativeFetcherWithAnnotationObjects() {
    $definitions = array();
    $definitions['non_container_aware_discovery'] = (object) array(
      'id' => 'non_container_aware_discovery',
70
      'deriver' => '\Drupal\Tests\Core\Plugin\Discovery\TestDerivativeDiscoveryWithObject',
71 72
    );

73
    $this->discoveryMain->expects($this->any())
74 75 76
      ->method('getDefinitions')
      ->will($this->returnValue($definitions));

77
    $discovery = new DerivativeDiscoveryDecorator($this->discoveryMain);
78 79 80 81 82 83
    $definitions = $discovery->getDefinitions();

    // Ensure that both test derivatives got added.
    $this->assertEquals(2, count($definitions));
    $this->assertInstanceOf('\stdClass', $definitions['non_container_aware_discovery:test_discovery_0']);
    $this->assertEquals('non_container_aware_discovery', $definitions['non_container_aware_discovery:test_discovery_0']->id);
84
    $this->assertEquals('\Drupal\Tests\Core\Plugin\Discovery\TestDerivativeDiscoveryWithObject', $definitions['non_container_aware_discovery:test_discovery_0']->deriver);
85 86 87

    $this->assertInstanceOf('\stdClass', $definitions['non_container_aware_discovery:test_discovery_1']);
    $this->assertEquals('non_container_aware_discovery', $definitions['non_container_aware_discovery:test_discovery_1']->id);
88
    $this->assertEquals('\Drupal\Tests\Core\Plugin\Discovery\TestDerivativeDiscoveryWithObject', $definitions['non_container_aware_discovery:test_discovery_1']->deriver);
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
  /**
   * Tests the getDerivativeFetcher method with a non-existent class.
   *
   * @see \Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator::getDeriver().\
   *
   * @expectedException \Drupal\Component\Plugin\Exception\InvalidDeriverException
   * @expectedExceptionMessage Plugin (non_existent_discovery) deriver "\Drupal\system\Tests\Plugin\NonExistentDeriver" does not exist.
   */
  public function testNonExistentDerivativeFetcher() {
    $definitions = array();
    // Do this with a class that doesn't exist.
    $definitions['non_existent_discovery'] = array(
      'id' => 'non_existent_discovery',
      'deriver' => '\Drupal\system\Tests\Plugin\NonExistentDeriver',
    );
    $this->discoveryMain->expects($this->any())
      ->method('getDefinitions')
      ->will($this->returnValue($definitions));

    $discovery = new DerivativeDiscoveryDecorator($this->discoveryMain);
    $discovery->getDefinitions();
  }

114 115 116
  /**
   * Tests the getDerivativeFetcher method with an invalid class.
   *
117
   * @see \Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator::getDeriver().\
118
   *
119
   * @expectedException \Drupal\Component\Plugin\Exception\InvalidDeriverException
120
   * @expectedExceptionMessage Plugin (invalid_discovery) deriver "\Drupal\system\Tests\Plugin\DerivativeTest" must implement \Drupal\Component\Plugin\Derivative\DeriverInterface.
121 122 123 124 125 126
   */
  public function testInvalidDerivativeFetcher() {
    $definitions = array();
    // Do this with a class that doesn't implement the interface.
    $definitions['invalid_discovery'] = array(
      'id' => 'invalid_discovery',
127
      'deriver' => '\Drupal\system\Tests\Plugin\DerivativeTest',
128
    );
129
    $this->discoveryMain->expects($this->any())
130 131 132
      ->method('getDefinitions')
      ->will($this->returnValue($definitions));

133
    $discovery = new DerivativeDiscoveryDecorator($this->discoveryMain);
134
    $discovery->getDefinitions();
135
  }
136 137 138 139 140 141 142 143

  /**
   * Tests derivative definitions when a definition already exists.
   */
  public function testExistingDerivative() {
    $definitions = array();
    $definitions['non_container_aware_discovery'] = array(
      'id' => 'non_container_aware_discovery',
144
      'deriver' => '\Drupal\Tests\Core\Plugin\Discovery\TestDerivativeDiscovery',
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
      'string' => 'string',
      'empty_string' => 'not_empty',
      'array' => array('one', 'two'),
      'empty_array' => array('three'),
      'null_value' => TRUE,
    );
    // This will clash with a derivative id.
    // @see \Drupal\Tests\Core\Plugin\Discovery\TestDerivativeDiscovery
    $definitions['non_container_aware_discovery:test_discovery_1'] = array(
      'id' => 'non_container_aware_discovery:test_discovery_1',
      'string' => 'string',
      'empty_string' => '',
      'array' => array('one', 'two'),
      'empty_array' => array(),
      'null_value' => NULL,
    );

    $this->discoveryMain->expects($this->any())
      ->method('getDefinitions')
      ->will($this->returnValue($definitions));

    $discovery = new DerivativeDiscoveryDecorator($this->discoveryMain);
    $returned_definitions = $discovery->getDefinitions();

    // If the definition was merged, there should only be two.
    $this->assertCount(2, $returned_definitions);

    $expected = $definitions['non_container_aware_discovery'];
    $expected['id'] = 'non_container_aware_discovery:test_discovery_1';
    $this->assertArrayEquals($expected, $returned_definitions['non_container_aware_discovery:test_discovery_1']);
  }

  /**
   * Tests a single definition when a derivative already exists.
   */
  public function testSingleExistingDerivative() {
    $base_definition = array(
      'id' => 'non_container_aware_discovery',
183
      'deriver' => '\Drupal\Tests\Core\Plugin\Discovery\TestDerivativeDiscovery',
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
      'string' => 'string',
      'empty_string' => 'not_empty',
      'array' => array('one', 'two'),
      'empty_array' => array('three'),
      'null_value' => TRUE,
    );
    // This will clash with a derivative id.
    // @see \Drupal\Tests\Core\Plugin\Discovery\TestDerivativeDiscovery
    $derivative_definition = array(
      'id' => 'non_container_aware_discovery:test_discovery_1',
      'string' => 'string',
      'empty_string' => '',
      'array' => array('one', 'two'),
      'empty_array' => array(),
      'null_value' => NULL,
    );

    $this->discoveryMain->expects($this->at(0))
      ->method('getDefinition')
      ->with('non_container_aware_discovery:test_discovery_1')
      ->will($this->returnValue($derivative_definition));
    $this->discoveryMain->expects($this->at(1))
      ->method('getDefinition')
      ->with('non_container_aware_discovery')
      ->will($this->returnValue($base_definition));

    $discovery = new DerivativeDiscoveryDecorator($this->discoveryMain);

    $expected = $base_definition;
    $expected['id'] = 'non_container_aware_discovery:test_discovery_1';
    $this->assertArrayEquals($expected, $discovery->getDefinition('non_container_aware_discovery:test_discovery_1'));
  }

217
}