AnnotationFileLoaderTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 AbstractAnnotationLoaderTest
  15. {
  16. protected $loader;
  17. protected $reader;
  18. protected function setUp()
  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. /**
  35. * @expectedException \InvalidArgumentException
  36. * @expectedExceptionMessage Did you forgot to add the "<?php" start tag at the beginning of the file?
  37. */
  38. public function testLoadFileWithoutStartTag()
  39. {
  40. $this->loader->load(__DIR__.'/../Fixtures/OtherAnnotatedClasses/NoStartTagClass.php');
  41. }
  42. public function testLoadVariadic()
  43. {
  44. $route = new Route(['path' => '/path/to/{id}']);
  45. $this->reader->expects($this->once())->method('getClassAnnotation');
  46. $this->reader->expects($this->once())->method('getMethodAnnotations')
  47. ->will($this->returnValue([$route]));
  48. $this->loader->load(__DIR__.'/../Fixtures/OtherAnnotatedClasses/VariadicClass.php');
  49. }
  50. /**
  51. * @requires PHP 7.0
  52. */
  53. public function testLoadAnonymousClass()
  54. {
  55. $this->reader->expects($this->never())->method('getClassAnnotation');
  56. $this->reader->expects($this->never())->method('getMethodAnnotations');
  57. $this->loader->load(__DIR__.'/../Fixtures/OtherAnnotatedClasses/AnonymousClassInTrait.php');
  58. }
  59. public function testSupports()
  60. {
  61. $fixture = __DIR__.'/../Fixtures/annotated.php';
  62. $this->assertTrue($this->loader->supports($fixture), '->supports() returns true if the resource is loadable');
  63. $this->assertFalse($this->loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  64. $this->assertTrue($this->loader->supports($fixture, 'annotation'), '->supports() checks the resource type if specified');
  65. $this->assertFalse($this->loader->supports($fixture, 'foo'), '->supports() checks the resource type if specified');
  66. }
  67. }