AnnotationDirectoryLoaderTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\Routing\Loader\AnnotationDirectoryLoader;
  12. use Symfony\Component\Config\FileLocator;
  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(4))->method('getClassAnnotation');
  26. $this->reader
  27. ->expects($this->any())
  28. ->method('getMethodAnnotations')
  29. ->will($this->returnValue(array()))
  30. ;
  31. $this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses');
  32. }
  33. public function testLoadIgnoresHiddenDirectories()
  34. {
  35. $this->expectAnnotationsToBeReadFrom(array(
  36. 'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
  37. 'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BazClass',
  38. 'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BazClass',
  39. 'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\FooClass',
  40. ));
  41. $this->reader
  42. ->expects($this->any())
  43. ->method('getMethodAnnotations')
  44. ->will($this->returnValue(array()))
  45. ;
  46. $this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses');
  47. }
  48. public function testSupports()
  49. {
  50. $fixturesDir = __DIR__.'/../Fixtures';
  51. $this->assertTrue($this->loader->supports($fixturesDir), '->supports() returns true if the resource is loadable');
  52. $this->assertFalse($this->loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  53. $this->assertTrue($this->loader->supports($fixturesDir, 'annotation'), '->supports() checks the resource type if specified');
  54. $this->assertFalse($this->loader->supports($fixturesDir, 'foo'), '->supports() checks the resource type if specified');
  55. }
  56. public function testItSupportsAnyAnnotation()
  57. {
  58. $this->assertTrue($this->loader->supports(__DIR__.'/../Fixtures/even-with-not-existing-folder', 'annotation'));
  59. }
  60. public function testLoadFileIfLocatedResourceIsFile()
  61. {
  62. $this->reader->expects($this->exactly(1))->method('getClassAnnotation');
  63. $this->reader
  64. ->expects($this->any())
  65. ->method('getMethodAnnotations')
  66. ->will($this->returnValue(array()))
  67. ;
  68. $this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses/FooClass.php');
  69. }
  70. private function expectAnnotationsToBeReadFrom(array $classes)
  71. {
  72. $this->reader->expects($this->exactly(count($classes)))
  73. ->method('getClassAnnotation')
  74. ->with($this->callback(function (\ReflectionClass $class) use ($classes) {
  75. return in_array($class->getName(), $classes);
  76. }));
  77. }
  78. }