AnnotationDirectoryLoaderTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\Loader\AnnotationDirectoryLoader;
  13. class AnnotationDirectoryLoaderTest extends AbstractAnnotationLoaderTestCase
  14. {
  15. protected $loader;
  16. protected $reader;
  17. protected function setUp(): void
  18. {
  19. parent::setUp();
  20. $this->reader = $this->getReader();
  21. $this->loader = new AnnotationDirectoryLoader(new FileLocator(), $this->getClassLoader($this->reader));
  22. }
  23. public function testLoad()
  24. {
  25. $this->reader->expects($this->exactly(4))->method('getClassAnnotation');
  26. $this->reader
  27. ->expects($this->any())
  28. ->method('getMethodAnnotations')
  29. ->willReturn([])
  30. ;
  31. $this->reader
  32. ->expects($this->any())
  33. ->method('getClassAnnotations')
  34. ->willReturn([])
  35. ;
  36. $this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses');
  37. }
  38. public function testLoadIgnoresHiddenDirectories()
  39. {
  40. $this->expectAnnotationsToBeReadFrom([
  41. 'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
  42. 'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BazClass',
  43. 'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\FooClass',
  44. 'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\EncodingClass',
  45. ]);
  46. $this->reader
  47. ->expects($this->any())
  48. ->method('getMethodAnnotations')
  49. ->willReturn([])
  50. ;
  51. $this->reader
  52. ->expects($this->any())
  53. ->method('getClassAnnotations')
  54. ->willReturn([])
  55. ;
  56. $this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses');
  57. }
  58. public function testSupports()
  59. {
  60. $fixturesDir = __DIR__.'/../Fixtures';
  61. $this->assertTrue($this->loader->supports($fixturesDir), '->supports() returns true if the resource is loadable');
  62. $this->assertFalse($this->loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  63. $this->assertTrue($this->loader->supports($fixturesDir, 'annotation'), '->supports() checks the resource type if specified');
  64. $this->assertFalse($this->loader->supports($fixturesDir, 'foo'), '->supports() checks the resource type if specified');
  65. }
  66. public function testItSupportsAnyAnnotation()
  67. {
  68. $this->assertTrue($this->loader->supports(__DIR__.'/../Fixtures/even-with-not-existing-folder', 'annotation'));
  69. }
  70. public function testLoadFileIfLocatedResourceIsFile()
  71. {
  72. $this->reader->expects($this->exactly(1))->method('getClassAnnotation');
  73. $this->reader
  74. ->expects($this->any())
  75. ->method('getMethodAnnotations')
  76. ->willReturn([])
  77. ;
  78. $this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses/FooClass.php');
  79. }
  80. private function expectAnnotationsToBeReadFrom(array $classes)
  81. {
  82. $this->reader->expects($this->exactly(\count($classes)))
  83. ->method('getClassAnnotation')
  84. ->with($this->callback(function (\ReflectionClass $class) use ($classes) {
  85. return \in_array($class->getName(), $classes);
  86. }));
  87. }
  88. }