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\Loader\ObjectRouteLoader;
15
use Symfony\Component\Routing\Route;
16
use Symfony\Component\Routing\RouteCollection;
17
 
18
class ObjectRouteLoaderTest extends \PHPUnit_Framework_TestCase
19
{
20
    public function testLoadCallsServiceAndReturnsCollection()
21
    {
22
        $loader = new ObjectRouteLoaderForTest();
23
 
24
        // create a basic collection that will be returned
25
        $collection = new RouteCollection();
26
        $collection->add('foo', new Route('/foo'));
27
 
28
        $loader->loaderMap = array(
29
            'my_route_provider_service' => new RouteService($collection),
30
        );
31
 
32
        $actualRoutes = $loader->load(
33
            'my_route_provider_service:loadRoutes',
34
            'service'
35
        );
36
 
37
        $this->assertSame($collection, $actualRoutes);
38
        // the service file should be listed as a resource
39
        $this->assertNotEmpty($actualRoutes->getResources());
40
    }
41
 
42
    /**
43
     * @expectedException \InvalidArgumentException
44
     * @dataProvider getBadResourceStrings
45
     */
46
    public function testExceptionWithoutSyntax($resourceString)
47
    {
48
        $loader = new ObjectRouteLoaderForTest();
49
        $loader->load($resourceString);
50
    }
51
 
52
    public function getBadResourceStrings()
53
    {
54
        return array(
55
            array('Foo'),
56
            array('Bar::baz'),
57
            array('Foo:Bar:baz'),
58
        );
59
    }
60
 
61
    /**
62
     * @expectedException \LogicException
63
     */
64
    public function testExceptionOnNoObjectReturned()
65
    {
66
        $loader = new ObjectRouteLoaderForTest();
67
        $loader->loaderMap = array('my_service' => 'NOT_AN_OBJECT');
68
        $loader->load('my_service:method');
69
    }
70
 
71
    /**
72
     * @expectedException \BadMethodCallException
73
     */
74
    public function testExceptionOnBadMethod()
75
    {
76
        $loader = new ObjectRouteLoaderForTest();
77
        $loader->loaderMap = array('my_service' => new \stdClass());
78
        $loader->load('my_service:method');
79
    }
80
 
81
    /**
82
     * @expectedException \LogicException
83
     */
84
    public function testExceptionOnMethodNotReturningCollection()
85
    {
86
        $service = $this->getMockBuilder('stdClass')
87
            ->setMethods(array('loadRoutes'))
88
            ->getMock();
89
        $service->expects($this->once())
90
            ->method('loadRoutes')
91
            ->will($this->returnValue('NOT_A_COLLECTION'));
92
 
93
        $loader = new ObjectRouteLoaderForTest();
94
        $loader->loaderMap = array('my_service' => $service);
95
        $loader->load('my_service:loadRoutes');
96
    }
97
}
98
 
99
class ObjectRouteLoaderForTest extends ObjectRouteLoader
100
{
101
    public $loaderMap = array();
102
 
103
    protected function getServiceObject($id)
104
    {
105
        return isset($this->loaderMap[$id]) ? $this->loaderMap[$id] : null;
106
    }
107
}
108
 
109
class RouteService
110
{
111
    private $collection;
112
 
113
    public function __construct($collection)
114
    {
115
        $this->collection = $collection;
116
    }
117
 
118
    public function loadRoutes()
119
    {
120
        return $this->collection;
121
    }
122
}