You are here

class DiscoveryTraitTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Component/Plugin/Discovery/DiscoveryTraitTest.php \Drupal\Tests\Component\Plugin\Discovery\DiscoveryTraitTest

@group Plugin @coversDefaultClass Drupal\Component\Plugin\Discovery\DiscoveryTrait

Hierarchy

Expanded class hierarchy of DiscoveryTraitTest

File

core/tests/Drupal/Tests/Component/Plugin/Discovery/DiscoveryTraitTest.php, line 16
Contains \Drupal\Tests\Component\Plugin\Discovery\DiscoveryTraitTest.

Namespace

Drupal\Tests\Component\Plugin\Discovery
View source
class DiscoveryTraitTest extends UnitTestCase {

  /**
   * Data provider for testDoGetDefinition().
   *
   * @return array
   *   - Expected plugin definition.
   *   - Plugin definition array, to pass to doGetDefinition().
   *   - Plugin ID to get, passed to doGetDefinition().
   */
  public function providerDoGetDefinition() {
    return array(
      [
        'definition',
        [
          'plugin_name' => 'definition',
        ],
        'plugin_name',
      ],
      [
        NULL,
        [
          'plugin_name' => 'definition',
        ],
        'bad_plugin_name',
      ],
    );
  }

  /**
   * @covers ::doGetDefinition
   * @dataProvider providerDoGetDefinition
   */
  public function testDoGetDefinition($expected, $definitions, $plugin_id) {

    // Mock the trait.
    $trait = $this
      ->getMockForTrait('Drupal\\Component\\Plugin\\Discovery\\DiscoveryTrait');

    // Un-protect the method using reflection.
    $method_ref = new \ReflectionMethod($trait, 'doGetDefinition');
    $method_ref
      ->setAccessible(TRUE);

    // Call doGetDefinition, with $exception_on_invalid always FALSE.
    $this
      ->assertSame($expected, $method_ref
      ->invoke($trait, $definitions, $plugin_id, FALSE));
  }

  /**
   * Data provider for testDoGetDefinitionException()
   *
   * @return array
   *   - Expected plugin definition.
   *   - Plugin definition array, to pass to doGetDefinition().
   *   - Plugin ID to get, passed to doGetDefinition().
   */
  public function providerDoGetDefinitionException() {
    return array(
      [
        FALSE,
        [
          'plugin_name' => 'definition',
        ],
        'bad_plugin_name',
      ],
    );
  }

  /**
   * @covers ::doGetDefinition
   * @expectedException Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @dataProvider providerDoGetDefinitionException
   * @uses Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function testDoGetDefinitionException($expected, $definitions, $plugin_id) {

    // Mock the trait.
    $trait = $this
      ->getMockForTrait('Drupal\\Component\\Plugin\\Discovery\\DiscoveryTrait');

    // Un-protect the method using reflection.
    $method_ref = new \ReflectionMethod($trait, 'doGetDefinition');
    $method_ref
      ->setAccessible(TRUE);

    // Call doGetDefinition, with $exception_on_invalid always TRUE.
    $this
      ->assertSame($expected, $method_ref
      ->invoke($trait, $definitions, $plugin_id, TRUE));
  }

  /**
   * @covers ::getDefinition
   * @dataProvider providerDoGetDefinition
   */
  public function testGetDefinition($expected, $definitions, $plugin_id) {

    // Since getDefinition is a wrapper around doGetDefinition(), we can re-use
    // its data provider. We just have to tell abstract method getDefinitions()
    // to use the $definitions array.
    $trait = $this
      ->getMockForTrait('Drupal\\Component\\Plugin\\Discovery\\DiscoveryTrait');
    $trait
      ->expects($this
      ->once())
      ->method('getDefinitions')
      ->willReturn($definitions);

    // Call getDefinition(), with $exception_on_invalid always FALSE.
    $this
      ->assertSame($expected, $trait
      ->getDefinition($plugin_id, FALSE));
  }

  /**
   * @covers ::getDefinition
   * @expectedException Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @dataProvider providerDoGetDefinitionException
   * @uses Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function testGetDefinitionException($expected, $definitions, $plugin_id) {

    // Since getDefinition is a wrapper around doGetDefinition(), we can re-use
    // its data provider. We just have to tell abstract method getDefinitions()
    // to use the $definitions array.
    $trait = $this
      ->getMockForTrait('Drupal\\Component\\Plugin\\Discovery\\DiscoveryTrait');
    $trait
      ->expects($this
      ->once())
      ->method('getDefinitions')
      ->willReturn($definitions);

    // Call getDefinition(), with $exception_on_invalid always TRUE.
    $this
      ->assertSame($expected, $trait
      ->getDefinition($plugin_id, TRUE));
  }

  /**
   * Data provider for testHasDefinition().
   *
   * @return array
   *   - Expected TRUE or FALSE.
   *   - Plugin ID to look for.
   */
  public function providerHasDefinition() {
    return array(
      [
        TRUE,
        'valid',
      ],
      [
        FALSE,
        'not_valid',
      ],
    );
  }

  /**
   * @covers ::hasDefinition
   * @dataProvider providerHasDefinition
   */
  public function testHasDefinition($expected, $plugin_id) {
    $trait = $this
      ->getMockBuilder('Drupal\\Component\\Plugin\\Discovery\\DiscoveryTrait')
      ->setMethods(array(
      'getDefinition',
    ))
      ->getMockForTrait();

    // Set up our mocked getDefinition() to return TRUE for 'valid' and FALSE
    // for 'not_valid'.
    $trait
      ->expects($this
      ->once())
      ->method('getDefinition')
      ->will($this
      ->returnValueMap(array(
      [
        'valid',
        FALSE,
        TRUE,
      ],
      [
        'not_valid',
        FALSE,
        FALSE,
      ],
    )));

    // Call hasDefinition().
    $this
      ->assertSame($expected, $trait
      ->hasDefinition($plugin_id));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DiscoveryTraitTest::providerDoGetDefinition public function Data provider for testDoGetDefinition().
DiscoveryTraitTest::providerDoGetDefinitionException public function Data provider for testDoGetDefinitionException()
DiscoveryTraitTest::providerHasDefinition public function Data provider for testHasDefinition().
DiscoveryTraitTest::testDoGetDefinition public function @covers ::doGetDefinition @dataProvider providerDoGetDefinition
DiscoveryTraitTest::testDoGetDefinitionException public function @covers ::doGetDefinition @expectedException Drupal\Component\Plugin\Exception\PluginNotFoundException @dataProvider providerDoGetDefinitionException @uses Drupal\Component\Plugin\Exception\PluginNotFoundException
DiscoveryTraitTest::testGetDefinition public function @covers ::getDefinition @dataProvider providerDoGetDefinition
DiscoveryTraitTest::testGetDefinitionException public function @covers ::getDefinition @expectedException Drupal\Component\Plugin\Exception\PluginNotFoundException @dataProvider providerDoGetDefinitionException @uses Drupal\Component\Plugin\Exception\PluginNotFoundException
DiscoveryTraitTest::testHasDefinition public function @covers ::hasDefinition @dataProvider providerHasDefinition
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 259