Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 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
        $defaults = array_replace($methodArgs, $routeData['defaults']);
140
        $this->assertCount(
141
            count($defaults),
142
            array_intersect_assoc($defaults, $route->getDefaults()),
143
            '->load preserves defaults annotation and merges them with default arguments in method signature'
144
        );
145
        $this->assertEquals($routeData['schemes'], $route->getSchemes(), '->load preserves schemes annotation');
146
        $this->assertEquals($routeData['methods'], $route->getMethods(), '->load preserves methods annotation');
147
        $this->assertSame($routeData['condition'], $route->getCondition(), '->load preserves condition annotation');
148
    }
149
 
150
    public function testClassRouteLoad()
151
    {
152
        $classRouteData = array(
153
            'path' => '/prefix',
154
            'schemes' => array('https'),
155
            'methods' => array('GET'),
156
        );
157
 
158
        $methodRouteData = array(
159
            'name' => 'route1',
160
            'path' => '/path',
161
            'schemes' => array('http'),
162
            'methods' => array('POST', 'PUT'),
163
        );
164
 
165
        $this->reader
166
            ->expects($this->once())
167
            ->method('getClassAnnotation')
168
            ->will($this->returnValue($this->getAnnotatedRoute($classRouteData)))
169
        ;
170
        $this->reader
171
            ->expects($this->once())
172
            ->method('getMethodAnnotations')
173
            ->will($this->returnValue(array($this->getAnnotatedRoute($methodRouteData))))
174
        ;
175
 
176
        $routeCollection = $this->loader->load('Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass');
177
        $route = $routeCollection->get($methodRouteData['name']);
178
 
179
        $this->assertSame($classRouteData['path'].$methodRouteData['path'], $route->getPath(), '->load concatenates class and method route path');
180
        $this->assertEquals(array_merge($classRouteData['schemes'], $methodRouteData['schemes']), $route->getSchemes(), '->load merges class and method route schemes');
181
        $this->assertEquals(array_merge($classRouteData['methods'], $methodRouteData['methods']), $route->getMethods(), '->load merges class and method route methods');
182
    }
183
 
184
    private function getAnnotatedRoute($data)
185
    {
186
        return new Route($data);
187
    }
188
}