AnnotationDirectoryLoaderTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 AbstractAnnotationLoaderTest
  14. {
  15. protected $loader;
  16. protected $reader;
  17. protected function setUp()
  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(3))->method('getClassAnnotation');
  26. $this->reader
  27. ->expects($this->any())
  28. ->method('getMethodAnnotations')
  29. ->will($this->returnValue([]))
  30. ;
  31. $this->reader
  32. ->expects($this->any())
  33. ->method('getClassAnnotations')
  34. ->will($this->returnValue([]))
  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. ]);
  45. $this->reader
  46. ->expects($this->any())
  47. ->method('getMethodAnnotations')
  48. ->will($this->returnValue([]))
  49. ;
  50. $this->reader
  51. ->expects($this->any())
  52. ->method('getClassAnnotations')
  53. ->will($this->returnValue([]))
  54. ;
  55. $this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses');
  56. }
  57. public function testSupports()
  58. {
  59. $fixturesDir = __DIR__.'/../Fixtures';
  60. $this->assertTrue($this->loader->supports($fixturesDir), '->supports() returns true if the resource is loadable');
  61. $this->assertFalse($this->loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  62. $this->assertTrue($this->loader->supports($fixturesDir, 'annotation'), '->supports() checks the resource type if specified');
  63. $this->assertFalse($this->loader->supports($fixturesDir, 'foo'), '->supports() checks the resource type if specified');
  64. }
  65. public function testItSupportsAnyAnnotation()
  66. {
  67. $this->assertTrue($this->loader->supports(__DIR__.'/../Fixtures/even-with-not-existing-folder', 'annotation'));
  68. }
  69. public function testLoadFileIfLocatedResourceIsFile()
  70. {
  71. $this->reader->expects($this->exactly(1))->method('getClassAnnotation');
  72. $this->reader
  73. ->expects($this->any())
  74. ->method('getMethodAnnotations')
  75. ->will($this->returnValue([]))
  76. ;
  77. $this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses/FooClass.php');
  78. }
  79. private function expectAnnotationsToBeReadFrom(array $classes)
  80. {
  81. $this->reader->expects($this->exactly(\count($classes)))
  82. ->method('getClassAnnotation')
  83. ->with($this->callback(function (\ReflectionClass $class) use ($classes) {
  84. return \in_array($class->getName(), $classes);
  85. }));
  86. }
  87. }