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\Matcher;
13
 
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\Routing\Route;
16
use Symfony\Component\Routing\RouteCollection;
17
use Symfony\Component\Routing\RequestContext;
18
use Symfony\Component\Routing\Matcher\TraceableUrlMatcher;
19
 
20
class TraceableUrlMatcherTest extends \PHPUnit_Framework_TestCase
21
{
22
    public function test()
23
    {
24
        $coll = new RouteCollection();
25
        $coll->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('POST')));
26
        $coll->add('bar', new Route('/bar/{id}', array(), array('id' => '\d+')));
27
        $coll->add('bar1', new Route('/bar/{name}', array(), array('id' => '\w+'), array(), '', array(), array('POST')));
28
        $coll->add('bar2', new Route('/foo', array(), array(), array(), 'baz'));
29
        $coll->add('bar3', new Route('/foo1', array(), array(), array(), 'baz'));
30
        $coll->add('bar4', new Route('/foo2', array(), array(), array(), 'baz', array(), array(), 'context.getMethod() == "GET"'));
31
 
32
        $context = new RequestContext();
33
        $context->setHost('baz');
34
 
35
        $matcher = new TraceableUrlMatcher($coll, $context);
36
        $traces = $matcher->getTraces('/babar');
37
        $this->assertSame(array(0, 0, 0, 0, 0, 0), $this->getLevels($traces));
38
 
39
        $traces = $matcher->getTraces('/foo');
40
        $this->assertSame(array(1, 0, 0, 2), $this->getLevels($traces));
41
 
42
        $traces = $matcher->getTraces('/bar/12');
43
        $this->assertSame(array(0, 2), $this->getLevels($traces));
44
 
45
        $traces = $matcher->getTraces('/bar/dd');
46
        $this->assertSame(array(0, 1, 1, 0, 0, 0), $this->getLevels($traces));
47
 
48
        $traces = $matcher->getTraces('/foo1');
49
        $this->assertSame(array(0, 0, 0, 0, 2), $this->getLevels($traces));
50
 
51
        $context->setMethod('POST');
52
        $traces = $matcher->getTraces('/foo');
53
        $this->assertSame(array(2), $this->getLevels($traces));
54
 
55
        $traces = $matcher->getTraces('/bar/dd');
56
        $this->assertSame(array(0, 1, 2), $this->getLevels($traces));
57
 
58
        $traces = $matcher->getTraces('/foo2');
59
        $this->assertSame(array(0, 0, 0, 0, 0, 1), $this->getLevels($traces));
60
    }
61
 
62
    public function testMatchRouteOnMultipleHosts()
63
    {
64
        $routes = new RouteCollection();
65
        $routes->add('first', new Route(
66
            '/mypath/',
67
            array('_controller' => 'MainBundle:Info:first'),
68
            array(),
69
            array(),
70
            'some.example.com'
71
        ));
72
 
73
        $routes->add('second', new Route(
74
            '/mypath/',
75
            array('_controller' => 'MainBundle:Info:second'),
76
            array(),
77
            array(),
78
            'another.example.com'
79
        ));
80
 
81
        $context = new RequestContext();
82
        $context->setHost('baz');
83
 
84
        $matcher = new TraceableUrlMatcher($routes, $context);
85
 
86
        $traces = $matcher->getTraces('/mypath/');
87
        $this->assertSame(
88
            array(TraceableUrlMatcher::ROUTE_ALMOST_MATCHES, TraceableUrlMatcher::ROUTE_ALMOST_MATCHES),
89
            $this->getLevels($traces)
90
        );
91
    }
92
 
93
    public function getLevels($traces)
94
    {
95
        $levels = array();
96
        foreach ($traces as $trace) {
97
            $levels[] = $trace['level'];
98
        }
99
 
100
        return $levels;
101
    }
102
 
103
    public function testRoutesWithConditions()
104
    {
105
        $routes = new RouteCollection();
106
        $routes->add('foo', new Route('/foo', array(), array(), array(), 'baz', array(), array(), "request.headers.get('User-Agent') matches '/firefox/i'"));
107
 
108
        $context = new RequestContext();
109
        $context->setHost('baz');
110
 
111
        $matcher = new TraceableUrlMatcher($routes, $context);
112
 
113
        $notMatchingRequest = Request::create('/foo', 'GET');
114
        $traces = $matcher->getTracesForRequest($notMatchingRequest);
115
        $this->assertEquals("Condition \"request.headers.get('User-Agent') matches '/firefox/i'\" does not evaluate to \"true\"", $traces[0]['log']);
116
 
117
        $matchingRequest = Request::create('/foo', 'GET', array(), array(), array(), array('HTTP_USER_AGENT' => 'Firefox'));
118
        $traces = $matcher->getTracesForRequest($matchingRequest);
119
        $this->assertEquals('Route matches!', $traces[0]['log']);
120
    }
121
}