PluginTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace Drupal\Tests\Component\Annotation;
  3. use Drupal\Component\Annotation\Plugin;
  4. use PHPUnit\Framework\TestCase;
  5. /**
  6. * @coversDefaultClass \Drupal\Component\Annotation\Plugin
  7. * @group Annotation
  8. */
  9. class PluginTest extends TestCase {
  10. /**
  11. * @covers ::__construct
  12. * @covers ::parse
  13. * @covers ::get
  14. */
  15. public function testGet() {
  16. // Assert all values are accepted through constructor and default value is
  17. // used for non existent but defined property.
  18. $plugin = new PluginStub([
  19. 'foo' => 'bar',
  20. 'biz' => [
  21. 'baz' => 'boom',
  22. ],
  23. 'nestedAnnotation' => new Plugin([
  24. 'foo' => 'bar',
  25. ]),
  26. ]);
  27. $this->assertEquals([
  28. // This property wasn't in our definition but is defined as a property on
  29. // our plugin class.
  30. 'defaultProperty' => 'testvalue',
  31. 'foo' => 'bar',
  32. 'biz' => [
  33. 'baz' => 'boom',
  34. ],
  35. 'nestedAnnotation' => [
  36. 'foo' => 'bar',
  37. ],
  38. ], $plugin->get());
  39. // Without default properties, we get a completely empty plugin definition.
  40. $plugin = new Plugin([]);
  41. $this->assertEquals([], $plugin->get());
  42. }
  43. /**
  44. * @covers ::getProvider
  45. */
  46. public function testGetProvider() {
  47. $plugin = new Plugin(['provider' => 'example']);
  48. $this->assertEquals('example', $plugin->getProvider());
  49. }
  50. /**
  51. * @covers ::setProvider
  52. */
  53. public function testSetProvider() {
  54. $plugin = new Plugin([]);
  55. $plugin->setProvider('example');
  56. $this->assertEquals('example', $plugin->getProvider());
  57. }
  58. /**
  59. * @covers ::getId
  60. */
  61. public function testGetId() {
  62. $plugin = new Plugin(['id' => 'example']);
  63. $this->assertEquals('example', $plugin->getId());
  64. }
  65. /**
  66. * @covers ::getClass
  67. */
  68. public function testGetClass() {
  69. $plugin = new Plugin(['class' => 'example']);
  70. $this->assertEquals('example', $plugin->getClass());
  71. }
  72. /**
  73. * @covers ::setClass
  74. */
  75. public function testSetClass() {
  76. $plugin = new Plugin([]);
  77. $plugin->setClass('example');
  78. $this->assertEquals('example', $plugin->getClass());
  79. }
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. class PluginStub extends Plugin {
  85. protected $defaultProperty = 'testvalue';
  86. }