AnnotationFileLoaderTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Routing\Tests\Loader;
  11. use Symfony\Component\Config\FileLocator;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Routing\Loader\AnnotationFileLoader;
  14. class AnnotationFileLoaderTest extends AbstractAnnotationLoaderTestCase
  15. {
  16. protected $loader;
  17. protected $reader;
  18. protected function setUp(): void
  19. {
  20. parent::setUp();
  21. $this->reader = $this->getReader();
  22. $this->loader = new AnnotationFileLoader(new FileLocator(), $this->getClassLoader($this->reader));
  23. }
  24. public function testLoad()
  25. {
  26. $this->reader->expects($this->once())->method('getClassAnnotation');
  27. $this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses/FooClass.php');
  28. }
  29. public function testLoadTraitWithClassConstant()
  30. {
  31. $this->reader->expects($this->never())->method('getClassAnnotation');
  32. $this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses/FooTrait.php');
  33. }
  34. public function testLoadFileWithoutStartTag()
  35. {
  36. $this->expectException(\InvalidArgumentException::class);
  37. $this->expectExceptionMessage('Did you forgot to add the "<?php" start tag at the beginning of the file?');
  38. $this->loader->load(__DIR__.'/../Fixtures/OtherAnnotatedClasses/NoStartTagClass.php');
  39. }
  40. public function testLoadVariadic()
  41. {
  42. $route = new Route('/path/to/{id}');
  43. $this->reader->expects($this->once())->method('getClassAnnotation');
  44. $this->reader->expects($this->once())->method('getMethodAnnotations')
  45. ->willReturn([$route]);
  46. $this->loader->load(__DIR__.'/../Fixtures/OtherAnnotatedClasses/VariadicClass.php');
  47. }
  48. public function testLoadAnonymousClass()
  49. {
  50. $this->reader->expects($this->never())->method('getClassAnnotation');
  51. $this->reader->expects($this->never())->method('getMethodAnnotations');
  52. $this->loader->load(__DIR__.'/../Fixtures/OtherAnnotatedClasses/AnonymousClassInTrait.php');
  53. }
  54. public function testLoadAbstractClass()
  55. {
  56. $this->reader->expects($this->never())->method('getClassAnnotation');
  57. $this->reader->expects($this->never())->method('getMethodAnnotations');
  58. $this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses/AbstractClass.php');
  59. }
  60. public function testSupports()
  61. {
  62. $fixture = __DIR__.'/../Fixtures/annotated.php';
  63. $this->assertTrue($this->loader->supports($fixture), '->supports() returns true if the resource is loadable');
  64. $this->assertFalse($this->loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  65. $this->assertTrue($this->loader->supports($fixture, 'annotation'), '->supports() checks the resource type if specified');
  66. $this->assertFalse($this->loader->supports($fixture, 'foo'), '->supports() checks the resource type if specified');
  67. }
  68. /**
  69. * @requires PHP 8
  70. */
  71. public function testLoadAttributesClassAfterComma()
  72. {
  73. $this->reader->expects($this->once())->method('getClassAnnotation');
  74. $this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamAfterCommaController.php');
  75. }
  76. public function testLoadAttributesInlineClassAfterComma()
  77. {
  78. $this->reader->expects($this->once())->method('getClassAnnotation');
  79. $this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamInlineAfterCommaController.php');
  80. }
  81. /**
  82. * @requires PHP 8
  83. */
  84. public function testLoadAttributesQuotedClassAfterComma()
  85. {
  86. $this->reader->expects($this->once())->method('getClassAnnotation');
  87. $this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamQuotedAfterCommaController.php');
  88. }
  89. public function testLoadAttributesInlineQuotedClassAfterComma()
  90. {
  91. $this->reader->expects($this->once())->method('getClassAnnotation');
  92. $this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamInlineQuotedAfterCommaController.php');
  93. }
  94. /**
  95. * @requires PHP 8
  96. */
  97. public function testLoadAttributesClassAfterParenthesis()
  98. {
  99. $this->reader->expects($this->once())->method('getClassAnnotation');
  100. $this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamAfterParenthesisController.php');
  101. }
  102. public function testLoadAttributesInlineClassAfterParenthesis()
  103. {
  104. $this->reader->expects($this->once())->method('getClassAnnotation');
  105. $this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamInlineAfterParenthesisController.php');
  106. }
  107. /**
  108. * @requires PHP 8
  109. */
  110. public function testLoadAttributesQuotedClassAfterParenthesis()
  111. {
  112. $this->reader->expects($this->once())->method('getClassAnnotation');
  113. $this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamQuotedAfterParenthesisController.php');
  114. }
  115. public function testLoadAttributesInlineQuotedClassAfterParenthesis()
  116. {
  117. $this->reader->expects($this->once())->method('getClassAnnotation');
  118. $this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamInlineQuotedAfterParenthesisController.php');
  119. }
  120. }