DiscoveryTraitTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Drupal\Tests\Component\Plugin\Discovery;
  3. use Drupal\Component\Plugin\Exception\PluginNotFoundException;
  4. use PHPUnit\Framework\TestCase;
  5. /**
  6. * @group Plugin
  7. * @coversDefaultClass \Drupal\Component\Plugin\Discovery\DiscoveryTrait
  8. */
  9. class DiscoveryTraitTest extends TestCase {
  10. /**
  11. * Data provider for testDoGetDefinition().
  12. *
  13. * @return array
  14. * - Expected plugin definition.
  15. * - Plugin definition array, to pass to doGetDefinition().
  16. * - Plugin ID to get, passed to doGetDefinition().
  17. */
  18. public function providerDoGetDefinition() {
  19. return [
  20. ['definition', ['plugin_name' => 'definition'], 'plugin_name'],
  21. [NULL, ['plugin_name' => 'definition'], 'bad_plugin_name'],
  22. ];
  23. }
  24. /**
  25. * @covers ::doGetDefinition
  26. * @dataProvider providerDoGetDefinition
  27. */
  28. public function testDoGetDefinition($expected, $definitions, $plugin_id) {
  29. // Mock the trait.
  30. $trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryTrait');
  31. // Un-protect the method using reflection.
  32. $method_ref = new \ReflectionMethod($trait, 'doGetDefinition');
  33. $method_ref->setAccessible(TRUE);
  34. // Call doGetDefinition, with $exception_on_invalid always FALSE.
  35. $this->assertSame(
  36. $expected,
  37. $method_ref->invoke($trait, $definitions, $plugin_id, FALSE)
  38. );
  39. }
  40. /**
  41. * Data provider for testDoGetDefinitionException()
  42. *
  43. * @return array
  44. * - Expected plugin definition.
  45. * - Plugin definition array, to pass to doGetDefinition().
  46. * - Plugin ID to get, passed to doGetDefinition().
  47. */
  48. public function providerDoGetDefinitionException() {
  49. return [
  50. [FALSE, ['plugin_name' => 'definition'], 'bad_plugin_name'],
  51. ];
  52. }
  53. /**
  54. * @covers ::doGetDefinition
  55. * @dataProvider providerDoGetDefinitionException
  56. * @uses \Drupal\Component\Plugin\Exception\PluginNotFoundException
  57. */
  58. public function testDoGetDefinitionException($expected, $definitions, $plugin_id) {
  59. // Mock the trait.
  60. $trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryTrait');
  61. // Un-protect the method using reflection.
  62. $method_ref = new \ReflectionMethod($trait, 'doGetDefinition');
  63. $method_ref->setAccessible(TRUE);
  64. // Call doGetDefinition, with $exception_on_invalid always TRUE.
  65. $this->expectException(PluginNotFoundException::class);
  66. $method_ref->invoke($trait, $definitions, $plugin_id, TRUE);
  67. }
  68. /**
  69. * @covers ::getDefinition
  70. * @dataProvider providerDoGetDefinition
  71. */
  72. public function testGetDefinition($expected, $definitions, $plugin_id) {
  73. // Since getDefinition is a wrapper around doGetDefinition(), we can re-use
  74. // its data provider. We just have to tell abstract method getDefinitions()
  75. // to use the $definitions array.
  76. $trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryTrait');
  77. $trait->expects($this->once())
  78. ->method('getDefinitions')
  79. ->willReturn($definitions);
  80. // Call getDefinition(), with $exception_on_invalid always FALSE.
  81. $this->assertSame(
  82. $expected,
  83. $trait->getDefinition($plugin_id, FALSE)
  84. );
  85. }
  86. /**
  87. * @covers ::getDefinition
  88. * @dataProvider providerDoGetDefinitionException
  89. * @uses \Drupal\Component\Plugin\Exception\PluginNotFoundException
  90. */
  91. public function testGetDefinitionException($expected, $definitions, $plugin_id) {
  92. // Since getDefinition is a wrapper around doGetDefinition(), we can re-use
  93. // its data provider. We just have to tell abstract method getDefinitions()
  94. // to use the $definitions array.
  95. $trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryTrait');
  96. $trait->expects($this->once())
  97. ->method('getDefinitions')
  98. ->willReturn($definitions);
  99. // Call getDefinition(), with $exception_on_invalid always TRUE.
  100. $this->expectException(PluginNotFoundException::class);
  101. $trait->getDefinition($plugin_id, TRUE);
  102. }
  103. /**
  104. * Data provider for testHasDefinition().
  105. *
  106. * @return array
  107. * - Expected TRUE or FALSE.
  108. * - Plugin ID to look for.
  109. */
  110. public function providerHasDefinition() {
  111. return [
  112. [TRUE, 'valid'],
  113. [FALSE, 'not_valid'],
  114. ];
  115. }
  116. /**
  117. * @covers ::hasDefinition
  118. * @dataProvider providerHasDefinition
  119. */
  120. public function testHasDefinition($expected, $plugin_id) {
  121. $trait = $this->getMockBuilder('Drupal\Component\Plugin\Discovery\DiscoveryTrait')
  122. ->setMethods(['getDefinition'])
  123. ->getMockForTrait();
  124. // Set up our mocked getDefinition() to return TRUE for 'valid' and FALSE
  125. // for 'not_valid'.
  126. $trait->expects($this->once())
  127. ->method('getDefinition')
  128. ->will($this->returnValueMap([
  129. ['valid', FALSE, TRUE],
  130. ['not_valid', FALSE, FALSE],
  131. ]));
  132. // Call hasDefinition().
  133. $this->assertSame(
  134. $expected,
  135. $trait->hasDefinition($plugin_id)
  136. );
  137. }
  138. }