PluginTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. 1 => 'oak',
  20. 'foo' => 'bar',
  21. 'biz' => [
  22. 'baz' => 'boom',
  23. ],
  24. 'nestedAnnotation' => new Plugin([
  25. 'foo' => 'bar',
  26. ]),
  27. ]);
  28. $this->assertEquals([
  29. // This property wasn't in our definition but is defined as a property on
  30. // our plugin class.
  31. 'defaultProperty' => 'testvalue',
  32. 1 => 'oak',
  33. 'foo' => 'bar',
  34. 'biz' => [
  35. 'baz' => 'boom',
  36. ],
  37. 'nestedAnnotation' => [
  38. 'foo' => 'bar',
  39. ],
  40. ], $plugin->get());
  41. // Without default properties, we get a completely empty plugin definition.
  42. $plugin = new Plugin([]);
  43. $this->assertEquals([], $plugin->get());
  44. }
  45. /**
  46. * @covers ::getProvider
  47. */
  48. public function testGetProvider() {
  49. $plugin = new Plugin(['provider' => 'example']);
  50. $this->assertEquals('example', $plugin->getProvider());
  51. }
  52. /**
  53. * @covers ::setProvider
  54. */
  55. public function testSetProvider() {
  56. $plugin = new Plugin([]);
  57. $plugin->setProvider('example');
  58. $this->assertEquals('example', $plugin->getProvider());
  59. }
  60. /**
  61. * @covers ::getId
  62. */
  63. public function testGetId() {
  64. $plugin = new Plugin(['id' => 'example']);
  65. $this->assertEquals('example', $plugin->getId());
  66. }
  67. /**
  68. * @covers ::getClass
  69. */
  70. public function testGetClass() {
  71. $plugin = new Plugin(['class' => 'example']);
  72. $this->assertEquals('example', $plugin->getClass());
  73. }
  74. /**
  75. * @covers ::setClass
  76. */
  77. public function testSetClass() {
  78. $plugin = new Plugin([]);
  79. $plugin->setClass('example');
  80. $this->assertEquals('example', $plugin->getClass());
  81. }
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. class PluginStub extends Plugin {
  87. protected $defaultProperty = 'testvalue';
  88. }