44229633b5cbc98cf568a64c8429da9e133446db
[yaffs-website] / vendor / symfony / routing / Tests / Loader / AnnotationClassLoaderTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Routing\Tests\Loader;
13
14 use Symfony\Component\Routing\Annotation\Route;
15
16 class AnnotationClassLoaderTest extends AbstractAnnotationLoaderTest
17 {
18     protected $loader;
19     private $reader;
20
21     protected function setUp()
22     {
23         parent::setUp();
24
25         $this->reader = $this->getReader();
26         $this->loader = $this->getClassLoader($this->reader);
27     }
28
29     /**
30      * @expectedException \InvalidArgumentException
31      */
32     public function testLoadMissingClass()
33     {
34         $this->loader->load('MissingClass');
35     }
36
37     /**
38      * @expectedException \InvalidArgumentException
39      */
40     public function testLoadAbstractClass()
41     {
42         $this->loader->load('Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\AbstractClass');
43     }
44
45     /**
46      * @dataProvider provideTestSupportsChecksResource
47      */
48     public function testSupportsChecksResource($resource, $expectedSupports)
49     {
50         $this->assertSame($expectedSupports, $this->loader->supports($resource), '->supports() returns true if the resource is loadable');
51     }
52
53     public function provideTestSupportsChecksResource()
54     {
55         return array(
56             array('class', true),
57             array('\fully\qualified\class\name', true),
58             array('namespaced\class\without\leading\slash', true),
59             array('ÿClassWithLegalSpecialCharacters', true),
60             array('5', false),
61             array('foo.foo', false),
62             array(null, false),
63         );
64     }
65
66     public function testSupportsChecksTypeIfSpecified()
67     {
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');
70     }
71
72     public function getLoadTests()
73     {
74         return array(
75             array(
76                 'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
77                 array('name' => 'route1', 'path' => '/path'),
78                 array('arg2' => 'defaultValue2', 'arg3' => 'defaultValue3'),
79             ),
80             array(
81                 'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
82                 array('defaults' => array('arg2' => 'foo'), 'requirements' => array('arg3' => '\w+')),
83                 array('arg2' => 'defaultValue2', 'arg3' => 'defaultValue3'),
84             ),
85             array(
86                 'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
87                 array('options' => array('foo' => 'bar')),
88                 array('arg2' => 'defaultValue2', 'arg3' => 'defaultValue3'),
89             ),
90             array(
91                 'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
92                 array('schemes' => array('https'), 'methods' => array('GET')),
93                 array('arg2' => 'defaultValue2', 'arg3' => 'defaultValue3'),
94             ),
95             array(
96                 'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
97                 array('condition' => 'context.getMethod() == "GET"'),
98                 array('arg2' => 'defaultValue2', 'arg3' => 'defaultValue3'),
99             ),
100         );
101     }
102
103     /**
104      * @dataProvider getLoadTests
105      */
106     public function testLoad($className, $routeData = array(), $methodArgs = array())
107     {
108         $routeData = array_replace(array(
109             'name' => 'route',
110             'path' => '/',
111             'requirements' => array(),
112             'options' => array(),
113             'defaults' => array(),
114             'schemes' => array(),
115             'methods' => array(),
116             'condition' => '',
117         ), $routeData);
118
119         $this->reader
120             ->expects($this->once())
121             ->method('getMethodAnnotations')
122             ->will($this->returnValue(array($this->getAnnotatedRoute($routeData))))
123         ;
124
125         $routeCollection = $this->loader->load($className);
126         $route = $routeCollection->get($routeData['name']);
127
128         $this->assertSame($routeData['path'], $route->getPath(), '->load preserves path annotation');
129         $this->assertCount(
130             count($routeData['requirements']),
131             array_intersect_assoc($routeData['requirements'], $route->getRequirements()),
132             '->load preserves requirements annotation'
133         );
134         $this->assertCount(
135             count($routeData['options']),
136             array_intersect_assoc($routeData['options'], $route->getOptions()),
137             '->load preserves options annotation'
138         );
139         $this->assertCount(
140             count($routeData['defaults']),
141             $route->getDefaults(),
142             '->load preserves defaults annotation'
143         );
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');
147     }
148
149     public function testClassRouteLoad()
150     {
151         $classRouteData = array(
152             'path' => '/prefix',
153             'schemes' => array('https'),
154             'methods' => array('GET'),
155         );
156
157         $methodRouteData = array(
158             'name' => 'route1',
159             'path' => '/path',
160             'schemes' => array('http'),
161             'methods' => array('POST', 'PUT'),
162         );
163
164         $this->reader
165             ->expects($this->once())
166             ->method('getClassAnnotation')
167             ->will($this->returnValue($this->getAnnotatedRoute($classRouteData)))
168         ;
169         $this->reader
170             ->expects($this->once())
171             ->method('getMethodAnnotations')
172             ->will($this->returnValue(array($this->getAnnotatedRoute($methodRouteData))))
173         ;
174
175         $routeCollection = $this->loader->load('Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass');
176         $route = $routeCollection->get($methodRouteData['name']);
177
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');
181     }
182
183     private function getAnnotatedRoute($data)
184     {
185         return new Route($data);
186     }
187 }