AnnotatedClassDiscoveryTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Drupal\Tests\Component\Plugin\Discovery;
  3. use Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery;
  4. use Drupal\Component\FileCache\FileCacheFactory;
  5. use org\bovigo\vfs\vfsStream;
  6. use org\bovigo\vfs\vfsStreamDirectory;
  7. use org\bovigo\vfs\vfsStreamWrapper;
  8. use PHPUnit\Framework\TestCase;
  9. /**
  10. * @coversDefaultClass \Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery
  11. *
  12. * @group Annotation
  13. * @group Plugin
  14. */
  15. class AnnotatedClassDiscoveryTest extends TestCase {
  16. /**
  17. * All the Drupal documentation standards tags.
  18. *
  19. * @var string[]
  20. */
  21. public function provideBadAnnotations() {
  22. return [
  23. ['addtogroup'],
  24. ['code'],
  25. ['defgroup'],
  26. ['deprecated'],
  27. ['endcode'],
  28. ['endlink'],
  29. ['file'],
  30. ['ingroup'],
  31. ['group'],
  32. ['link'],
  33. ['mainpage'],
  34. ['param'],
  35. ['ref'],
  36. ['return'],
  37. ['section'],
  38. ['see'],
  39. ['subsection'],
  40. ['throws'],
  41. ['todo'],
  42. ['var'],
  43. ['{'],
  44. ['}'],
  45. ];
  46. }
  47. /**
  48. * Make sure AnnotatedClassDiscovery never tries to autoload bad annotations.
  49. *
  50. * @dataProvider provideBadAnnotations
  51. *
  52. * @coversNothing
  53. */
  54. public function testAutoloadBadAnnotations($annotation) {
  55. // Set up a class file in vfsStream.
  56. vfsStreamWrapper::register();
  57. $root = new vfsStreamDirectory('root');
  58. vfsStreamWrapper::setRoot($root);
  59. FileCacheFactory::setPrefix(__CLASS__);
  60. // Make a directory for discovery.
  61. $url = vfsStream::url('root');
  62. mkdir($url . '/DrupalTest');
  63. // Create a class docblock with our annotation.
  64. $php_file = "<?php\nnamespace DrupalTest;\n/**\n";
  65. $php_file .= " * @$annotation\n";
  66. $php_file .= " */\nclass TestClass {}";
  67. file_put_contents($url . '/DrupalTest/TestClass.php', $php_file);
  68. // Create an AnnotatedClassDiscovery object referencing the virtual file.
  69. $discovery = new AnnotatedClassDiscovery(
  70. ['\\DrupalTest\\TestClass' => [vfsStream::url('root/DrupalTest')]], '\\DrupalTest\\Component\\Annotation\\'
  71. );
  72. // Register our class loader which will fail if the annotation reader tries
  73. // to autoload disallowed annotations.
  74. $class_loader = function ($class_name) use ($annotation) {
  75. $name_array = explode('\\', $class_name);
  76. $name = array_pop($name_array);
  77. if ($name == $annotation) {
  78. $this->fail('Attempted to autoload a non-plugin annotation: ' . $name);
  79. }
  80. };
  81. spl_autoload_register($class_loader, TRUE, TRUE);
  82. // Now try to get plugin definitions.
  83. $definitions = $discovery->getDefinitions();
  84. // Unregister to clean up.
  85. spl_autoload_unregister($class_loader);
  86. // Assert that no annotations were loaded.
  87. $this->assertEmpty($definitions);
  88. }
  89. }