No Description

AnnotationClassLoaderTest.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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' => 'defaultValue2', 'arg3' => 'defaultValue3'),
  76. ),
  77. array(
  78. 'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
  79. array('name' => 'route1', 'defaults' => array('arg2' => 'foo'), 'condition' => 'context.getMethod() == "GET"'),
  80. array('arg2' => 'defaultValue2', 'arg3' => 'defaultValue3'),
  81. ),
  82. );
  83. }
  84. /**
  85. * @dataProvider getLoadTests
  86. */
  87. public function testLoad($className, $routeDatas = array(), $methodArgs = array())
  88. {
  89. $routeDatas = array_replace(array(
  90. 'name' => 'route',
  91. 'path' => '/',
  92. 'requirements' => array(),
  93. 'options' => array(),
  94. 'defaults' => array(),
  95. 'schemes' => array(),
  96. 'methods' => array(),
  97. 'condition' => null,
  98. ), $routeDatas);
  99. $this->reader
  100. ->expects($this->once())
  101. ->method('getMethodAnnotations')
  102. ->will($this->returnValue(array($this->getAnnotatedRoute($routeDatas))))
  103. ;
  104. $routeCollection = $this->loader->load($className);
  105. $route = $routeCollection->get($routeDatas['name']);
  106. $this->assertSame($routeDatas['path'], $route->getPath(), '->load preserves path annotation');
  107. $this->assertSame($routeDatas['requirements'], $route->getRequirements(), '->load preserves requirements annotation');
  108. $this->assertCount(0, array_intersect($route->getOptions(), $routeDatas['options']), '->load preserves options annotation');
  109. $this->assertSame(array_replace($methodArgs, $routeDatas['defaults']), $route->getDefaults(), '->load preserves defaults annotation');
  110. $this->assertEquals($routeDatas['condition'], $route->getCondition(), '->load preserves condition annotation');
  111. }
  112. public function testClassRouteLoad()
  113. {
  114. $classRouteDatas = array('path' => '/classRoutePrefix');
  115. $routeDatas = array(
  116. 'name' => 'route1',
  117. 'path' => '/',
  118. );
  119. $this->reader
  120. ->expects($this->once())
  121. ->method('getClassAnnotation')
  122. ->will($this->returnValue($this->getAnnotatedRoute($classRouteDatas)))
  123. ;
  124. $this->reader
  125. ->expects($this->once())
  126. ->method('getMethodAnnotations')
  127. ->will($this->returnValue(array($this->getAnnotatedRoute($routeDatas))))
  128. ;
  129. $routeCollection = $this->loader->load('Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass');
  130. $route = $routeCollection->get($routeDatas['name']);
  131. $this->assertSame($classRouteDatas['path'].$routeDatas['path'], $route->getPath(), '->load preserves class route path annotation');
  132. }
  133. private function getAnnotatedRoute($datas)
  134. {
  135. return new Route($datas);
  136. }
  137. }