4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing\Tests\Loader;
14 use Symfony\Component\Routing\Annotation\Route;
16 class AnnotationClassLoaderTest extends AbstractAnnotationLoaderTest
21 protected function setUp()
25 $this->reader = $this->getReader();
26 $this->loader = $this->getClassLoader($this->reader);
30 * @expectedException \InvalidArgumentException
32 public function testLoadMissingClass()
34 $this->loader->load('MissingClass');
38 * @expectedException \InvalidArgumentException
40 public function testLoadAbstractClass()
42 $this->loader->load('Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\AbstractClass');
46 * @dataProvider provideTestSupportsChecksResource
48 public function testSupportsChecksResource($resource, $expectedSupports)
50 $this->assertSame($expectedSupports, $this->loader->supports($resource), '->supports() returns true if the resource is loadable');
53 public function provideTestSupportsChecksResource()
57 array('\fully\qualified\class\name', true),
58 array('namespaced\class\without\leading\slash', true),
59 array('ÿClassWithLegalSpecialCharacters', true),
61 array('foo.foo', false),
66 public function testSupportsChecksTypeIfSpecified()
68 $this->assertTrue($this->loader->supports('class', 'annotation'), '->supports() checks the resource type if specified');
69 $this->assertFalse($this->loader->supports('class', 'foo'), '->supports() checks the resource type if specified');
72 public function getLoadTests()
76 'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
77 array('name' => 'route1', 'path' => '/path'),
78 array('arg2' => 'defaultValue2', 'arg3' => 'defaultValue3'),
81 'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
82 array('defaults' => array('arg2' => 'foo'), 'requirements' => array('arg3' => '\w+')),
83 array('arg2' => 'defaultValue2', 'arg3' => 'defaultValue3'),
86 'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
87 array('options' => array('foo' => 'bar')),
88 array('arg2' => 'defaultValue2', 'arg3' => 'defaultValue3'),
91 'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
92 array('schemes' => array('https'), 'methods' => array('GET')),
93 array('arg2' => 'defaultValue2', 'arg3' => 'defaultValue3'),
96 'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
97 array('condition' => 'context.getMethod() == "GET"'),
98 array('arg2' => 'defaultValue2', 'arg3' => 'defaultValue3'),
104 * @dataProvider getLoadTests
106 public function testLoad($className, $routeData = array(), $methodArgs = array())
108 $routeData = array_replace(array(
111 'requirements' => array(),
112 'options' => array(),
113 'defaults' => array(),
114 'schemes' => array(),
115 'methods' => array(),
120 ->expects($this->once())
121 ->method('getMethodAnnotations')
122 ->will($this->returnValue(array($this->getAnnotatedRoute($routeData))))
125 $routeCollection = $this->loader->load($className);
126 $route = $routeCollection->get($routeData['name']);
128 $this->assertSame($routeData['path'], $route->getPath(), '->load preserves path annotation');
130 count($routeData['requirements']),
131 array_intersect_assoc($routeData['requirements'], $route->getRequirements()),
132 '->load preserves requirements annotation'
135 count($routeData['options']),
136 array_intersect_assoc($routeData['options'], $route->getOptions()),
137 '->load preserves options annotation'
140 count($routeData['defaults']),
141 $route->getDefaults(),
142 '->load preserves defaults annotation'
144 $this->assertEquals($routeData['schemes'], $route->getSchemes(), '->load preserves schemes annotation');
145 $this->assertEquals($routeData['methods'], $route->getMethods(), '->load preserves methods annotation');
146 $this->assertSame($routeData['condition'], $route->getCondition(), '->load preserves condition annotation');
149 public function testClassRouteLoad()
151 $classRouteData = array(
153 'schemes' => array('https'),
154 'methods' => array('GET'),
157 $methodRouteData = array(
160 'schemes' => array('http'),
161 'methods' => array('POST', 'PUT'),
165 ->expects($this->once())
166 ->method('getClassAnnotation')
167 ->will($this->returnValue($this->getAnnotatedRoute($classRouteData)))
170 ->expects($this->once())
171 ->method('getMethodAnnotations')
172 ->will($this->returnValue(array($this->getAnnotatedRoute($methodRouteData))))
175 $routeCollection = $this->loader->load('Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass');
176 $route = $routeCollection->get($methodRouteData['name']);
178 $this->assertSame($classRouteData['path'].$methodRouteData['path'], $route->getPath(), '->load concatenates class and method route path');
179 $this->assertEquals(array_merge($classRouteData['schemes'], $methodRouteData['schemes']), $route->getSchemes(), '->load merges class and method route schemes');
180 $this->assertEquals(array_merge($classRouteData['methods'], $methodRouteData['methods']), $route->getMethods(), '->load merges class and method route methods');
183 private function getAnnotatedRoute($data)
185 return new Route($data);