AnnotationClassLoaderTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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\Annotation\Route;
  12. class AnnotationClassLoaderTest extends AbstractAnnotationLoaderTest
  13. {
  14. protected $loader;
  15. protected function setUp()
  16. {
  17. parent::setUp();
  18. $this->reader = $this->getReader();
  19. $this->loader = $this->getClassLoader($this->reader);
  20. }
  21. /**
  22. * @expectedException \InvalidArgumentException
  23. */
  24. public function testLoadMissingClass()
  25. {
  26. $this->loader->load('MissingClass');
  27. }
  28. /**
  29. * @expectedException \InvalidArgumentException
  30. */
  31. public function testLoadAbstractClass()
  32. {
  33. $this->loader->load('Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\AbstractClass');
  34. }
  35. /**
  36. * @dataProvider provideTestSupportsChecksResource
  37. */
  38. public function testSupportsChecksResource($resource, $expectedSupports)
  39. {
  40. $this->assertSame($expectedSupports, $this->loader->supports($resource), '->supports() returns true if the resource is loadable');
  41. }
  42. public function provideTestSupportsChecksResource()
  43. {
  44. return array(
  45. array('class', true),
  46. array('\fully\qualified\class\name', true),
  47. array('namespaced\class\without\leading\slash', true),
  48. array('ÿClassWithLegalSpecialCharacters', true),
  49. array('5', false),
  50. array('foo.foo', false),
  51. array(null, false),
  52. );
  53. }
  54. public function testSupportsChecksTypeIfSpecified()
  55. {
  56. $this->assertTrue($this->loader->supports('class', 'annotation'), '->supports() checks the resource type if specified');
  57. $this->assertFalse($this->loader->supports('class', 'foo'), '->supports() checks the resource type if specified');
  58. }
  59. public function getLoadTests()
  60. {
  61. return array(
  62. array(
  63. 'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
  64. array('name' => 'route1'),
  65. array('arg2' => 'defaultValue2', 'arg3' => 'defaultValue3')
  66. ),
  67. array(
  68. 'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
  69. array('name' => 'route1', 'defaults' => array('arg2' => 'foo')),
  70. array('arg2' => 'defaultValue2', 'arg3' => 'defaultValue3')
  71. ),
  72. array(
  73. 'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
  74. array('name' => 'route1', 'defaults' => array('arg2' => 'foobar')),
  75. array('arg2' => false, 'arg3' => 'defaultValue3')
  76. ),
  77. );
  78. }
  79. /**
  80. * @dataProvider getLoadTests
  81. */
  82. public function testLoad($className, $routeDatas = array(), $methodArgs = array())
  83. {
  84. $routeDatas = array_replace(array(
  85. 'name' => 'route',
  86. 'path' => '/',
  87. 'requirements' => array(),
  88. 'options' => array(),
  89. 'defaults' => array(),
  90. 'schemes' => array(),
  91. 'methods' => array(),
  92. ), $routeDatas);
  93. $this->reader
  94. ->expects($this->once())
  95. ->method('getMethodAnnotations')
  96. ->will($this->returnValue(array($this->getAnnotatedRoute($routeDatas))))
  97. ;
  98. $routeCollection = $this->loader->load($className);
  99. $route = $routeCollection->get($routeDatas['name']);
  100. $this->assertSame($routeDatas['path'], $route->getPath(), '->load preserves path annotation');
  101. $this->assertSame($routeDatas['requirements'],$route->getRequirements(), '->load preserves requirements annotation');
  102. $this->assertCount(0, array_intersect($route->getOptions(), $routeDatas['options']), '->load preserves options annotation');
  103. $this->assertSame(array_replace($methodArgs, $routeDatas['defaults']), $route->getDefaults(), '->load preserves defaults annotation');
  104. }
  105. private function getAnnotatedRoute($datas)
  106. {
  107. return new Route($datas);
  108. }
  109. }